4
from flexget.entry import Entry
5
from flexget.plugin import register_plugin, PluginWarning
7
log = logging.getLogger('nyaa')
9
# TODO: Other categories
10
CATEGORIES = {'all': '0_0',
12
FILTERS = ['all', 'filter remakes', 'trusted only', 'a+ only']
16
"""Nyaa urlrewriter and search plugin."""
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)
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)
42
status = rss.get('status', False)
44
raise PluginWarning('Search result not 200 (OK), received %s' % status)
46
ex = rss.get('bozo_exception', False)
48
raise PluginWarning('Got bozo_exception (bad feed)')
50
for item in rss.entries:
51
# Check if item passes comparator
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():
58
entry['title'] = item.title
59
entry['url'] = item.link
60
entry['search_ratio'] = comparator.ratio()
61
# TODO: parse some shit
62
#entry['torrent_seeds'] = int(item.seeds)
63
#entry['torrent_leeches'] = int(item.leechs)
64
#entry['search_sort'] = torrent_availability(entry['torrent_seeds'], entry['torrent_leeches'])
65
#entry['content_size'] = int(item.size) / 1024 / 1024
71
raise PluginWarning('No matches for %s' % name, log, log_once=True)
73
entries.sort(reverse=True, key=lambda x: x.get('search_sort'))
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'])