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:
16
UrlRewriter and Search functionality for demonoid.
23
- category: <category>
25
- sub-category: <category>
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'])
49
#root = validator.factory()
51
#advanced = root.accept('dict')
52
#advanced.accept('url', key='url')
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')
63
def search(self, query, comparator, config):
67
#TODO: check if urls are ever passed and if is wanted behaviour.
68
#did: removed the url function & replaced it with category specification.
70
#TODO: check wether we should support quality selection for movies/tv
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)
80
#url = url + urllib.quote(name.encode('utf-8'))
81
page = urlopener(url, log)
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():
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)
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)
111
entry['content_size'] = int(float(size.group(1)) * 1000 / 1024 ** 2)
113
entries.append(entry)
116
dashindex = name.rfind('-')
118
return self.search_title(name[:dashindex], comparator=comparator, config=config)
120
raise PluginWarning('No close matches for %s' % name, log, log_once=True)
122
log.debug('search got %d results' % len(entries))
124
#TODO: check of this does anything/should even be here. indent seems strange.
126
return torrent_availability(a['torrent_seeds'], a['torrent_leeches'])
128
entries.sort(reverse=True, key=lambda x: x.get('search_sorted'))
131
register_plugin(UrlRewriteDemonoid, 'demonoid', groups=['urlrewriter', 'search'])