-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.py
37 lines (33 loc) · 965 Bytes
/
hash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from requests import get
from shutil import copyfileobj
from hashlib import sha256
import gzip
# fetch and save to file
def fetch_and_save(url, name):
file = get(url, stream=True)
with open(name, 'wb') as out:
copyfileobj(file.raw, out)
# generate sha265 of file
def hashfile(file):
with open(file, "rb") as infile:
f = infile.read()
hash = sha256()
hash.update(f)
return hash.hexdigest()
# generate sha265 of stream directly from url (to use for modification checks)
def fetch_and_hash(url):
file = get(url, stream=True)
f = file.raw.read()
hash = sha256()
hash.update(f)
return hash.hexdigest()
# some files show up gzizpped, and this saves text files in binary. The function below gets them out either way.
def open_binary_string_file(filename):
try:
with open(filename, "rb") as f:
bytes = f.read()
return bytes.decode()
except UnicodeDecodeError:
with gzip.open(filename, "rb") as f:
bytes = f.read()
return bytes.decode()