Skip to content

Instantly share code, notes, and snippets.

@pochemuto
Created December 19, 2017 20:41
Show Gist options
  • Save pochemuto/19f4e37bde520b39cd816e4015c47deb to your computer and use it in GitHub Desktop.
Save pochemuto/19f4e37bde520b39cd816e4015c47deb to your computer and use it in GitHub Desktop.
Checks that scraper database contains rom hashes
#!/usr/bin/env python3
import csv
import hashlib
import sys
import os
HASH_DATA_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'scraper/hash.csv')
def load():
result = {}
with open(HASH_DATA_FILE) as hash_data:
reader = csv.reader(hash_data)
for row in reader:
result[row[0]] = row[3]
return result
def sha(filepath):
sha_func = hashlib.sha1()
with open(filepath, 'rb') as input_file:
bytes = input_file.read()
sha_func.update(bytes)
return sha_func.hexdigest()
if __name__ == '__main__':
if len(sys.argv) == 1:
print("Too few arguments")
exit(1)
files = sys.argv[1:]
if len(sys.argv) == 2 and os.path.isdir(sys.argv[1]):
files = os.listdir(sys.argv[1])
hashes = load()
for filename in files:
if not os.path.isfile(filename):
print('skipped ' + filename)
continue
shasum = sha(filename)
found = shasum in hashes
if found:
print('OK ' + filename + ' ~~~ ' + hashes[shasum])
else:
print('MISSED ' + filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment