5
from flexget.plugin import register_plugin, PluginWarning
6
from flexget.entry import Entry
7
from flexget.utils.search import torrent_availability, StringComparator
9
log = logging.getLogger('torrentz')
11
REGEXP = re.compile(r'http://torrentz\.eu/(?P<hash>[a-f0-9]{40})')
14
class UrlRewriteTorrentz(object):
15
"""Torrentz urlrewriter."""
17
def url_rewritable(self, feed, entry):
18
return REGEXP.match(entry['url'])
20
def url_rewrite(self, feed, entry):
21
hash = REGEXP.match(entry['url']).group(1)
22
entry['url'] = 'http://zoink.it/torrent/%s.torrent' % hash.upper()
24
def search(self, query, comparator, config=None):
25
entries = self.search_title(query, comparator)
26
log.debug('Search got %d results' % len(entries))
29
def search_title(self, name, comparator=StringComparator()):
30
# urllib.quote will crash if the unicode string has non ascii characters, so encode in utf-8 beforehand
31
comparator.set_seq1(name)
32
name = comparator.search_string()
33
url = 'http://torrentz.eu/feed?q=%s' % urllib.quote(name.encode('utf-8'))
34
log.debug('requesting: %s' % url)
35
rss = feedparser.parse(url)
38
status = rss.get('status', False)
40
raise PluginWarning('Search result not 200 (OK), received %s' % status)
42
ex = rss.get('bozo_exception', False)
44
raise PluginWarning('Got bozo_exception (bad feed)')
46
for item in rss.entries:
47
# assign confidence score of how close this link is to the name you're looking for. .6 and above is "close"
48
comparator.set_seq2(item.title)
49
log.debug('name: %s' % comparator.a)
50
log.debug('found name: %s' % comparator.b)
51
log.debug('confidence: %s' % comparator.ratio())
52
if not comparator.matches():
55
m = re.search(r'Size: ([\d]+) Mb Seeds: ([,\d]+) Peers: ([,\d]+)', item.description, re.IGNORECASE)
57
log.debug('regexp did not find seeds / peer data')
61
entry['title'] = item.title
62
entry['url'] = item.link
63
entry['content_size'] = int(m.group(1))
64
entry['torrent_seeds'] = int(m.group(2).replace(',', ''))
65
entry['torrent_leeches'] = int(m.group(3).replace(',', ''))
66
entry['search_ratio'] = comparator.ratio()
67
entry['search_sort'] = torrent_availability(entry['torrent_seeds'], entry['torrent_leeches'])
72
raise PluginWarning('No close matches for %s' % name, log, log_once=True)
74
entries.sort(reverse=True, key=lambda x: x.get('search_sort'))
78
register_plugin(UrlRewriteTorrentz, 'torrentz', groups=['urlrewriter', 'search'])