-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickler.py
40 lines (30 loc) · 1.24 KB
/
pickler.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
38
39
40
"""Script to download honcode database as pickled python dictionary"""
import argparse
import os
import pickle
parser = argparse.ArgumentParser()
parser.add_argument("--path", default=None, help="Desired database location.")
args = parser.parse_args()
def download_honcode_database(path="data"):
"""downloads honcode database as .txt file to `path`"""
os.system(f"wget -P {path} https://www.honcode.ch/HONcode/Plugin/listeMD5.txt")
def pickle_honcode_database(path="data"):
"""pickles honcode .txt database at `path`"""
d = {}
file_path = os.path.join(os.getcwd(), path, "listeMD5.txt")
with open(file_path, "r", encoding="utf-8") as md5_file:
for line in md5_file:
if len(line.split()) == 2:
url_hash, hon_code = line.split()
d[url_hash] = hon_code
out_file = os.path.join(os.getcwd(), path, "honcode.dat")
with open(out_file, "wb+") as dat_file:
pickle.dump(d, dat_file)
print(f"Data written to {out_file}")
if __name__ == "__main__":
download_honcode_database(
args.path
) if args.path is not None else download_honcode_database()
pickle_honcode_database(
args.path
) if args.path is not None else pickle_honcode_database()