-
Notifications
You must be signed in to change notification settings - Fork 13
/
EztvProvider.php
137 lines (119 loc) · 3.11 KB
/
EztvProvider.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
<?php
namespace Axon\Search\Provider;
use Buzz\Browser;
use Nomnom\Nomnom;
use Symfony\Component\DomCrawler\Crawler;
use Axon\Search\Model\Torrent;
use Axon\Search\Exception\ConnectionException;
use Axon\Search\Exception\UnexpectedResponseException;
/**
* @author Ramon Kleiss <ramonkleiss@gmail.com>
*/
class EztvProvider implements ProviderInterface
{
/**
* @var string
*/
const DEFAULT_HOST = 'eztv.it';
/**
* @var string
*/
const DEFAULT_PATH = '/search';
/**
* @var Browser
*/
protected $browser;
/**
* Constructor
*
* @param Browser $browser
*/
public function __construct(Browser $browser = null)
{
$this->browser = $browser ?: new Browser();
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'EZTV';
}
/**
* {@inheritDoc}
*/
public function getCanonicalName()
{
return 'eztv';
}
/**
* {@inheritDoc}
*/
public function search($query, $page = null)
{
try {
$response = $this->browser->post(
$this->getUrl(),
array(),
$this->getQuery($query)
);
} catch (\Exception $e) {
throw new ConnectionException(sprintf(
'Could not connect to "%s"',
$this->getUrl()
), 0, $e);
}
if ($response->getStatusCode() != 200) {
throw new UnexpectedResponseException(sprintf(
'Unexpected response: %s (%d)',
$response->getReasonPhrase(),
$response->getStatusCode()
));
}
return $this->transformResponse($response->getContent());
}
/**
* @return string
*/
public function getUrl()
{
return sprintf('http://%s%s/', self::DEFAULT_HOST, self::DEFAULT_PATH);
}
/**
* @param string $html
*
* @return Torrent[]
*/
protected function transformResponse($html)
{
$crawler = new Crawler($html);
return $crawler->filter('tr.forum_header_border')->each(function ($node) {
$magnet = $node->filter('a.magnet')->first()->attr('href');
preg_match('/btih:([0-9A-Za-z]+)&/', $magnet, $matches);
$hash = $matches[1];
$size = $node->filter('a.epinfo')->attr('title');
preg_match('/\(([0-9\.]+) ([A-Za-z]+)\)/', $size, $matches);
$size = $matches[1];
$unit = $matches[2];
$converter = new Nomnom($size);
$torrent = new Torrent();
$torrent->setName($node->filter('td.forum_thread_post')->eq(1)->text());
$torrent->setHash($hash);
$torrent->setSize($converter->from($unit)->to('B'));
return $torrent;
});
}
/**
* @param string $query
*
* @return string
*/
protected function getQuery($query)
{
return http_build_query(array(
'SearchString' => $query,
'SearchString1' => null,
'search' => 'Search'
));
}
}