flexget.plugins.urlrewrite_demonoid
Covered: 45 lines
Missed: 53 lines
Skipped 34 lines
Percent: 45 %
  1
import re
  2
import urllib
  3
import logging
  4
from plugin_urlrewriting import UrlRewritingError
  5
from flexget.entry import Entry
  6
from flexget.plugin import register_plugin, internet, PluginWarning
  7
from flexget.utils.tools import urlopener
  8
from flexget.utils.soup import get_soup
  9
from flexget.utils.search import torrent_availability, StringComparator
 11
log = logging.getLogger('demonoid')
 14
class UrlRewriteDemonoid:
 15
    """
 16
    UrlRewriter and Search functionality for demonoid.
 18
    Will accept:
 19
      demonoid: <category>
 21
    Or:
 22
      demonoid:
 23
        - category: <category>
 24
        - quality: <quality>
 25
        - sub-category: <category>
 27
    Categories:
 29
    * all
 30
    * applications
 31
    * audio books
 32
    * tv
 33
    * games
 34
    * books
 35
    * comics
 36
    * anime
 37
    * misc
 38
    * movies
 39
    * music
 40
    * music videos
 41
    * pictures
 42
    """
 44
    def validator(self):
 45
        from flexget import validator
 46
        root = validator.factory('choice')
 47
        root.accept_choices(['all', 'applications', 'audio books', 'tv', 'games', 'books', 'comics', 'anime', 'misc',
 48
                             'movies', 'music', 'music videos', 'pictures'])
 53
        return root
 55
    def url_rewritable(self, feed, entry):
 56
        return entry['url'].startswith('http://www.demonoid.me/files/details/')
 58
    def url_rewrite(self, feed, entry):
 59
        entry['url'] = entry['url'].replace('details', 'download')
 62
    @internet(log)
 63
    def search(self, query, comparator, config):
 64
        """
 65
        """
 71
        comparator.set_seq1(query)
 72
        name = comparator.search_string()
 73
        optiondict = ({'all': 0, 'applications': 5, 'audio books': 17, 'books': 11, 'comics': 10, 'games': 4,
 74
                           'anime': 9, 'misc': 6, 'movies': 1, 'music': 2, 'music videos': 13, 'pictures': 8, 'tv': 3})
 76
        url = ('http://www.demonoid.me/files/?category=%s&subcategory=All&quality=All&seeded=0&external=2&to=1&uid=0&query=%s'
 77
              % (optiondict[config], urllib.quote(name.encode('utf-8'))))
 78
        log.debug('Using %s as demonoid search url' % url)
 81
        page = urlopener(url, log)
 82
        soup = get_soup(page)
 83
        entries = []
 84
        for td in soup.findAll('td', attrs={'colspan': '9'}):
 85
            link = td.findAll('a')[0]
 86
            found_title = link.contents[0]
 87
            comparator.set_seq2(found_title)
 88
            log.debug('name: %s' % comparator.a)
 89
            log.debug('found name: %s' % comparator.b)
 90
            log.debug('confidence: %s' % comparator.ratio())
 91
            if not comparator.matches():
 92
                continue
 94
            entry = Entry()
 95
            entry['title'] = found_title
 96
            entry['url'] = 'http://www.demonoid.me' + link.get('href')
 97
            tds = td.parent.nextSibling.nextSibling.findAll('td')
 98
            entry['torrent_seeds'] = int(tds[6].findNext('font').contents[0])
 99
            entry['torrent_leeches'] = int(tds[7].findNext('font').contents[0])
100
            entry['search_ratio'] = comparator.ratio()
101
            entry['search_sort'] = torrent_availability(entry['torrent_seeds'], entry['torrent_leeches'])
103
            size = tds[3].contents[0]
104
            size = re.search(r'([\.\d]+) ([GMK])B', size)
105
            if size:
106
                if size.group(2) == 'G':
107
                    entry['content_size'] = int(float(size.group(1)) * 1000 ** 3 / 1024 ** 2)
108
                elif size.group(2) == 'M':
109
                    entry['content_size'] = int(float(size.group(1)) * 1000 ** 2 / 1024 ** 2)
110
                else:
111
                    entry['content_size'] = int(float(size.group(1)) * 1000 / 1024 ** 2)
113
            entries.append(entry)
115
        if not entries:
116
            dashindex = name.rfind('-')
117
            if dashindex != -1:
118
                return self.search_title(name[:dashindex], comparator=comparator, config=config)
119
            else:
120
                raise PluginWarning('No close matches for %s' % name, log, log_once=True)
122
        log.debug('search got %d results' % len(entries))
125
        def score(a):
126
            return torrent_availability(a['torrent_seeds'], a['torrent_leeches'])
128
        entries.sort(reverse=True, key=lambda x: x.get('search_sorted'))
129
        return entries
131
register_plugin(UrlRewriteDemonoid, 'demonoid', groups=['urlrewriter', 'search'])