diff --git a/couchpotato/core/downloaders/transmission/main.py b/couchpotato/core/downloaders/transmission/main.py index 3864e1d6..2ab24d14 100644 --- a/couchpotato/core/downloaders/transmission/main.py +++ b/couchpotato/core/downloaders/transmission/main.py @@ -5,6 +5,7 @@ from base64 import b64encode from couchpotato.core.downloaders.base import Downloader from couchpotato.core.helpers.encoding import isInt from couchpotato.core.logger import CPLog + import urllib2 import httplib import json diff --git a/couchpotato/core/providers/torrent/thepiratebay/__init__.py b/couchpotato/core/providers/torrent/thepiratebay/__init__.py index 9c132dd7..d8e05600 100644 --- a/couchpotato/core/providers/torrent/thepiratebay/__init__.py +++ b/couchpotato/core/providers/torrent/thepiratebay/__init__.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- from main import ThePirateBay +from main import TPBProxy def start(): @@ -18,21 +19,8 @@ config = [{'name': 'ThePirateBay', 'groups': [{ 'name': 'domain_for_tpb', 'label': 'Proxy server', 'default': 'http://thepiratebay.se', - 'description': 'Which domain do you preferr(or it\'s not blocked)', + 'description': 'Default domain for requests', 'type': 'dropdown', - 'values': [ - ('(Sweden) thepiratebay.se', 'http://thepiratebay.se'), - ('(Sweden) tpb.ipredator.se (ssl)', - 'https://tpb.ipredator.se'), - ('(Germany) depiraatbaai.be', 'http://depiraatbaai.be'), - ('(UK) piratereverse.info (ssl)', - 'https://piratereverse.info'), - ('(UK) tpb.pirateparty.org.uk (ssl)', - 'https://tpb.pirateparty.org.uk'), - ('(Netherlands) thepiratebay.se.coevoet.nl', - 'http://thepiratebay.se.coevoet.nl'), - ('(direct) 194.71.107.80', 'http://194.71.107.80'), - ('(direct) 194.71.107.83', 'http://194.71.107.81'), - ], + 'values': TPBProxy.list, }], }]}] diff --git a/couchpotato/core/providers/torrent/thepiratebay/main.py b/couchpotato/core/providers/torrent/thepiratebay/main.py index a08f581e..6dd779f3 100644 --- a/couchpotato/core/providers/torrent/thepiratebay/main.py +++ b/couchpotato/core/providers/torrent/thepiratebay/main.py @@ -6,8 +6,55 @@ from couchpotato.core.event import fireEvent from couchpotato.core.helpers.variable import getTitle from couchpotato.core.logger import CPLog from couchpotato.core.providers.torrent.base import TorrentProvider +from random import sample as random +from urlparse import urlparse import re +import time + + +class TPBProxy(object): + + """ TPBProxy deals with failed or blocked TPB proxys + it works as Round-robin balancer if user seleced + or default domain becomes unavaliable. + """ + + list = [ + ('(Sweden) thepiratebay.se', 'http://thepiratebay.se'), + ('(Sweden) tpb.ipredator.se (ssl)', 'https://tpb.ipredator.se' + ), + ('(Germany) depiraatbaai.be', 'http://depiraatbaai.be'), + ('(UK) piratereverse.info (ssl)', 'https://piratereverse.info' + ), + ('(UK) tpb.pirateparty.org.uk (ssl)', + 'https://tpb.pirateparty.org.uk'), + ('(Netherlands) argumentomteemigreren.nl', + 'http://argumentomteemigreren.nl'), + ('(direct) 194.71.107.80', 'http://194.71.107.80'), + ('(direct) 194.71.107.81', 'http://194.71.107.81'), + ('(direct) 194.71.107.82', 'http://194.71.107.82'), + ('(direct) 194.71.107.83', 'http://194.71.107.83'), + ] + + @staticmethod + def get_proxy(http_failed_disabled=None, current=None): + + # compare lists and user/default value, exclude filter + + unused = [item for item in TPBProxy.list if item + not in http_failed_disabled and current not in item] + + if len(unused) > 0: + + # only return uri + + return random(unused, 1)[0][1] + else: + + # this should disable provider for some time + + raise Exception('All ThePirateBay proxies are exhausted') class ThePirateBay(TorrentProvider): @@ -27,13 +74,30 @@ class ThePirateBay(TorrentProvider): def __init__(self): super(ThePirateBay, self).__init__() - self.urls = {'test': self.getapiurl(), + self.urls = {'test': self.api_domain(), 'detail': '%s/torrent/%s', 'search': '%s/search/%s/0/7/%d'} - def getapiurl(self, url=''): - return ('http://thepiratebay.se', self.conf('domain_for_tpb' - ))[self.conf('domain_for_tpb') != None] + url + def api_domain(self, url=''): + + # default domain + + domain = ('http://thepiratebay.se', self.conf('domain_for_tpb' + ))[self.conf('domain_for_tpb') != None] + + host = urlparse(domain).hostname + + # Clear disabled list for default or user selected host if time expired + + if self.http_failed_disabled.get(host, 0) > 0: + if self.http_failed_disabled[host] > time.time() - 900: + # get new random domain + domain = TPBProxy.get_proxy(self.http_failed_disabled, domain) + else: + del self.http_failed_request[host] + del self.http_failed_disabled[host] + + return domain + url def search(self, movie, quality): @@ -43,7 +107,7 @@ class ThePirateBay(TorrentProvider): cache_key = 'thepiratebay.%s.%s' % (movie['library' ]['identifier'], quality.get('identifier')) - search_url = self.urls['search'] % (self.getapiurl(), + search_url = self.urls['search'] % (self.api_domain(), self.for_search(getTitle(movie['library']) + ' ' + quality['identifier']), self.getCatId(quality['identifier'])[0]) @@ -86,8 +150,9 @@ class ThePirateBay(TorrentProvider): alt=re.compile('Helpers')) != None] moderated = (0, 50)[result.find('img', alt=re.compile('Moderator')) != None] - is_imdb = self.imdb_match(self.getapiurl(link['href' - ]), movie['library']['identifier']) + is_imdb = \ + self.imdb_match(self.api_domain(link['href']), + movie['library']['identifier']) self.log.info('Name: %s', link.string) self.log.info('Seeders: %s', result.find_all('td' @@ -102,7 +167,7 @@ class ThePirateBay(TorrentProvider): new['name'] = link.string new['id'] = re.search('/(?P\d+)/', link['href' ]).group('id') - new['url'] = self.getapiurl(link['href']) + new['url'] = self.api_domain(link['href']) new['magnet'] = download['href'] new['size'] = self.parseSize(size) new['seeders'] = int(result.find_all('td'