refactor(nyaa) (#3742)

This commit is contained in:
Dag 2023-10-12 20:32:17 +02:00 committed by GitHub
parent 6a72c56cdd
commit e55a88fb8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 51 deletions

View File

@ -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,52 +60,25 @@ 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;
}
$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'])) {
$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))
@ -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);
}
}