From 4a9452672a77d14c6c0c8def313050b1d645b0eb Mon Sep 17 00:00:00 2001 From: Ruud Date: Wed, 17 Sep 2014 12:59:24 +0200 Subject: [PATCH] Use status_code to stop requesting url --- couchpotato/core/media/_base/providers/nzb/newznab.py | 7 ++++--- couchpotato/core/plugins/base.py | 9 ++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/couchpotato/core/media/_base/providers/nzb/newznab.py b/couchpotato/core/media/_base/providers/nzb/newznab.py index 87ecc752..62b787d8 100644 --- a/couchpotato/core/media/_base/providers/nzb/newznab.py +++ b/couchpotato/core/media/_base/providers/nzb/newznab.py @@ -187,11 +187,12 @@ class Base(NZBProvider, RSS): self.limits_reached[host] = False return data except HTTPError as e: - if e.response.status_code == 503: + sc = e.response.status_code + if sc in [503, 429]: response = e.read().lower() - if 'maximum api' in response or 'download limit' in response: + if sc == 429 or 'maximum api' in response or 'download limit' in response: if not self.limits_reached.get(host): - log.error('Limit reached for newznab provider: %s', host) + log.error('Limit reached / to many requests for newznab provider: %s', host) self.limits_reached[host] = time.time() return 'try_next' diff --git a/couchpotato/core/plugins/base.py b/couchpotato/core/plugins/base.py index 2c7f9e13..ecc77b30 100644 --- a/couchpotato/core/plugins/base.py +++ b/couchpotato/core/plugins/base.py @@ -198,6 +198,7 @@ class Plugin(object): del self.http_failed_disabled[host] self.wait(host) + status_code = None try: kwargs = { @@ -212,6 +213,7 @@ class Plugin(object): log.info('Opening url: %s %s, data: %s', (method, url, [x for x in data.keys()] if isinstance(data, dict) else 'with data')) response = r.request(method, url, **kwargs) + status_code = response.status_code if response.status_code == requests.codes.ok: data = response.content else: @@ -224,6 +226,11 @@ class Plugin(object): # Save failed requests by hosts try: + + # To many requests + if status_code == 429: + self.http_failed_request[host] = time.time() + if not self.http_failed_request.get(host): self.http_failed_request[host] = 1 else: @@ -255,7 +262,7 @@ class Plugin(object): if wait > 0: log.debug('Waiting for %s, %d seconds', (self.getName(), wait)) - time.sleep(wait) + time.sleep(min(wait, 30)) def beforeCall(self, handler): self.isRunning('%s.%s' % (self.getName(), handler.__name__))