Python3 Migrate

This commit is contained in:
MariuszC
2020-01-18 20:01:00 +01:00
parent ea05af2d15
commit 6cd7e0fe44
691 changed files with 201846 additions and 598 deletions

View File

@@ -0,0 +1,57 @@
import logging
import time
import requests
from mopidy import httpclient
logger = logging.getLogger(__name__)
def get_requests_session(proxy_config, user_agent):
proxy = httpclient.format_proxy(proxy_config)
full_user_agent = httpclient.format_user_agent(user_agent)
session = requests.Session()
session.proxies.update({"http": proxy, "https": proxy})
session.headers.update({"user-agent": full_user_agent})
return session
def download(session, uri, timeout=1.0, chunk_size=4096):
try:
response = session.get(uri, stream=True, timeout=timeout)
except requests.exceptions.Timeout:
logger.warning(
"Download of %r failed due to connection timeout after " "%.3fs",
uri,
timeout,
)
return None
except requests.exceptions.InvalidSchema:
logger.warning("Download of %r failed due to unsupported schema", uri)
return None
except requests.exceptions.RequestException as exc:
logger.warning("Download of %r failed: %s", uri, exc)
logger.debug("Download exception details", exc_info=True)
return None
content = []
deadline = time.time() + timeout
for chunk in response.iter_content(chunk_size):
content.append(chunk)
if time.time() > deadline:
logger.warning(
"Download of %r failed due to download taking more "
"than %.3fs",
uri,
timeout,
)
return None
if not response.ok:
logger.warning("Problem downloading %r: %s", uri, response.reason)
return None
return b"".join(content)