-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdate_json.py
54 lines (43 loc) · 1.42 KB
/
update_json.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
#!/usr/bin/env python
# Author: Wilfried JEANNIARD 2022 https://github.com/willoucom
# This work is licensed under the Creative Commons Attribution 4.0 International License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/
# or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
import os
import json
import hashlib
import calendar;
import time;
# Json file to be parsed
file_json = "games_menu.json"
# Get hash of the file
def hash_file(path):
with open(path, "rb") as f:
file_hash = hashlib.md5()
chunk = f.read(8192)
while chunk:
file_hash.update(chunk)
chunk = f.read(8192)
return file_hash.hexdigest()
# open Json
with open(file_json, 'r') as f:
data = json.load(f)
for file in data["files"]:
# Find the file
if os.path.exists(file):
filename = file
elif os.path.exists(os.path.basename(file)):
filename = os.path.basename(file)
# Update filesize
size = os.path.getsize(filename)
data["files"][file]['size'] = size
# Update hash
hash = hash_file(filename)
data["files"][file]['hash'] = hash
# Update timestamp
data["timestamp"] = calendar.timegm(time.gmtime())
# Serializing json
json_object = json.dumps(data, indent=4)
# Writing Json
with open(file_json, "w") as outfile:
outfile.write(json_object)