forked from rwv/chinese-dos-games
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
156 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,84 @@ | ||
import hashlib | ||
import inspect | ||
import os | ||
import urllib.request | ||
|
||
from game_infos import game_infos | ||
from game_infos import game_infos, game_infos_ordered, update_json | ||
|
||
root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) | ||
|
||
PREFIX = "https://dos.zczc.cz/static/gamedata/" | ||
DESTINATION = os.path.join(root, 'static', 'gamedata') | ||
BUF_SIZE = 65536 | ||
|
||
# 创建文件夹 | ||
folder = os.path.isdir(DESTINATION) | ||
if not folder: | ||
os.makedirs(DESTINATION) | ||
|
||
for identifier in game_infos['games'].keys(): | ||
url = PREFIX + urllib.parse.quote(identifier) + '.zip' | ||
file = os.path.normcase(os.path.join(DESTINATION, identifier + '.zip')) | ||
print('Downloading {} game file'.format(identifier)) | ||
urllib.request.urlretrieve(url, file) | ||
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 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) | ||
|
||
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) | ||
print('Downloading {} game file'.format(identifier)) | ||
urllib.request.urlretrieve(url, file) | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import inspect | ||
import json | ||
import os | ||
from collections import OrderedDict | ||
|
||
root = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) | ||
|
||
with open(os.path.join(root, 'games.json'), encoding='utf8') as f: | ||
game_infos = json.loads(f.read()) | ||
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)) |
Oops, something went wrong.