import requests
import os
from multiprocessing.pool import ThreadPool
import datetime
from time import time as timer


urls = []
path_to_media = 'https://www.thenewsspy.com/media'
most_common_languages = ["en", "de", "ar", "ru", "es", "fr", "no", "dk", "fi", "se", "nl", "it", "pt"]

for language in most_common_languages:
	for i in range(1,3):
		filename = f'{language.lower()}-{i}.mp4'
		urls.append((filename, f'{path_to_media}/{filename}'))

def url_response(url):
	path, url = url
	r = requests.get(url, stream = True)
	if r.status_code == 200:
		with open(path, 'wb') as f:
			for ch in r:
				f.write(ch)
	elif r.status_code == 404:
		with open('404.txt', 'a+') as f:
			f.write(f'[{datetime.datetime.now().strftime("%B %d, %Y %I:%M%p")}] "{path}" Not Found.\n')
	return f'[{r.status_code}] "{path}"'

start = timer()
results = ThreadPool(8).imap_unordered(url_response, urls)
for path in results:
    print(path)

print(f"Media download finished after: {timer() - start}")
