import os
import time
from http.client import IncompleteRead
from urllib.error import HTTPError
from urllib.request import Request, urlopen

from tqdm import tqdm

import metadata

items = metadata.load()
progress = tqdm(total=len(items), dynamic_ncols=True)
total_bytes = 0

for item in items:
    progress.update()
    file_name = item.image_filename

    try:
        if os.path.exists(file_name) and os.path.getsize(file_name) > 0:
            continue
    except (IOError, OSError):
        continue

    n_tries_max = 5
    i_try = 0
    while i_try < n_tries_max:
        try:
            start = time.time()
            request = Request(item.image_url)
            with open(file_name, 'wb') as image_file:
                image_file.write(urlopen(request).read())
                current_bytes = image_file.tell()
            total_bytes += current_bytes
            if total_bytes < 0.9e9:
                progress.desc = '{:0.1f} MB, {:.1f} MB/s'.format(
                    total_bytes * 1e-6, current_bytes * 1e-6 / (time.time() - start))
            else:
                progress.desc = '{:0.1f} GB, {:.1f} MB/s'.format(
                    total_bytes * 1e-9, current_bytes * 1e-6 / (time.time() - start))
        except (HTTPError, IncompleteRead):
            print("Error downloading file {:s}".format(item.image_url))
            break
        except (IOError, OSError) as e:
            print("Error storing file {:s}".format(file_name))
            print(e)
            time.sleep(2)
            i_try += 1
        else:
            break
