diff --git a/couchpotato/core/providers/base.py b/couchpotato/core/providers/base.py index e3709aa9..47db3c89 100644 --- a/couchpotato/core/providers/base.py +++ b/couchpotato/core/providers/base.py @@ -86,6 +86,7 @@ class YarrProvider(Provider): sizeKb = ['kb', 'kib'] login_opener = None + last_login_check = 0 def __init__(self): addEvent('provider.enabled_types', self.getEnabledProviderType) @@ -101,6 +102,22 @@ class YarrProvider(Provider): def login(self): + # Check if we are still logged in every hour + now = time.time() + if self.login_opener and self.last_login_check < (now - 3600): + try: + output = self.urlopen(self.urls['login_check']) + if self.loginCheckSuccess(output): + self.last_login_check = now + return True + else: + self.login_opener = None + except: + self.login_opener = None + + if self.login_opener: + return True + try: cookiejar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) @@ -119,15 +136,19 @@ class YarrProvider(Provider): except: error = traceback.format_exc() + self.login_opener = None log.error('Failed to login %s: %s', (self.getName(), error)) return False def loginSuccess(self, output): return True + def loginCheckSuccess(self, output): + return True + def loginDownload(self, url = '', nzb_id = ''): try: - if not self.login_opener and not self.login(): + if not self.login(): log.error('Failed downloading from %s', self.getName()) return self.urlopen(url, opener = self.login_opener) except: diff --git a/couchpotato/core/providers/nzb/ftdworld/main.py b/couchpotato/core/providers/nzb/ftdworld/main.py index caecadad..340dac14 100644 --- a/couchpotato/core/providers/nzb/ftdworld/main.py +++ b/couchpotato/core/providers/nzb/ftdworld/main.py @@ -16,6 +16,7 @@ class FTDWorld(NZBProvider): 'detail': 'http://ftdworld.net/spotinfo.php?id=%s', 'download': 'http://ftdworld.net/cgi-bin/nzbdown.pl?fileID=%s', 'login': 'http://ftdworld.net/api/login.php', + 'login_check': 'http://ftdworld.net/api/login.php', } http_time_between_calls = 3 #seconds @@ -78,3 +79,6 @@ class FTDWorld(NZBProvider): return json.loads(output).get('goodToGo', False) except: return False + + loginCheckSuccess = loginSuccess + diff --git a/couchpotato/core/providers/torrent/hdbits/main.py b/couchpotato/core/providers/torrent/hdbits/main.py index 218c3691..53f9a7ed 100644 --- a/couchpotato/core/providers/torrent/hdbits/main.py +++ b/couchpotato/core/providers/torrent/hdbits/main.py @@ -16,6 +16,7 @@ class HDBits(TorrentProvider): 'detail' : 'https://hdbits.org/details.php?id=%s&source=browse', 'search' : 'https://hdbits.org/json_search.php?imdb=%s', 'download' : 'https://hdbits.org/download.php/%s.torrent?id=%s&passkey=%s&source=details.browse', + 'login_check': 'http://hdbits.org/inbox.php', } http_time_between_calls = 1 #seconds @@ -53,3 +54,5 @@ class HDBits(TorrentProvider): def loginSuccess(self, output): return '/logout.php' in output.lower() + + loginCheckSuccess = loginSuccess diff --git a/couchpotato/core/providers/torrent/iptorrents/main.py b/couchpotato/core/providers/torrent/iptorrents/main.py index 75dc6f12..70fbc6de 100644 --- a/couchpotato/core/providers/torrent/iptorrents/main.py +++ b/couchpotato/core/providers/torrent/iptorrents/main.py @@ -16,6 +16,7 @@ class IPTorrents(TorrentProvider): 'base_url' : 'http://www.iptorrents.com', 'login' : 'http://www.iptorrents.com/torrents/', 'search' : 'http://www.iptorrents.com/torrents/?l%d=1%s&q=%s&qf=ti', + 'login_check': 'http://www.iptorrents.com/inbox.php', } cat_ids = [ @@ -72,12 +73,15 @@ class IPTorrents(TorrentProvider): except: log.error('Failed to parsing %s: %s', (self.getName(), traceback.format_exc())) - def loginSuccess(self, output): - return 'don\'t have an account' not in output.lower() - def getLoginParams(self): return tryUrlencode({ 'username': self.conf('username'), 'password': self.conf('password'), 'login': 'submit', }) + + def loginSuccess(self, output): + return 'don\'t have an account' not in output.lower() + + def loginCheckSuccess(self, output): + return '/logout.php' in output.lower() diff --git a/couchpotato/core/providers/torrent/passthepopcorn/main.py b/couchpotato/core/providers/torrent/passthepopcorn/main.py index 3f6e2b83..8a75ec53 100644 --- a/couchpotato/core/providers/torrent/passthepopcorn/main.py +++ b/couchpotato/core/providers/torrent/passthepopcorn/main.py @@ -21,6 +21,7 @@ class PassThePopcorn(TorrentProvider): 'detail': 'https://tls.passthepopcorn.me/torrents.php?torrentid=%s', 'torrent': 'https://tls.passthepopcorn.me/torrents.php', 'login': 'https://tls.passthepopcorn.me/ajax.php?action=login', + 'login_check': 'https://tls.passthepopcorn.me/ajax.php?action=login', 'search': 'https://tls.passthepopcorn.me/search/%s/0/7/%d' } @@ -52,18 +53,6 @@ class PassThePopcorn(TorrentProvider): 'cam': {'Source': ['CAM']} } - class NotLoggedInHTTPError(urllib2.HTTPError): - def __init__(self, url, code, msg, headers, fp): - urllib2.HTTPError.__init__(self, url, code, msg, headers, fp) - - class PTPHTTPRedirectHandler(urllib2.HTTPRedirectHandler): - def http_error_302(self, req, fp, code, msg, headers): - log.debug("302 detected; redirected to %s", headers['Location']) - if (headers['Location'] != 'login.php'): - return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers) - else: - raise PassThePopcorn.NotLoggedInHTTPError(req.get_full_url(), code, msg, headers, fp) - def _search(self, movie, quality, results): movie_title = getTitle(movie['library']) @@ -76,7 +65,7 @@ class PassThePopcorn(TorrentProvider): }) # Do login for the cookies - if not self.login_opener and not self.login(): + if not self.login(): return try: @@ -142,34 +131,6 @@ class PassThePopcorn(TorrentProvider): except: log.error('Failed getting results from %s: %s', (self.getName(), traceback.format_exc())) - def login(self): - - cookieprocessor = urllib2.HTTPCookieProcessor(cookielib.CookieJar()) - opener = urllib2.build_opener(cookieprocessor, PassThePopcorn.PTPHTTPRedirectHandler()) - opener.addheaders = [ - ('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1'), - ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), - ('Accept-Language', 'en-gb,en;q=0.5'), - ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'), - ('Keep-Alive', '115'), - ('Connection', 'keep-alive'), - ('Cache-Control', 'max-age=0'), - ] - - try: - response = opener.open(self.urls['login'], self.getLoginParams()) - except urllib2.URLError as e: - log.error('Login to PassThePopcorn failed: %s', e) - return False - - if response.getcode() == 200: - log.debug('Login HTTP status 200; seems successful') - self.login_opener = opener - return True - else: - log.error('Login to PassThePopcorn failed: returned code %d', response.getcode()) - return False - def torrentMeetsQualitySpec(self, torrent, quality): if not quality in self.post_search_filters: @@ -244,3 +205,11 @@ class PassThePopcorn(TorrentProvider): 'keeplogged': '1', 'login': 'Login' }) + + def loginSuccess(self, output): + try: + return json.loads(output).get('Result', '').lower() == 'ok' + except: + return False + + loginCheckSuccess = loginSuccess diff --git a/couchpotato/core/providers/torrent/sceneaccess/main.py b/couchpotato/core/providers/torrent/sceneaccess/main.py index 904bafb9..67e4447d 100644 --- a/couchpotato/core/providers/torrent/sceneaccess/main.py +++ b/couchpotato/core/providers/torrent/sceneaccess/main.py @@ -12,7 +12,8 @@ class SceneAccess(TorrentProvider): urls = { 'test': 'https://www.sceneaccess.eu/', - 'login' : 'https://www.sceneaccess.eu/login', + 'login': 'https://www.sceneaccess.eu/login', + 'login_check': 'https://sceneaccess.eu/inbox', 'detail': 'https://www.sceneaccess.eu/details?id=%s', 'search': 'https://www.sceneaccess.eu/browse?method=2&c%d=%d', 'download': 'https://www.sceneaccess.eu/%s', @@ -40,7 +41,7 @@ class SceneAccess(TorrentProvider): url = "%s&%s" % (url, arguments) # Do login for the cookies - if not self.login_opener and not self.login(): + if not self.login(): return data = self.getHTMLData(url, opener = self.login_opener) @@ -91,3 +92,8 @@ class SceneAccess(TorrentProvider): item['description'] = description return item + + def loginSuccess(self, output): + return '/inbox' in output.lower() + + loginCheckSuccess = loginSuccess diff --git a/couchpotato/core/providers/torrent/scenehd/main.py b/couchpotato/core/providers/torrent/scenehd/main.py index 93897c67..d3d79cc6 100644 --- a/couchpotato/core/providers/torrent/scenehd/main.py +++ b/couchpotato/core/providers/torrent/scenehd/main.py @@ -29,7 +29,7 @@ class SceneHD(TorrentProvider): url = "%s&%s" % (self.urls['search'], arguments) # Cookie login - if not self.login_opener and not self.login(): + if not self.login(): return data = self.getHTMLData(url, opener = self.login_opener) diff --git a/couchpotato/core/providers/torrent/torrentday/main.py b/couchpotato/core/providers/torrent/torrentday/main.py index 5e207c1e..c804d0e0 100644 --- a/couchpotato/core/providers/torrent/torrentday/main.py +++ b/couchpotato/core/providers/torrent/torrentday/main.py @@ -10,7 +10,8 @@ class TorrentDay(TorrentProvider): urls = { 'test': 'http://www.td.af/', - 'login' : 'http://www.td.af/torrents/', + 'login': 'http://www.td.af/torrents/', + 'login_check': 'http://www.torrentday.com/userdetails.php', 'detail': 'http://www.td.af/details.php?id=%s', 'search': 'http://www.td.af/V3/API/API.php', 'download': 'http://www.td.af/download.php/%s/%s', @@ -62,3 +63,6 @@ class TorrentDay(TorrentProvider): def loginSuccess(self, output): return 'Password not correct' not in output + + def loginCheckSuccess(self, output): + return 'logout.php' in output.lower() diff --git a/couchpotato/core/providers/torrent/torrentleech/main.py b/couchpotato/core/providers/torrent/torrentleech/main.py index 4247c530..8c41340f 100644 --- a/couchpotato/core/providers/torrent/torrentleech/main.py +++ b/couchpotato/core/providers/torrent/torrentleech/main.py @@ -14,6 +14,7 @@ class TorrentLeech(TorrentProvider): urls = { 'test' : 'http://www.torrentleech.org/', 'login' : 'http://www.torrentleech.org/user/account/login/', + 'login_check': 'http://torrentleech.org/user/messages', 'detail' : 'http://www.torrentleech.org/torrent/%s', 'search' : 'http://www.torrentleech.org/torrents/browse/index/query/%s/categories/%d', 'download' : 'http://www.torrentleech.org%s', @@ -77,3 +78,5 @@ class TorrentLeech(TorrentProvider): def loginSuccess(self, output): return '/user/account/logout' in output.lower() or 'welcome back' in output.lower() + + loginCheckSuccess = loginSuccess diff --git a/couchpotato/core/providers/torrent/torrentshack/main.py b/couchpotato/core/providers/torrent/torrentshack/main.py index df2ea291..1ef6c1df 100644 --- a/couchpotato/core/providers/torrent/torrentshack/main.py +++ b/couchpotato/core/providers/torrent/torrentshack/main.py @@ -13,6 +13,7 @@ class TorrentShack(TorrentProvider): urls = { 'test' : 'http://www.torrentshack.net/', 'login' : 'http://www.torrentshack.net/login.php', + 'login_check': 'http://www.torrentshack.net/inbox.php', 'detail' : 'http://www.torrentshack.net/torrent/%s', 'search' : 'http://www.torrentshack.net/torrents.php?searchstr=%s&filter_cat[%d]=1', 'download' : 'http://www.torrentshack.net/%s', @@ -79,3 +80,5 @@ class TorrentShack(TorrentProvider): def loginSuccess(self, output): return 'logout.php' in output.lower() + + loginCheckSuccess = loginSuccess