-
Notifications
You must be signed in to change notification settings - Fork 13
/
YifyProvider.php
86 lines (75 loc) · 1.8 KB
/
YifyProvider.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
<?php
namespace Axon\Search\Provider;
use Axon\Search\Model\Torrent;
use Axon\Search\Exception\UnexpectedResponseException;
/**
* Search YIFY torrents
*
* @author Ramon Kleiss <ramonkleiss@gmail.com>
*/
class YifyProvider extends AbstractProvider
{
/**
* @var string
*/
const DEFAULT_HOST = 'yts.re';
/**
* @var string
*/
const DEFAULT_PATH = '/api/list.json';
/**
* {@inheritDoc}
*/
public function getName()
{
return 'YIFY Torrents';
}
/**
* {@inheritDoc}
*/
public function getCanonicalName()
{
return 'yts';
}
/**
* Generate the url for a search query
*
* @param string $query
* @param integer|null $page
*/
public function getUrl($query, $page = null)
{
$url = sprintf(
'http://%s%s?keywords=%s',
self::DEFAULT_HOST,
self::DEFAULT_PATH,
rawurlencode($query)
);
if (is_integer($page)) {
$url .= sprintf('&set=%d', $page);
}
return $url;
}
/**
* @param string $rawResponse
*
* @return Torrent[]
*/
protected function transformResponse($rawResponse)
{
if (!($stdClass = json_decode($rawResponse))) {
throw new UnexpectedResponseException(
'Could not parse response'
);
}
return array_map(function ($result) {
$torrent = new Torrent();
$torrent->setName($result->MovieTitle);
$torrent->setHash($result->TorrentHash);
$torrent->setSize($result->SizeByte);
$torrent->setSeeds($result->TorrentSeeds);
$torrent->setPeers($result->TorrentPeers);
return $torrent;
}, $stdClass->MovieList);
}
}