2019-05-06 14:28:42 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class AO3Bridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'AO3';
|
|
|
|
const URI = 'https://archiveofourown.org/';
|
|
|
|
const CACHE_TIMEOUT = 1800;
|
|
|
|
const DESCRIPTION = 'Returns works or chapters from Archive of Our Own';
|
|
|
|
const MAINTAINER = 'Obsidienne';
|
|
|
|
const PARAMETERS = [
|
|
|
|
'List' => [
|
|
|
|
'url' => [
|
|
|
|
'name' => 'url',
|
|
|
|
'required' => true,
|
2024-02-16 06:14:17 +03:00
|
|
|
// Example: F/F tag
|
|
|
|
'exampleValue' => 'https://archiveofourown.org/tags/F*s*F/works',
|
|
|
|
],
|
|
|
|
'range' => [
|
|
|
|
'name' => 'Chapter Content',
|
|
|
|
'title' => 'Chapter(s) to include in each work\'s feed entry',
|
|
|
|
'defaultValue' => null,
|
|
|
|
'type' => 'list',
|
|
|
|
'values' => [
|
|
|
|
'None' => null,
|
|
|
|
'First' => 'first',
|
|
|
|
'Latest' => 'last',
|
|
|
|
'Entire work' => 'all',
|
|
|
|
],
|
2019-05-06 14:28:42 +03:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'Bookmarks' => [
|
|
|
|
'user' => [
|
|
|
|
'name' => 'user',
|
|
|
|
'required' => true,
|
|
|
|
// Example: Nyaaru's bookmarks
|
|
|
|
'exampleValue' => 'Nyaaru',
|
2022-07-01 16:10:30 +03:00
|
|
|
],
|
2019-05-06 14:28:42 +03:00
|
|
|
],
|
|
|
|
'Work' => [
|
|
|
|
'id' => [
|
|
|
|
'name' => 'id',
|
|
|
|
'required' => true,
|
|
|
|
// Example: latest chapters from A Better Past by LysSerris
|
|
|
|
'exampleValue' => '18181853',
|
|
|
|
],
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
2019-05-06 14:28:42 +03:00
|
|
|
];
|
2023-09-10 22:50:15 +03:00
|
|
|
private $title;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-12-13 11:53:42 +03:00
|
|
|
public function collectData()
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'Bookmarks':
|
2024-02-16 06:14:17 +03:00
|
|
|
$this->collectList($this->getURI());
|
2022-12-13 11:53:42 +03:00
|
|
|
break;
|
|
|
|
case 'List':
|
2024-02-16 06:14:17 +03:00
|
|
|
$this->collectList($this->getURI());
|
2022-12-13 11:53:42 +03:00
|
|
|
break;
|
|
|
|
case 'Work':
|
2024-02-16 06:14:17 +03:00
|
|
|
$this->collectWork($this->getURI());
|
2022-12-13 11:53:42 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Feed for lists of works (e.g. recent works, search results, filtered tags,
|
|
|
|
* bookmarks, series, collections).
|
|
|
|
*/
|
2019-05-06 14:28:42 +03:00
|
|
|
private function collectList($url)
|
|
|
|
{
|
2024-02-16 06:14:17 +03:00
|
|
|
$version = 'v0.0.1';
|
2024-08-30 05:21:51 +03:00
|
|
|
$headers = [
|
|
|
|
"useragent: rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)"
|
|
|
|
];
|
|
|
|
$response = getContents($url, $headers);
|
2024-02-16 06:14:17 +03:00
|
|
|
|
2024-08-30 05:21:51 +03:00
|
|
|
$html = \str_get_html($response);
|
2019-05-06 14:28:42 +03:00
|
|
|
$html = defaultLinkTo($html, self::URI);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-02-16 06:14:17 +03:00
|
|
|
// Get list title. Will include page range + count in some cases
|
2024-03-31 23:41:58 +03:00
|
|
|
$heading = ($html->find('#main h2', 0));
|
2024-02-16 06:14:17 +03:00
|
|
|
if ($heading->find('a.tag')) {
|
|
|
|
$heading = $heading->find('a.tag', 0);
|
|
|
|
}
|
|
|
|
$this->title = $heading->plaintext;
|
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
foreach ($html->find('.index.group > li') as $element) {
|
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$title = $element->find('div h4 a', 0);
|
|
|
|
if (!isset($title)) {
|
|
|
|
continue; // discard deleted works
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-05-06 14:28:42 +03:00
|
|
|
$item['title'] = $title->plaintext;
|
|
|
|
$item['uri'] = $title->href;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$strdate = $element->find('div p.datetime', 0)->plaintext;
|
|
|
|
$item['timestamp'] = strtotime($strdate);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-03-28 21:43:17 +03:00
|
|
|
// detach from rest of page because remove() is buggy
|
|
|
|
$element = str_get_html($element->outertext());
|
|
|
|
$tags = $element->find('ul.required-tags', 0);
|
|
|
|
foreach ($tags->childNodes() as $tag) {
|
|
|
|
$item['categories'][] = html_entity_decode($tag->plaintext);
|
|
|
|
}
|
|
|
|
$tags->remove();
|
|
|
|
$tags = $element->find('ul.tags', 0);
|
|
|
|
foreach ($tags->childNodes() as $tag) {
|
|
|
|
$item['categories'][] = html_entity_decode($tag->plaintext);
|
|
|
|
}
|
|
|
|
$tags->remove();
|
|
|
|
|
|
|
|
$item['content'] = implode('', $element->childNodes());
|
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$chapters = $element->find('dl dd.chapters', 0);
|
|
|
|
// bookmarked series and external works do not have a chapters count
|
|
|
|
$chapters = (isset($chapters) ? $chapters->plaintext : 0);
|
|
|
|
$item['uid'] = $item['uri'] . "/$strdate/$chapters";
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-02-16 06:14:17 +03:00
|
|
|
// Fetch workskin of desired chapter(s) in list
|
|
|
|
if ($this->getInput('range')) {
|
|
|
|
$url = $item['uri'];
|
|
|
|
switch ($this->getInput('range')) {
|
|
|
|
case ('all'):
|
|
|
|
$url .= '?view_full_work=true';
|
|
|
|
break;
|
|
|
|
case ('first'):
|
|
|
|
break;
|
|
|
|
case ('last'):
|
|
|
|
// only way to get this is using the navigate page unfortunately
|
|
|
|
$url .= '/navigate';
|
2024-08-30 05:21:51 +03:00
|
|
|
$response = getContents($url, $headers);
|
|
|
|
$html = \str_get_html($response);
|
2024-02-16 06:14:17 +03:00
|
|
|
$html = defaultLinkTo($html, self::URI);
|
|
|
|
$url = $html->find('ol.index.group > li > a', -1)->href;
|
|
|
|
break;
|
|
|
|
}
|
2024-08-30 05:21:51 +03:00
|
|
|
$response = getContents($url, $headers);
|
|
|
|
|
|
|
|
$html = \str_get_html($response);
|
2024-02-16 06:14:17 +03:00
|
|
|
$html = defaultLinkTo($html, self::URI);
|
2024-03-28 21:43:17 +03:00
|
|
|
// remove duplicate fic summary
|
|
|
|
if ($ficsum = $html->find('#workskin > .preface > .summary', 0)) {
|
|
|
|
$ficsum->remove();
|
|
|
|
}
|
2024-02-16 06:14:17 +03:00
|
|
|
$item['content'] .= $html->find('#workskin', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use predictability of download links to generate enclosures
|
|
|
|
$wid = explode('/', $item['uri'])[4];
|
|
|
|
foreach (['azw3', 'epub', 'mobi', 'pdf', 'html'] as $ext) {
|
|
|
|
$item['enclosures'][] = 'https://archiveofourown.org/downloads/' . $wid . '/work.' . $ext;
|
|
|
|
}
|
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-05-06 14:28:42 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-12-13 11:53:42 +03:00
|
|
|
/**
|
|
|
|
* Feed for recent chapters of a specific work.
|
|
|
|
*/
|
2024-02-16 06:14:17 +03:00
|
|
|
private function collectWork($url)
|
2019-05-06 14:28:42 +03:00
|
|
|
{
|
2023-09-10 22:50:15 +03:00
|
|
|
$version = 'v0.0.1';
|
2024-08-30 05:21:51 +03:00
|
|
|
$headers = [
|
|
|
|
"useragent: rss-bridge $version (https://github.com/RSS-Bridge/rss-bridge)"
|
|
|
|
];
|
|
|
|
$response = getContents($url . '/navigate', $headers);
|
2023-07-16 23:07:34 +03:00
|
|
|
|
2024-08-30 05:21:51 +03:00
|
|
|
$html = \str_get_html($response);
|
2019-05-06 14:28:42 +03:00
|
|
|
$html = defaultLinkTo($html, self::URI);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-08-30 05:21:51 +03:00
|
|
|
$response = getContents($url . '?view_full_work=true', $headers);
|
|
|
|
|
|
|
|
$workhtml = \str_get_html($response);
|
2024-02-16 06:14:17 +03:00
|
|
|
$workhtml = defaultLinkTo($workhtml, self::URI);
|
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$this->title = $html->find('h2 a', 0)->plaintext;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-02-16 06:14:17 +03:00
|
|
|
$nav = $html->find('ol.index.group > li');
|
|
|
|
for ($i = 0; $i < count($nav); $i++) {
|
2019-05-06 14:28:42 +03:00
|
|
|
$item = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-02-16 06:14:17 +03:00
|
|
|
$element = $nav[$i];
|
2019-05-06 14:28:42 +03:00
|
|
|
$item['title'] = $element->find('a', 0)->plaintext;
|
2024-02-16 06:14:17 +03:00
|
|
|
$item['content'] = $workhtml->find('#chapter-' . ($i + 1), 0);
|
2019-05-06 14:28:42 +03:00
|
|
|
$item['uri'] = $element->find('a', 0)->href;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$strdate = $element->find('span.datetime', 0)->plaintext;
|
|
|
|
$strdate = str_replace('(', '', $strdate);
|
|
|
|
$strdate = str_replace(')', '', $strdate);
|
|
|
|
$item['timestamp'] = strtotime($strdate);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$item['uid'] = $item['uri'] . "/$strdate";
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
$this->items = array_reverse($this->items);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
$name = parent::getName() . " $this->queriedContext";
|
|
|
|
if (isset($this->title)) {
|
|
|
|
$name .= " - $this->title";
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-05-06 14:28:42 +03:00
|
|
|
return $name;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-06 14:28:42 +03:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return self::URI . '/favicon.ico';
|
|
|
|
}
|
2024-02-16 06:14:17 +03:00
|
|
|
|
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
$url = parent::getURI();
|
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case 'Bookmarks':
|
|
|
|
$user = $this->getInput('user');
|
|
|
|
$url = self::URI
|
|
|
|
. '/users/' . $user
|
|
|
|
. '/bookmarks?bookmark_search[sort_column]=bookmarkable_date';
|
|
|
|
break;
|
|
|
|
case 'List':
|
|
|
|
$url = $this->getInput('url');
|
|
|
|
break;
|
|
|
|
case 'Work':
|
|
|
|
$url = self::URI . '/works/' . $this->getInput('id');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
2019-05-06 14:28:42 +03:00
|
|
|
}
|