flexget.plugins.urlrewrite_torrentz
Covered: 20 lines
Missed: 38 lines
Skipped 21 lines
Percent: 34 %
 1
import logging
 2
import re
 3
import urllib
 4
import feedparser
 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))
27
        return entries
29
    def search_title(self, name, comparator=StringComparator()):
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)
36
        entries = []
38
        status = rss.get('status', False)
39
        if status != 200:
40
            raise PluginWarning('Search result not 200 (OK), received %s' % status)
42
        ex = rss.get('bozo_exception', False)
43
        if ex:
44
            raise PluginWarning('Got bozo_exception (bad feed)')
46
        for item in rss.entries:
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():
53
                continue
55
            m = re.search(r'Size: ([\d]+) Mb Seeds: ([,\d]+) Peers: ([,\d]+)', item.description, re.IGNORECASE)
56
            if not m:
57
                log.debug('regexp did not find seeds / peer data')
58
                continue
60
            entry = Entry()
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'])
68
            entries.append(entry)
71
        if not entries:
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'))
76
        return entries
78
register_plugin(UrlRewriteTorrentz, 'torrentz', groups=['urlrewriter', 'search'])