From eb799e59a6d53043d9fa0193dbcea3ec290da3a5 Mon Sep 17 00:00:00 2001 From: Jisagi Date: Sat, 10 Jun 2023 18:28:00 +0200 Subject: [PATCH] [NyaaTorrentsBridge] Add custom fields (#3420) * Update NyaaTorrentsBridge.php * lint * lint #2 * Sir Lint the Third * Add torrent id to custom fields * Proposed improvements --- bridges/NyaaTorrentsBridge.php | 48 ++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/bridges/NyaaTorrentsBridge.php b/bridges/NyaaTorrentsBridge.php index e281b79d..da3c34f5 100644 --- a/bridges/NyaaTorrentsBridge.php +++ b/bridges/NyaaTorrentsBridge.php @@ -2,10 +2,24 @@ class NyaaTorrentsBridge extends FeedExpander { - const MAINTAINER = 'ORelio'; + const MAINTAINER = 'ORelio & Jisagi'; 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' => [ @@ -65,23 +79,41 @@ class NyaaTorrentsBridge extends FeedExpander return self::URI . 'static/favicon.png'; } - public function collectData() + public function getURI() { - $this->collectExpandableDatas( - self::URI . '?page=rss&s=id&o=desc&' + 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') - ]), - 20 - ); + ]); + } + + 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); } protected function parseItem($newItem) { - $item = parent::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 $item['uri'] = str_replace('/download/', '/view/', $item['uri']);