Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NyaaTorrents] Rewrite as Feed Expander (#2072) #2073

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 33 additions & 69 deletions bridges/NyaaTorrentsBridge.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
class NyaaTorrentsBridge extends BridgeAbstract {
class NyaaTorrentsBridge extends FeedExpander {

const MAINTAINER = 'ORelio';
const NAME = 'NyaaTorrents';
Expand Down Expand Up @@ -63,81 +63,45 @@ public function getIcon() {
return self::URI . 'static/favicon.png';
}

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&'
. 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;
}
}