forked from rwv/chinese-dos-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload_data.py
104 lines (82 loc) · 2.87 KB
/
download_data.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import hashlib
import inspect
import os
import json
import urllib.request
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor, wait
root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
PREFIX = "https://dos.zczc.cz/static/games/bin/"
DESTINATION = os.path.join(root, 'bin')
BUF_SIZE = 65536
THREAD_SIZE = 10
with open(os.path.join(root, 'games.json'), encoding='utf8') as f:
content = f.read()
game_infos_ordered = json.loads(content, object_pairs_hook=OrderedDict)
game_infos = json.loads(content)
def update_json(ordered_dict):
with open(os.path.join(root, 'games.json'), encoding='utf8', mode='w') as f:
f.write(json.dumps(ordered_dict, indent=2, ensure_ascii=False))
def generate_sha256(file):
"""
generate file's sha256 checksum
:param file: file's location
:return: sha256 string
"""
sha256 = hashlib.sha256()
with open(file, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
sha256.update(data)
return sha256.hexdigest()
def download(identifier, url, file):
print('Downloading {} game file'.format(identifier))
urllib.request.urlretrieve(url, file)
def main(prefix=PREFIX, destination=DESTINATION):
"""
check game archives whether exists and their checksum, download from target.
:return: the list of downloaded file
"""
# create folder
folder = os.path.isdir(destination)
if not folder:
os.makedirs(destination)
executor = ThreadPoolExecutor(max_workers=THREAD_SIZE)
all_task = list()
downloaded = list()
for identifier in game_infos['games'].keys():
file = os.path.normcase(os.path.join(destination, identifier + '.zip'))
url = prefix + urllib.parse.quote(identifier) + '.zip'
if os.path.isfile(file) and generate_sha256(file) == game_infos['games'][identifier]['sha256']:
print('skip {}'.format(identifier))
else:
downloaded.append(identifier)
task = executor.submit(download, identifier, url, file)
all_task.append(task)
wait(all_task)
return downloaded
def files_sha256():
"""
print existing game archives' sha256
:return: a dict of sha256 string
"""
result = dict()
for identifier in game_infos_ordered['games'].keys():
file = os.path.normcase(os.path.join(DESTINATION, identifier + '.zip'))
if os.path.isfile(file):
result[identifier] = generate_sha256(file)
return result
def update_sha256():
"""
update sha256 to the json
:return: the updated json
"""
sha256_dict = files_sha256()
for identifier, sha256 in sha256_dict.items():
game_infos_ordered['games'][identifier]['sha256'] = sha256
update_json(game_infos_ordered)
return
if __name__ == '__main__':
main()