Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Created June 16, 2020 02:12
Show Gist options
  • Save pyrofolium/b34ebd4ccd173edb3e11fccf2e7f7bd8 to your computer and use it in GitHub Desktop.
Save pyrofolium/b34ebd4ccd173edb3e11fccf2e7f7bd8 to your computer and use it in GitHub Desktop.
# 3003792
# http://i.imgur.com/cz0yhtx.jpg
# http://i.imgur.com/KxyEGOn.jpg
# http://i.imgur.com/vPae8qL.jpg
# http://i.imgur.com/cz0yhtx.jpg
# bad_input
# http://www.google.com/isadsada
import sys
import urllib.request
from typing import Dict, Tuple
import hashlib
from collections import OrderedDict
# MUST USE LATEST VERSION OF PYTHON. LATEST VERSION OF PYTHON DICTS HAVE ORDER, EARLIER THEY DO NOT!!!!!!. (3.7++)
def process_picture(url_string: str, input_capacity: int, input_current_size: int = 0,
input_stored: Dict[str, int] = None) -> Tuple[int, str]:
input_stored = OrderedDict() if input_stored is None else input_stored
if url_string in input_stored:
value = input_stored[url_string]
del input_stored[url_string] # updating dict does not change order must delete and insert new value.
input_stored[url_string] = value
return input_current_size, f"{url_string.strip()} CACHE {len(input_stored[url_string])} {hashlib.sha256(input_stored[url_string]).hexdigest()}"
else:
with urllib.request.urlopen(url_string) as response:
picture = response.read()
size = len(picture)
if size <= input_capacity - input_current_size:
input_stored[url_string] = picture
input_current_size += len(picture)
return input_current_size, f"{url_string.strip()} DOWNLOADED {len(input_stored[url_string])} {hashlib.sha256(input_stored[url_string]).hexdigest()}"
elif size > input_capacity:
return input_current_size, f"{url_string.strip()} DOWNLOADED {len(picture)} {hashlib.sha256(picture).hexdigest()}"
else:
for key, value in [i for i in input_stored.items()]:
if size > input_capacity - input_current_size:
input_current_size -= len(value)
del input_stored[key]
else:
break
input_stored[url_string] = picture
input_current_size += len(picture)
return input_current_size, f"{url_string.strip()} DOWNLOADED {len(input_stored[url_string])} {hashlib.sha256(input_stored[url_string]).hexdigest()}"
input_file_name = sys.argv[-2]
output_file_name = sys.argv[-1]
input_file = open(input_file_name, 'r')
capacity = None
current_size = 0
stored = OrderedDict()
output_file = open(output_file_name, 'w')
for index, line in enumerate(input_file.readlines()):
if index == 0:
capacity = int(line.strip())
print(capacity)
else:
url = line
try:
current_size, output_string = process_picture(url, capacity, current_size, stored)
output_file.write(output_string + "\n")
except Exception as _:
continue
output_file.close()
input_file.close()
# print(input_file_name)
# print(output_file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment