[NyaaTorrents] Rewrite as Feed Expander (#2073)

NyaaTorrents allows search criteria as URL parameters in RSS feed so we just need to expand feed items
This commit is contained in:
ORelio 2021-04-19 18:59:51 +02:00 committed by GitHub
parent 76c38332ee
commit 00a24a98be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
<?php <?php
class NyaaTorrentsBridge extends BridgeAbstract { class NyaaTorrentsBridge extends FeedExpander {
const MAINTAINER = 'ORelio'; const MAINTAINER = 'ORelio';
const NAME = 'NyaaTorrents'; const NAME = 'NyaaTorrents';
@ -64,57 +64,29 @@ class NyaaTorrentsBridge extends BridgeAbstract {
} }
public function collectData(){ public function collectData(){
$this->collectExpandableDatas(
// Determine base URL, either home page or user page self::URI . '?page=rss&s=id&o=desc&'
$base_uri = self::URI;
if (!empty($this->getInput('u')))
$base_uri = $base_uri . 'user/' . urlencode($this->getInput('u'));
// Build Search URL from base URL and search criteria
$search_url = $base_uri . '?s=id&o=desc&'
. http_build_query(array( . http_build_query(array(
'f' => $this->getInput('f'), 'f' => $this->getInput('f'),
'c' => $this->getInput('c'), 'c' => $this->getInput('c'),
'q' => $this->getInput('q') 'q' => $this->getInput('q'),
)); 'u' => $this->getInput('u')
)), 20);
// Retrieve torrent listing from search results, which does not contain torrent description
$html = getSimpleHTMLDOM($search_url)
or returnServerError('Could not request Nyaa: ' . $search_url);
$links = $html->find('a');
$results = array();
foreach ($links as $link)
if (strpos($link->href, '/view/') === 0 && !in_array($link->href, $results))
$results[] = $link->href;
if (empty($results) && empty($this->getInput('q')))
returnServerError('No results from Nyaa: ' . $url, 500);
//Process each item individually
foreach ($results as $element) {
//Limit total amount of requests
if(count($this->items) >= 20) {
break;
} }
$torrent_id = str_replace('/view/', '', $element); protected function parseItem($newItem){
$item = parent::parseItem($newItem);
//Ignore entries without valid torrent ID //Convert URI from torrent file to web page
if ($torrent_id != 0 && ctype_digit($torrent_id)) { $item['uri'] = str_replace('/download/', '/view/', $item['uri']);
$item['uri'] = str_replace('.torrent', '', $item['uri']);
//Retrieve data for this torrent ID if ($item_html = getSimpleHTMLDOMCached($item['uri'])) {
$item_uri = self::URI . 'view/' . $torrent_id;
//Retrieve full description from torrent page //Retrieve full description from page contents
if ($item_html = getSimpleHTMLDOMCached($item_uri)) {
//Retrieve data from page contents
$item_title = str_replace(' :: Nyaa', '', $item_html->find('title', 0)->plaintext);
$item_desc = str_get_html( $item_desc = str_get_html(
markdownToHtml(html_entity_decode($item_html->find('#torrent-description', 0)->innertext)) markdownToHtml(html_entity_decode($item_html->find('#torrent-description', 0)->innertext))
); );
$item_author = extractFromDelimiters($item_html->outertext, 'href="/user/', '"');
$item_date = intval(extractFromDelimiters($item_html->outertext, 'data-timestamp="', '"'));
//Retrieve image for thumbnail or generic logo fallback //Retrieve image for thumbnail or generic logo fallback
$item_image = $this->getURI() . 'static/img/avatar/default.png'; $item_image = $this->getURI() . 'static/img/avatar/default.png';
@ -125,19 +97,11 @@ class NyaaTorrentsBridge extends BridgeAbstract {
} }
} }
//Build and add final item //Add expanded fields to the current item
$item = array();
$item['uri'] = $item_uri;
$item['title'] = $item_title;
$item['author'] = $item_author;
$item['timestamp'] = $item_date;
$item['enclosures'] = array($item_image); $item['enclosures'] = array($item_image);
$item['content'] = $item_desc; $item['content'] = $item_desc;
$this->items[] = $item; }
}
} return $item;
$element = null;
}
$results = null;
} }
} }