From 00a24a98be20e1edf6807a2f40d76e0ba8403e12 Mon Sep 17 00:00:00 2001 From: ORelio Date: Mon, 19 Apr 2021 18:59:51 +0200 Subject: [PATCH] [NyaaTorrents] Rewrite as Feed Expander (#2073) NyaaTorrents allows search criteria as URL parameters in RSS feed so we just need to expand feed items --- bridges/NyaaTorrentsBridge.php | 102 +++++++++++---------------------- 1 file changed, 33 insertions(+), 69 deletions(-) diff --git a/bridges/NyaaTorrentsBridge.php b/bridges/NyaaTorrentsBridge.php index 997022aecff..1ce1a0276a2 100644 --- a/bridges/NyaaTorrentsBridge.php +++ b/bridges/NyaaTorrentsBridge.php @@ -1,5 +1,5 @@ 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( - '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; - } - - $torrent_id = str_replace('/view/', '', $element); - - //Ignore entries without valid torrent ID - if ($torrent_id != 0 && ctype_digit($torrent_id)) { + public function collectData(){ + $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'), + 'u' => $this->getInput('u') + )), 20); + } - //Retrieve data for this torrent ID - $item_uri = self::URI . 'view/' . $torrent_id; + protected function parseItem($newItem){ + $item = parent::parseItem($newItem); - //Retrieve full description from torrent page - if ($item_html = getSimpleHTMLDOMCached($item_uri)) { + //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 from page contents - $item_title = str_replace(' :: Nyaa', '', $item_html->find('title', 0)->plaintext); - $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="', '"')); + if ($item_html = getSimpleHTMLDOMCached($item['uri'])) { - //Retrieve image for thumbnail or generic logo fallback - $item_image = $this->getURI() . 'static/img/avatar/default.png'; - foreach ($item_desc->find('img') as $img) { - if (strpos($img->src, 'prez') === false) { - $item_image = $img->src; - break; - } - } + //Retrieve full description from page contents + $item_desc = str_get_html( + markdownToHtml(html_entity_decode($item_html->find('#torrent-description', 0)->innertext)) + ); - //Build and add final 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['content'] = $item_desc; - $this->items[] = $item; + //Retrieve image for thumbnail or generic logo fallback + $item_image = $this->getURI() . 'static/img/avatar/default.png'; + foreach ($item_desc->find('img') as $img) { + if (strpos($img->src, 'prez') === false) { + $item_image = $img->src; + break; } } - $element = null; + + //Add expanded fields to the current item + $item['enclosures'] = array($item_image); + $item['content'] = $item_desc; } - $results = null; + + return $item; } }