Skip to content

Commit

Permalink
Use app_id parameter and throttle API calls. Should take care of #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcartoixa committed Mar 28, 2018
1 parent 3dec9fd commit 51a4d9f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
10 changes: 9 additions & 1 deletion dlm/RarBGToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@ public function refresh($force = false)
{
$this->expiration = $now;
$this->token = NULL;

$curl = curl_init();
$url = SynoDLMSearchRarBG::API_URL.http_build_query(array(
'get_token' => 'get_token',
'app_id' => SynoDLMSearchRarBG::APP_ID
));
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => SynoDLMSearchRarBG::API_URL.'get_token=get_token',
CURLOPT_URL => $url,
CURLOPT_USERAGENT => RARBG_USER_AGENT,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($curl);

$this->expiration = time() + (14 * 60);
$this->token = json_decode($response)->token;

sleep(2); // API is rate limited
}
}

Expand Down
30 changes: 18 additions & 12 deletions dlm/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SynoDLMSearchRarBG
{
// cf. https://torrentapi.org/apidocs_v2.txt
const API_URL = 'https://torrentapi.org/pubapi_v2.php?';
const APP_ID = 'synology-dlm-rarbg';

public function __construct()
{
Expand All @@ -23,6 +24,7 @@ public function prepare($curl, $query)
'sort' => 'seeders',
'ranked' => 0,
'format' => 'json_extended',
'app_id' => SynoDLMSearchRarBG::APP_ID,
'token' => strval($this->token)
));
curl_setopt_array($curl, array(
Expand All @@ -35,21 +37,25 @@ public function prepare($curl, $query)
public function parse($plugin, $response)
{
$results = json_decode($response);
foreach ($results->torrent_results as $result)
var_dump($results);
if (json_last_error() === JSON_ERROR_NONE)
{
$title = $result->title;
$download = $result->download;
$size = floatval($result->size);
$datetime = substr($result->pubdate, 0, 19);
$page = $result->info_page;
$hash = '';
$seeds = $result->seeders;
$leechs = $result->leechers;
$category = $result->category;
foreach ($results->torrent_results as $result)
{
$title = $result->title;
$download = $result->download;
$size = floatval($result->size);
$datetime = substr($result->pubdate, 0, 19);
$page = $result->info_page;
$hash = '';
$seeds = $result->seeders;
$leechs = $result->leechers;
$category = $result->category;

$plugin->addResult($title, $download, $size, $datetime, $page, $hash, $seeds, $leechs, $category);
$plugin->addResult($title, $download, $size, $datetime, $page, $hash, $seeds, $leechs, $category);
}
return count($results->torrent_results);
}
return count($results->torrent_results);
}

private $token;
Expand Down
6 changes: 5 additions & 1 deletion dlm/tests/querier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
$plugin = new FakePlugin;
$search = new SynoDLMSearchRarBG;

$fhandle = fopen(dirname(__FILE__).'/../../tmp/curl.log', 'w');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_VERBOSE => 1,
CURLOPT_STDERR => $fhandle
));
$search->prepare($curl, 'test');
$response = curl_exec($curl);
Expand All @@ -25,5 +28,6 @@
}
}
curl_close($curl);
fclose($fhandle);

?>

0 comments on commit 51a4d9f

Please sign in to comment.