From 854386f22fd7c38462e3d89f46c94f5c13ac197b Mon Sep 17 00:00:00 2001 From: JayXon Date: Sun, 22 May 2016 23:07:10 -0700 Subject: [PATCH 1/2] Retry request if timeout in get_content --- src/you_get/common.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/you_get/common.py b/src/you_get/common.py index 9668df58..dadba69c 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -98,6 +98,7 @@ import logging import os import platform import re +import socket import sys import time from urllib import request, parse, error @@ -308,7 +309,14 @@ def get_content(url, headers={}, decoded=True): if cookies: cookies.add_cookie_header(req) req.headers.update(req.unredirected_hdrs) - response = request.urlopen(req) + + for i in range(10): + try: + response = request.urlopen(req, timeout=10) + break + except socket.timeout: + logging.debug('request attempt %s timeout' % str(i + 1)) + data = response.read() # Handle HTTP compression for gzip and deflate (zlib) From 48640e279f451cb2d365154870ed20e2d8bcc3e4 Mon Sep 17 00:00:00 2001 From: JayXon Date: Tue, 24 May 2016 20:58:28 -0700 Subject: [PATCH 2/2] Add -t and --timeout option To set socket timeout, default is 600 seconds. --- src/you_get/common.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/you_get/common.py b/src/you_get/common.py index dadba69c..100f3869 100755 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -312,7 +312,7 @@ def get_content(url, headers={}, decoded=True): for i in range(10): try: - response = request.urlopen(req, timeout=10) + response = request.urlopen(req) break except socket.timeout: logging.debug('request attempt %s timeout' % str(i + 1)) @@ -1071,11 +1071,12 @@ def script_main(script_name, download, download_playlist, **kwargs): -x | --http-proxy Use an HTTP proxy for downloading. -y | --extractor-proxy Use an HTTP proxy for extracting only. --no-proxy Never use a proxy. + -t | --timeout Set socket timeout. -d | --debug Show traceback and other debug info. ''' - short_opts = 'Vhfiuc:ndF:O:o:p:x:y:' - opts = ['version', 'help', 'force', 'info', 'url', 'cookies', 'no-caption', 'no-merge', 'no-proxy', 'debug', 'json', 'format=', 'stream=', 'itag=', 'output-filename=', 'output-dir=', 'player=', 'http-proxy=', 'extractor-proxy=', 'lang='] + short_opts = 'Vhfiuc:ndF:O:o:p:x:y:t:' + opts = ['version', 'help', 'force', 'info', 'url', 'cookies', 'no-caption', 'no-merge', 'no-proxy', 'debug', 'json', 'format=', 'stream=', 'itag=', 'output-filename=', 'output-dir=', 'player=', 'http-proxy=', 'extractor-proxy=', 'lang=', 'timeout='] if download_playlist: short_opts = 'l' + short_opts opts = ['playlist'] + opts @@ -1105,6 +1106,7 @@ def script_main(script_name, download, download_playlist, **kwargs): proxy = None extractor_proxy = None traceback = False + timeout = 600 for o, a in opts: if o in ('-V', '--version'): version() @@ -1178,6 +1180,8 @@ def script_main(script_name, download, download_playlist, **kwargs): extractor_proxy = a elif o in ('--lang',): lang = a + elif o in ('-t', '--timeout'): + timeout = int(a) else: log.e("try 'you-get --help' for more options") sys.exit(2) @@ -1187,6 +1191,8 @@ def script_main(script_name, download, download_playlist, **kwargs): set_http_proxy(proxy) + socket.setdefaulttimeout(timeout) + try: if stream_id: if not extractor_proxy: