flexget.plugins.urlrewrite_nyaa
Covered: 18 lines
Missed: 38 lines
Skipped 27 lines
Percent: 32 %
 1
import logging
 2
import urllib
 3
import feedparser
 4
from flexget.entry import Entry
 5
from flexget.plugin import register_plugin, PluginWarning
 7
log = logging.getLogger('nyaa')
10
CATEGORIES = {'all': '0_0',
11
              'anime': '1_0'}
12
FILTERS = ['all', 'filter remakes', 'trusted only', 'a+ only']
15
class UrlRewriteNyaa:
16
    """Nyaa urlrewriter and search plugin."""
18
    def validator(self):
19
        from flexget import validator
21
        root = validator.factory()
22
        root.accept('choice').accept_choices(CATEGORIES)
23
        advanced = root.accept('dict')
24
        advanced.accept('choice', key='category').accept_choices(CATEGORIES)
25
        advanced.accept('choice', key='filter').accept_choices(FILTERS)
26
        return root
28
    def search(self, query, comparator, config):
29
        if not isinstance(config, dict):
30
            config = {'category': config}
31
        config.setdefault('category', 'anime')
32
        config.setdefault('filter', 'all')
33
        comparator.set_seq1(query)
34
        name = comparator.search_string()
35
        url = 'http://www.nyaa.eu/?page=rss&cats=%s&filter=%s&term=%s' % (
36
              CATEGORIES[config['category']], FILTERS.index(config['filter']), urllib.quote(name.encode('utf-8')))
38
        log.debug('requesting: %s' % url)
39
        rss = feedparser.parse(url)
40
        entries = []
42
        status = rss.get('status', False)
43
        if status != 200:
44
            raise PluginWarning('Search result not 200 (OK), received %s' % status)
46
        ex = rss.get('bozo_exception', False)
47
        if ex:
48
            raise PluginWarning('Got bozo_exception (bad feed)')
50
        for item in rss.entries:
52
            comparator.set_seq2(item.title)
53
            log.debug('name: %s, found name: %s, confidence: %s' % (comparator.a, comparator.b, comparator.ratio()))
54
            if not comparator.matches():
55
                continue
57
            entry = Entry()
58
            entry['title'] = item.title
59
            entry['url'] = item.link
60
            entry['search_ratio'] = comparator.ratio()
67
            entries.append(entry)
70
        if not entries:
71
            raise PluginWarning('No matches for %s' % name, log, log_once=True)
73
        entries.sort(reverse=True, key=lambda x: x.get('search_sort'))
74
        return entries
76
    def url_rewritable(self, feed, entry):
77
        return entry['url'].startswith('http://www.nyaa.eu/?page=torrentinfo&tid=')
79
    def url_rewrite(self, feed, entry):
80
        entry['url'] = entry['url'].replace('torrentinfo', 'download')
82
register_plugin(UrlRewriteNyaa, 'nyaa', groups=['search', 'urlrewriter'])