From e55a88fb8ea6133bdc23076f1af80726b48c245a Mon Sep 17 00:00:00 2001 From: Dag Date: Thu, 12 Oct 2023 20:32:17 +0200 Subject: [PATCH] refactor(nyaa) (#3742) --- bridges/NyaaTorrentsBridge.php | 76 +++++++++++----------------------- 1 file changed, 25 insertions(+), 51 deletions(-) diff --git a/bridges/NyaaTorrentsBridge.php b/bridges/NyaaTorrentsBridge.php index da3c34f5..3129bfc4 100644 --- a/bridges/NyaaTorrentsBridge.php +++ b/bridges/NyaaTorrentsBridge.php @@ -6,20 +6,6 @@ class NyaaTorrentsBridge extends FeedExpander const NAME = 'NyaaTorrents'; const URI = 'https://nyaa.si/'; const DESCRIPTION = 'Returns the newest torrents, with optional search criteria.'; - const MAX_ITEMS = 20; - const CUSTOM_FIELD_PREFIX = 'nyaa:'; - const CUSTOM_FIELDS = [ - self::CUSTOM_FIELD_PREFIX . 'seeders' => 'seeders', - self::CUSTOM_FIELD_PREFIX . 'leechers' => 'leechers', - self::CUSTOM_FIELD_PREFIX . 'downloads' => 'downloads', - self::CUSTOM_FIELD_PREFIX . 'infoHash' => 'infoHash', - self::CUSTOM_FIELD_PREFIX . 'categoryId' => 'categoryId', - self::CUSTOM_FIELD_PREFIX . 'category' => 'category', - self::CUSTOM_FIELD_PREFIX . 'size' => 'size', - self::CUSTOM_FIELD_PREFIX . 'comments' => 'comments', - self::CUSTOM_FIELD_PREFIX . 'trusted' => 'trusted', - self::CUSTOM_FIELD_PREFIX . 'remake' => 'remake' - ]; const PARAMETERS = [ [ 'f' => [ @@ -74,58 +60,31 @@ class NyaaTorrentsBridge extends FeedExpander ] ]; - public function getIcon() - { - return self::URI . 'static/favicon.png'; - } - - public function getURI() - { - return self::URI . '?page=rss&s=id&o=desc&' - . http_build_query([ - 'f' => $this->getInput('f'), - 'c' => $this->getInput('c'), - 'q' => $this->getInput('q'), - 'u' => $this->getInput('u') - ]); - } - public function collectData() { - $content = getContents($this->getURI()); - $content = $this->fixCustomFields($content); - $rssContent = simplexml_load_string(trim($content)); - $this->collectRss2($rssContent, self::MAX_ITEMS); - } - - private function fixCustomFields($content) - { - $broken = array_keys(self::CUSTOM_FIELDS); - $fixed = array_values(self::CUSTOM_FIELDS); - return str_replace($broken, $fixed, $content); + $this->collectExpandableDatas($this->getURI()); } protected function parseItem($newItem) { $item = parent::parseRss2Item($newItem); - - // Add nyaa custom fields $item['id'] = str_replace(['https://nyaa.si/download/', '.torrent'], '', $item['uri']); - foreach (array_values(self::CUSTOM_FIELDS) as $value) { - $item[$value] = (string) $newItem->$value; - } - //Convert URI from torrent file to web page + $nyaaFields = (array)($newItem->children('nyaa', true)); + $item = array_merge($item, $nyaaFields); + + // Convert URI from torrent file to web page $item['uri'] = str_replace('/download/', '/view/', $item['uri']); $item['uri'] = str_replace('.torrent', '', $item['uri']); - if ($item_html = getSimpleHTMLDOMCached($item['uri'])) { - //Retrieve full description from page contents + $item_html = getSimpleHTMLDOMCached($item['uri']); + if ($item_html) { + // Retrieve full description from page contents $item_desc = str_get_html( markdownToHtml(html_entity_decode($item_html->find('#torrent-description', 0)->innertext)) ); - //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'; foreach ($item_desc->find('img') as $img) { if (strpos($img->src, 'prez') === false) { @@ -134,11 +93,26 @@ class NyaaTorrentsBridge extends FeedExpander } } - //Add expanded fields to the current item $item['enclosures'] = [$item_image]; $item['content'] = $item_desc; } return $item; } + + public function getIcon() + { + return self::URI . 'static/favicon.png'; + } + + public function getURI() + { + $params = [ + 'f' => $this->getInput('f'), + 'c' => $this->getInput('c'), + 'q' => $this->getInput('q'), + 'u' => $this->getInput('u'), + ]; + return self::URI . '?page=rss&s=id&o=desc&' . http_build_query($params); + } }