forked from HazukiShiro/qBittorrent-Search-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrentz.py
126 lines (101 loc) · 3.38 KB
/
torrentz.py
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
#VERSION: 1.2
#AUTHORS: Shiro Hazuki (hazuki.shiro@gmail.com), cdgg (cdgg.cdgg@gmail.com)
#
# GNU GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# <http://www.gnu.org/licenses/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
'TorrentZ plug-in for qBittorrent'
import re
import string
import urllib2
class torrentz(object):
url = 'http://torrentz.eu'
name = 'Torrentz'
supported_categories = {'all': ''}
out = [-1]*7
(LINK,NAME,SIZE,SEEDS,LEECH,ENGINE_URL,DESCRIPTION) = range(0,7)
MULTIPLIERS = {
'b' : 1,
'B' : 1,
'Kb' : 1024,
'Mb' : 1024**2,
'Gb' : 1024**3,
}
def reset(self):
self.out = [-1]*7
self.out[self.ENGINE_URL] = self.url
def put(self):
print '|'.join([str(x) for x in self.out])
self.reset()
def __init__(self):
self.reset()
def search(self, what, cat='all'):
nextpage = 'search?f=' + what
while nextpage != False:
u = urllib2.urlopen('http://torrentz.eu/' + nextpage)
content = ''
for line in u:
content += line.strip('\n')
u.close()
m = re.search('<div class="results">(.+)</div><div class="help">', content)
content = m.group(1)
l = string.split(content, "<dl>")[1:]
temp = string.split(l[-1], "</dl>")
l[-1] = temp[0]
p = temp[1]
# Searches for another page
nextpage = False
m = re.search("<a href=\"/([a-zA-Z0-9\%\&\;\+\?\=]+)\">Next \»\;</a>", p)
if m!=None:
nextpage = m.group(1).replace("&", "&")
# Name, link and description link
pattern1 = '<dt.*><a href="/([a-zA-Z0-9]+)">(.+)</a>'
# Seed, leech and size
pattern2 = '<span class="s">(.+)</span> <span class="u">(.+)</span> <span class="d">(.+)</span></dd>'
# Valid links
link = [
'http://www.torrenthound.com/torrent/{0}',
'http://h33t.com/download.php?id={0}',
'http://torrage.com/torrent/{0}.torrent',
'http://torcache.com/torrent/{0}.torrent',
'http://zoink.it/torrent/{0}.torrent',
]
m = None
for line in l:
m = re.match(pattern1, line)
self.out[self.DESCRIPTION] = '{0}/{1}'.format(self.out[self.ENGINE_URL], m.group(1))
# Test whether the link is availabe or not
available = False
for current_link in link:
self.out[self.LINK] = current_link.format(m.group(1))
try:
urllib2.urlopen(self.out[self.LINK])
available = True
break
except:
pass
if not available:
continue
self.out[self.NAME] = re.sub('<[bB]>|</[bB]>', '', m.group(2))
m = re.search(pattern2, line)
self.out[self.SEEDS] = m.group(2).replace(',', '')
self.out[self.LEECH] = m.group(3).replace(',', '')
# Torrent size
n = re.search("([0-9,.]+) (.+)", m.group(1))
try:
self.out[self.SIZE] = int(float(n.group(1)) * self.MULTIPLIERS[n.group(2)])
self.put()
except:
self.reset()