flexget.plugins.search_kat
Covered: 25 lines
Missed: 38 lines
Skipped 20 lines
Percent: 39 %
 1
import logging
 2
import urllib
 3
import feedparser
 4
from flexget.entry import Entry
 5
from flexget.utils.search import torrent_availability
 6
from flexget.plugin import PluginWarning, register_plugin
 8
log = logging.getLogger('kat')
11
class SearchKAT(object):
12
    """KAT search plugin.
14
    should accept:
15
    kat: <category>
17
    categories:
18
      all
19
      movies
20
      tv
21
      music
22
      books
23
      xxx
24
      other
25
    """
27
    def validator(self):
28
        from flexget import validator
30
        root = validator.factory('choice')
31
        root.accept_choices(['all', 'movies', 'tv', 'music', 'books', 'xxx', 'other'])
32
        return root
34
    def search(self, query, comparator, config):
35
        comparator.set_seq1(query)
36
        name = comparator.search_string().lower()
37
        url = 'http://www.kat.ph/search/%s/?rss=1' % urllib.quote(name.encode('utf-8'))
38
        if config != 'all':
39
            url += '&category=%s' % config
41
        log.debug('requesting: %s' % url)
42
        rss = feedparser.parse(url)
43
        entries = []
45
        status = rss.get('status', False)
46
        if status != 200:
47
            raise PluginWarning('Search result not 200 (OK), received %s' % status)
49
        ex = rss.get('bozo_exception', False)
50
        if ex:
51
            raise PluginWarning('Got bozo_exception (bad feed)')
53
        for item in rss.entries:
55
            comparator.set_seq2(item.title)
56
            log.debug('name: %s, found name: %s, confidence: %s' % (comparator.a, comparator.b, comparator.ratio()))
57
            if not comparator.matches():
58
                continue
60
            entry = Entry()
61
            entry['title'] = item.title
62
            if item.torrentlink.startswith('//'):
63
                entry['url'] = 'http:' + item.torrentlink
64
            else:
65
                entry['url'] = item.torrentlink
66
            entry['search_ratio'] = comparator.ratio()
67
            entry['torrent_seeds'] = int(item.seeds)
68
            entry['torrent_leeches'] = int(item.leechs)
69
            entry['search_sort'] = torrent_availability(entry['torrent_seeds'], entry['torrent_leeches'])
70
            entry['content_size'] = int(item.size) / 1024 / 1024
71
            entry['torrent_info_hash'] = item.hash
73
            entries.append(entry)
76
        if not entries:
77
            raise PluginWarning('No matches for %s' % name, log, log_once=True)
79
        entries.sort(reverse=True, key=lambda x: x.get('search_sort'))
80
        return entries
82
register_plugin(SearchKAT, 'kat', groups=['search'])