From f787e4e2027583476ca34bd01c8462f6459369bb Mon Sep 17 00:00:00 2001 From: James Taylor Date: Fri, 31 Jan 2020 20:06:15 -0800 Subject: Give a proper error message for 429 errors These occur when too many requests are coming from a Tor exit node. Before, there would be an error page with an exception instructing users to report the issue. But this is an expected and persistent issue. --- youtube/util.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'youtube/util.py') diff --git a/youtube/util.py b/youtube/util.py index feeec8c..f209060 100644 --- a/youtube/util.py +++ b/youtube/util.py @@ -97,6 +97,12 @@ class HTTPAsymmetricCookieProcessor(urllib.request.BaseHandler): https_request = http_request https_response = http_response +class FetchError(Exception): + def __init__(self, code, reason='', ip=None): + Exception.__init__(self, 'HTTP error during request: ' + code + ' ' + reason) + self.code = code + self.reason = reason + self.ip = ip def decode_content(content, encoding_header): encodings = encoding_header.replace(' ', '').split(',') @@ -161,6 +167,17 @@ def fetch_url(url, headers=(), timeout=15, report_text=None, data=None, cookieja content = response.read() response.release_conn() + if (response.status == 429 + and content.startswith(b'= 400: + raise FetchError(str(response.status), reason=response.reason, ip=None) + read_finish = time.time() if report_text: print(report_text, ' Latency:', round(response_time - start_time,3), ' Read time:', round(read_finish - response_time,3)) @@ -359,3 +376,9 @@ def parse_info_prepare_for_html(renderer, additional_info={}): add_extra_html_info(item) return item + +def check_gevent_exceptions(*tasks): + for task in tasks: + if task.exception: + raise task.exception + -- cgit v1.2.3