[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
1 changed files with 33 additions and 69 deletions

View File

@ -1,5 +1,5 @@
<?php
class NyaaTorrentsBridge extends BridgeAbstract {
class NyaaTorrentsBridge extends FeedExpander {
const MAINTAINER = 'ORelio';
const NAME = 'NyaaTorrents';
@ -64,57 +64,29 @@ class NyaaTorrentsBridge extends BridgeAbstract {
}
public function collectData(){
// Determine base URL, either home page or user page
$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&'
$this->collectExpandableDatas(
self::URI . '?page=rss&s=id&o=desc&'
. http_build_query(array(
'f' => $this->getInput('f'),
'c' => $this->getInput('c'),
'q' => $this->getInput('q')
));
// 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;
'q' => $this->getInput('q'),
'u' => $this->getInput('u')
)), 20);
}
$torrent_id = str_replace('/view/', '', $element);
protected function parseItem($newItem){
$item = parent::parseItem($newItem);
//Ignore entries without valid torrent ID
if ($torrent_id != 0 && ctype_digit($torrent_id)) {
//Convert URI from torrent file to web page
$item['uri'] = str_replace('/download/', '/view/', $item['uri']);
$item['uri'] = str_replace('.torrent', '', $item['uri']);
//Retrieve data for this torrent ID
$item_uri = self::URI . 'view/' . $torrent_id;
if ($item_html = getSimpleHTMLDOMCached($item['uri'])) {
//Retrieve full description from torrent page
if ($item_html = getSimpleHTMLDOMCached($item_uri)) {
//Retrieve data from page contents
$item_title = str_replace(' :: Nyaa', '', $item_html->find('title', 0)->plaintext);
//Retrieve full description from page contents
$item_desc = str_get_html(
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
$item_image = $this->getURI() . 'static/img/avatar/default.png';
@ -125,19 +97,11 @@ class NyaaTorrentsBridge extends BridgeAbstract {
}
}
//Build and add final item
$item = array();
$item['uri'] = $item_uri;
$item['title'] = $item_title;
$item['author'] = $item_author;
$item['timestamp'] = $item_date;
//Add expanded fields to the current item
$item['enclosures'] = array($item_image);
$item['content'] = $item_desc;
$this->items[] = $item;
}
}
$element = null;
}
$results = null;
}
return $item;
}
}