1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

move code from util.py

This commit is contained in:
Mike Fährmann 2015-04-08 01:46:04 +02:00
parent 41f00809ff
commit 513808d156
2 changed files with 35 additions and 35 deletions

View File

@ -8,10 +8,12 @@
"""Common classes and constants used by extractor modules.""" """Common classes and constants used by extractor modules."""
import time
import queue import queue
import threading
import requests import requests
# from ..util import safe_request import threading
import html.parser
class Message(): class Message():
@ -95,3 +97,34 @@ class AsyncExtractor(Extractor):
import traceback import traceback
print(traceback.format_exc()) print(traceback.format_exc())
put(None) put(None)
def safe_request(session, url, method="GET", *args, **kwargs):
tries = 0
while True:
# try to connect to remote source
try:
r = session.request(method, url, *args, **kwargs)
except requests.exceptions.ConnectionError:
tries += 1
time.sleep(1)
if tries == 5:
raise
continue
# reject error-status-codes
if r.status_code != requests.codes.ok:
tries += 1
time.sleep(1)
if tries == 5:
r.raise_for_status()
continue
# everything ok -- proceed to download
return r
def filename_from_url(url):
pos = url.rfind("/")
return url[pos+1:]
unescape = html.parser.HTMLParser().unescape

View File

@ -1,33 +0,0 @@
import time
import requests
import html.parser
def safe_request(session, url, method="GET", *args, **kwargs):
tries = 0
while True:
# try to connect to remote source
try:
r = session.request(method, url, *args, **kwargs)
except requests.exceptions.ConnectionError:
tries += 1
time.sleep(1)
if tries == 5:
raise
continue
# reject error-status-codes
if r.status_code != requests.codes.ok:
tries += 1
time.sleep(1)
if tries == 5:
r.raise_for_status()
continue
# everything ok -- proceed to download
return r
def filename_from_url(url):
pos = url.rfind("/")
return url[pos+1:]
unescape = html.parser.HTMLParser().unescape