import requests
import json
import pycountry
from multiprocessing.pool import ThreadPool
import datetime
from time import time as timer

def append_to_urls(language):
    filename = f'{language.lower()}.json'
    urls.append((filename, f'{path_to_files}/{filename}'))

count = { 'available': 0, 'already_exists': 0, 'unavailable': 0 }
path_to_files = 'https://thesecurestoffers.org/bitqtapp/i18n'
urls = []

append_to_urls('default')
append_to_urls('en')
for country in pycountry.countries:
    append_to_urls(country.alpha_2)

def handle_response(url):
    path, url = url
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        with open('Available.txt', 'a+') as f:
            alpha_2 = path.replace(".json", "")
            country = pycountry.countries.get(alpha_2=alpha_2.upper())
            country_name = 'Unknown' if country is None else country.name
            f.write(f'[{alpha_2}] {country_name},\n')
        try:
            with open(path, 'x') as f:
                json.dump(response.json(), f)
                count['available'] += 1
        except FileExistsError:
            count['already_exists'] += 1

    elif response.status_code == 404:
        with open('404.txt', 'a+') as f:
            count['unavailable'] += 1
            f.write(f'[{datetime.datetime.now().strftime("%B %d, %Y %I:%M%p")}] "{path}" Not Found.\n')
    return f'[{response.status_code}] "{path}"'

start = timer()
results = ThreadPool(16).imap_unordered(handle_response, urls)
for result in results:
    print(result)

print(f"Translations download finished after: {timer() - start}")
print(f"Download result: Available: {count['available']}, Already Existing: {count['already_exists']}, Unavailable: {count['unavailable']}")
