Skip to content

Commit

Permalink
fix rwv#37
Browse files Browse the repository at this point in the history
  • Loading branch information
rwv committed Sep 7, 2018
1 parent e6fd926 commit 502d71d
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 48 deletions.
83 changes: 73 additions & 10 deletions download_data.py
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()
10 changes: 9 additions & 1 deletion game_infos.py
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))
Loading

0 comments on commit 502d71d

Please sign in to comment.