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('piratebay')
14
class UrlRewritePirateBay(object):
15
"""PirateBay urlrewriter."""
18
def url_rewritable(self, feed, entry):
20
if url.endswith('.torrent'):
22
if url.startswith('http://thepiratebay.org/'):
24
if url.startswith('http://torrents.thepiratebay.org/'):
29
def url_rewrite(self, feed, entry):
30
if not 'url' in entry:
31
log.error("Didn't actually get a URL...")
33
log.debug("Got the URL: %s" % entry['url'])
34
if entry['url'].startswith('http://thepiratebay.org/search/'):
37
entry['url'] = self.search_title(entry['title'])[0]['url']
38
except PluginWarning, e:
39
raise UrlRewritingError(e)
42
entry['url'] = self.parse_download_page(entry['url'])
45
def parse_download_page(self, url):
46
page = urlopener(url, log)
49
tag_div = soup.find('div', attrs={'class': 'download'})
51
raise UrlRewritingError('Unable to locate download link from url %s' % url)
52
tag_a = tag_div.find('a')
53
torrent_url = tag_a.get('href')
56
raise UrlRewritingError(e)
59
def search(self, query, comparator, config=None):
60
entries = self.search_title(query, comparator)
61
log.debug('search got %d results' % len(entries))
65
def search_title(self, name, comparator=StringComparator(), url=None):
67
Search for name from piratebay.
68
If optional search :url: is passed it will be used instead of internal search.
71
comparator.set_seq1(name)
72
name = comparator.search_string()
74
# urllib.quote will crash if the unicode string has non ascii characters, so encode in utf-8 beforehand
75
url = 'http://thepiratebay.org/search/' + urllib.quote(name.encode('utf-8')) + '/0/7/0'
76
log.debug('Using %s as piratebay search url' % url)
77
page = urlopener(url, log)
80
for link in soup.findAll('a', attrs={'class': 'detLink'}):
81
comparator.set_seq2(link.contents[0])
82
log.debug('name: %s' % comparator.a)
83
log.debug('found name: %s' % comparator.b)
84
log.debug('confidence: %s' % comparator.ratio())
85
if not comparator.matches():
88
entry['title'] = link.contents[0]
89
entry['url'] = 'http://thepiratebay.org' + link.get('href')
90
tds = link.parent.parent.parent.findAll('td')
91
entry['torrent_seeds'] = int(tds[-2].contents[0])
92
entry['torrent_leeches'] = int(tds[-1].contents[0])
93
entry['search_ratio'] = comparator.ratio()
94
entry['search_sort'] = torrent_availability(entry['torrent_seeds'], entry['torrent_leeches'])
96
size = link.findNext(attrs={'class': 'detDesc'}).contents[0]
97
size = re.search('Size ([\.\d]+)\xa0([GMK])iB', size)
99
if size.group(2) == 'G':
100
entry['content_size'] = int(float(size.group(1)) * 1000 ** 3 / 1024 ** 2)
101
elif size.group(2) == 'M':
102
entry['content_size'] = int(float(size.group(1)) * 1000 ** 2 / 1024 ** 2)
104
entry['content_size'] = int(float(size.group(1)) * 1000 / 1024 ** 2)
105
entries.append(entry)
108
dashindex = name.rfind('-')
110
return self.search_title(name[:dashindex], comparator=comparator)
112
raise PluginWarning('No close matches for %s' % name, log, log_once=True)
114
entries.sort(reverse=True, key=lambda x: x.get('search_sort'))
118
register_plugin(UrlRewritePirateBay, 'piratebay', groups=['urlrewriter', 'search'])