2016-05-05 11:23:15 +00:00
|
|
|
import urllib.request
|
2017-03-03 16:24:58 +00:00
|
|
|
from szurubooru import config
|
2016-09-25 12:52:47 +00:00
|
|
|
from szurubooru import errors
|
2016-05-05 11:23:15 +00:00
|
|
|
|
2016-08-14 12:22:53 +00:00
|
|
|
|
2017-02-04 00:08:12 +00:00
|
|
|
def download(url: str) -> bytes:
|
2016-08-14 08:45:00 +00:00
|
|
|
assert url
|
2016-05-05 11:23:15 +00:00
|
|
|
request = urllib.request.Request(url)
|
2017-03-03 16:24:58 +00:00
|
|
|
if config.config['user_agent']:
|
|
|
|
request.add_header('User-Agent', config.config['user_agent'])
|
2016-05-05 11:23:15 +00:00
|
|
|
request.add_header('Referer', url)
|
2016-09-25 12:52:47 +00:00
|
|
|
try:
|
|
|
|
with urllib.request.urlopen(request) as handle:
|
|
|
|
return handle.read()
|
2016-09-26 20:51:00 +00:00
|
|
|
except Exception as ex:
|
|
|
|
raise errors.ProcessingError('Error downloading %s (%s)' % (url, ex))
|