2015-04-10 21:45:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2014, 2015 Mike Fährmann
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Downloader module for http urls"""
|
|
|
|
|
2014-10-12 21:56:44 +02:00
|
|
|
from .common import BasicDownloader
|
|
|
|
import time
|
|
|
|
import requests
|
|
|
|
|
|
|
|
class Downloader(BasicDownloader):
|
|
|
|
|
2015-04-10 19:19:12 +02:00
|
|
|
def __init__(self):
|
2014-10-12 21:56:44 +02:00
|
|
|
BasicDownloader.__init__(self)
|
2015-04-10 19:19:12 +02:00
|
|
|
self.session = requests.session()
|
2014-10-12 21:56:44 +02:00
|
|
|
|
|
|
|
def download_impl(self, url, file):
|
|
|
|
tries = 0
|
|
|
|
while True:
|
|
|
|
# try to connect to remote source
|
|
|
|
try:
|
|
|
|
response = self.session.get(url, stream=True, verify=True)
|
2015-04-10 21:45:41 +02:00
|
|
|
except requests.exceptions.ConnectionError as exptn:
|
2014-10-12 21:56:44 +02:00
|
|
|
tries += 1
|
2015-04-10 21:45:41 +02:00
|
|
|
self.print_error(file, exptn, tries, self.max_tries)
|
2014-10-12 21:56:44 +02:00
|
|
|
time.sleep(1)
|
|
|
|
if tries == self.max_tries:
|
|
|
|
raise
|
|
|
|
continue
|
|
|
|
|
|
|
|
# reject error-status-codes
|
|
|
|
if response.status_code != requests.codes.ok:
|
|
|
|
tries += 1
|
|
|
|
self.print_error(file, 'HTTP status "{} {}"'.format(
|
|
|
|
response.status_code, response.reason), tries, self.max_tries)
|
|
|
|
if response.status_code == 404:
|
|
|
|
return self.max_tries
|
|
|
|
time.sleep(1)
|
|
|
|
if tries == 5:
|
|
|
|
response.raise_for_status()
|
|
|
|
continue
|
|
|
|
|
|
|
|
# everything ok -- proceed to download
|
|
|
|
break
|
|
|
|
|
|
|
|
for data in response.iter_content(16384):
|
|
|
|
file.write(data)
|
|
|
|
return tries
|
2015-04-08 19:06:50 +02:00
|
|
|
|
|
|
|
def set_headers(self, headers):
|
2015-04-10 21:45:41 +02:00
|
|
|
"""Set headers for http requests"""
|
2015-04-08 19:06:50 +02:00
|
|
|
self.set_dict(self.session.headers, headers)
|
|
|
|
|
|
|
|
def set_cookies(self, cookies):
|
2015-04-10 21:45:41 +02:00
|
|
|
"""Set cookies for http requests"""
|
2015-04-08 19:06:50 +02:00
|
|
|
self.set_dict(self.session.cookies, cookies)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_dict(dest, src):
|
2015-04-10 21:45:41 +02:00
|
|
|
"""Copy the contents of dictionary 'src' to 'dest'"""
|
2015-04-08 19:06:50 +02:00
|
|
|
dest.clear()
|
|
|
|
dest.update(src)
|
|
|
|
|