# see LICENSE file for copyright and license details

"""
Network functions/helpers
"""

from os import makedirs
from os.path import dirname
import requests

from lib.log import die, info, warn


def download(uris):
    """
    Downloads a file by providing it an url and a write path in a tuple
    """
    url = uris[0]
    path = uris[1]
    info("dl: %s" % url)

    try:
        r = requests.get(url, stream=True, timeout=20)
    # TODO: investigate also requests.exceptions.ReadTimeout
    except requests.exceptions.ConnectionError as e:
        warn('Caught exception: "%s". Retrying...' % e)
        return download(uris)

    if r.status_code == 404:
        warn('failed: 404 not found!')
        return
    elif r.status_code != 200:
        die('failed: %d' % r.status_code)

    makedirs(dirname(path), exist_ok=True)
    f = open(path, 'wb')
    # chunk_size {sh,c}ould be more on gbit servers
    for chunk in r.iter_content(chunk_size=1024):
        if chunk:
            f.write(chunk)
            # f.flush()
    f.close()

@@ -432,75 +432,64 @@
     
-# TODO: investigate also requests.exceptions.ReadTimeout%0A    except
+except (requests.exceptions.ConnectionError,%0A           
  req
@@ -505,31 +505,28 @@
 eptions.
-ConnectionError
+ReadTimeout)
  as e:%0A 
@@ -636,14 +636,14 @@
 ode 
-== 404
+!= 200
 :%0A  
@@ -654,24 +654,27 @@
   warn('
+%25s 
 failed: 
 404 not 
@@ -669,48 +669,19 @@
 ed: 
-404 not found!')%0A        return%0A    elif
+%25d' %25 (url,
  r.s
@@ -686,32 +686,26 @@
 .status_code
- != 200:
+))
 %0A        die
@@ -705,41 +705,14 @@
     
-die('failed: %25d' %25 r.status_code)
+return
 %0A%0A  

fix multiprocessing issue with download()