diff --git a/modules/piratebay/backend.py b/modules/piratebay/backend.py index 19e734c5..f00aa3a9 100644 --- a/modules/piratebay/backend.py +++ b/modules/piratebay/backend.py @@ -18,7 +18,8 @@ # along with weboob. If not, see . from weboob.capabilities.torrent import ICapTorrent, MagnetOnly, Torrent -from weboob.tools.backend import BaseBackend +from weboob.tools.backend import BaseBackend, BackendConfig +from weboob.tools.value import Value from weboob.capabilities.base import NotAvailable from .browser import PiratebayBrowser @@ -35,9 +36,10 @@ class PiratebayBackend(BaseBackend, ICapTorrent): DESCRIPTION = 'The Pirate Bay BitTorrent tracker' LICENSE = 'AGPLv3+' BROWSER = PiratebayBrowser + CONFIG = BackendConfig(Value('proxybay', label='Use a Proxy Bay', regexp=r'https?://.*', default='', required=False)) def create_default_browser(self): - return self.create_browser() + return self.create_browser(self.config['proxybay'].get()) def get_torrent(self, id): return self.browser.get_torrent(id) @@ -63,6 +65,4 @@ class PiratebayBackend(BaseBackend, ICapTorrent): torrent.url = tor.url return torrent - OBJECTS = { - Torrent:fill_torrent - } + OBJECTS = {Torrent: fill_torrent} diff --git a/modules/piratebay/browser.py b/modules/piratebay/browser.py index c7867178..46062898 100644 --- a/modules/piratebay/browser.py +++ b/modules/piratebay/browser.py @@ -18,6 +18,7 @@ # along with weboob. If not, see . +from urlparse import urlsplit import urllib from weboob.tools.browser import BaseBrowser, BrowserHTTPNotFound @@ -30,25 +31,34 @@ __all__ = ['PiratebayBrowser'] class PiratebayBrowser(BaseBrowser): - DOMAIN = 'thepiratebay.se' - PROTOCOL = 'https' ENCODING = 'utf-8' - USER_AGENT = BaseBrowser.USER_AGENTS['wget'] - PAGES = {'https://thepiratebay.se/': IndexPage, - 'https://thepiratebay.se/search/.*/0/7/0': TorrentsPage, - 'https://thepiratebay.se/torrent/.*': TorrentPage - } + + def __init__(self, url, *args, **kwargs): + url = url or 'https://thepiratebay.sx/' + url_parsed = urlsplit(url) + self.PROTOCOL = url_parsed.scheme + self.DOMAIN = url_parsed.netloc + self.PAGES = { + '%s://%s/' % (self.PROTOCOL, self.DOMAIN): IndexPage, + '%s://%s/search/.*/0/7/0' % (self.PROTOCOL, self.DOMAIN): TorrentsPage, + '%s://%s/torrent/.*' % (self.PROTOCOL, self.DOMAIN): TorrentPage + } + BaseBrowser.__init__(self, *args, **kwargs) def iter_torrents(self, pattern): - self.location('https://thepiratebay.se/search/%s/0/7/0' % urllib.quote_plus(pattern.encode('utf-8'))) + self.location('%s://%s/search/%s/0/7/0' % (self.PROTOCOL, + self.DOMAIN, + urllib.quote_plus(pattern.encode('utf-8')))) assert self.is_on_page(TorrentsPage) return self.page.iter_torrents() - def get_torrent(self, id): + def get_torrent(self, _id): try: - self.location('https://thepiratebay.se/torrent/%s/' % id) + self.location('%s://%s/torrent/%s/' % (self.PROTOCOL, + self.DOMAIN, + _id)) except BrowserHTTPNotFound: return if self.is_on_page(TorrentPage): - return self.page.get_torrent(id) + return self.page.get_torrent(_id)