forked from rwv/chinese-dos-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload_data.py
61 lines (46 loc) · 1.61 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
import hashlib
import inspect
import os
import json
import urllib.request
from concurrent.futures import ThreadPoolExecutor, wait
root = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
PREFIX = "https://dos-bin.zczc.cz/"
DESTINATION = os.path.join(root, 'bin')
BUF_SIZE = 65536
THREAD_SIZE = 10
# read game infos from games.json
with open(os.path.join(root, 'games.json'), encoding='utf8') as f:
game_infos = json.load(f)
def generate_sha256(file):
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(f'Downloading {identifier} game file')
urllib.request.urlretrieve(url, file)
def main(prefix=PREFIX, destination=DESTINATION):
# create folder
os.makedirs(destination, exist_ok=True)
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(f'skip {identifier}')
else:
downloaded.append(identifier)
task = executor.submit(download, identifier, url, file)
all_task.append(task)
wait(all_task)
return downloaded
if __name__ == '__main__':
main()