-
Notifications
You must be signed in to change notification settings - Fork 7
/
search.php
executable file
·141 lines (112 loc) · 3.36 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Synology search class
*/
class SynoDLMSearchJackett
{
public $debug = FALSE;
private $qurl = 'http://<host>/api/v2.0/indexers/all/results/torznab/api?apikey=<apikey>&t=search&cat=&q=<query>';
/**
* synology hook
*
* @param $curl
* @param $query
* @param $username
* @param $password
*/
public function prepare($curl, $query, $username = '', $password = '')
{
// Strip protocols from the setting.
$username = strtr($username, [
'http://' => '',
'https://' => '',
]);
$url = strtr($this->qurl, [
'<host>' => trim($username, ' /'),
'<apikey>' => urlencode(trim($password)),
'<query>' => urlencode(trim($query)),
]);
if ($this->debug) {
error_log("REQUEST: {$url}");
}
// Setup the $curl handler.
curl_setopt($curl, CURLOPT_URL, $url);
}
/**
* synology hook
*
* @param $plugin
* @param $response
*
* @return int
*/
public function parse($plugin, $response)
{
$count = 0;
$xml = simplexml_load_string(trim($response), 'SimpleXMLElement');
if (empty($xml->channel)) {
return $count;
}
foreach ($xml->channel->item as $child) {
$peers = $child->xpath('torznab:attr[@name="peers"]')
? (int)$child->xpath('torznab:attr[@name="peers"]')[0]['value']
: 0;
$seeders = $child->xpath('torznab:attr[@name="seeders"]')
? (int)$child->xpath('torznab:attr[@name="seeders"]')[0]['value']
: 0;
$categories = [];
foreach ($child->xpath('torznab:attr[@name="category"]') as $category) {
$categories[] = $category['value'];
}
$indexer = (string) $child->jackettindexer;
$leechs = $peers ? $peers - $seeders : 0;
$title = urldecode((string) $child->title);
$download = (string) $child->link;
$size = (double) $child->size;
$datetime = date('Y-m-d H:i:s', strtotime($child->pubDate));
$page = (string) $child->guid;
$hash = md5($count . $download);
// Add record for every category
// foreach ($categories as $category) {
// $plugin->addResult($title, $download, $size, $datetime, $page, $hash, $seeders, $leechs, $category);
// }
if ($indexer) {
$title .= ' (' . $indexer . ')';
}
$plugin->addResult($title, $download, $size, $datetime, $page, $hash, $seeders, $leechs, array_shift($categories));
$count++;
}
return $count;
}
/**
* synology hook
*
* @param $username
* @param $password
*
* @return bool
*/
public function VerifyAccount($username, $password)
{
// Make new curl object for the verify request.
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYHOST => FALSE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_TIMEOUT => 20,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP)',
]);
// Use the same prepare method.
$this->prepare($curl, '', $username, $password);
$result = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if (strpos($result, 'Invalid API Key') !== false) {
return false;
}
// Assume that if HTTP status code is 200, then we have connection to the Jackett
return (int) $httpcode == 200;
}
}