46 lines
1.2 KiB
Python
Executable File
46 lines
1.2 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import json
|
|
import pathlib
|
|
import datetime
|
|
|
|
"""
|
|
ajoute une valeur de date lisible par un humain à chaque entrée d'un document JSON
|
|
|
|
TODO: rassembler les dates dans un groupe d'entrées
|
|
|
|
"""
|
|
|
|
def from_stamp(i: int) -> str:
|
|
"""normalise une date UNIX"""
|
|
dt = str(i)
|
|
if len(dt)>10:
|
|
dt = dt[:10]
|
|
sdt = str(datetime.datetime.fromtimestamp(int(dt)))
|
|
return sdt
|
|
|
|
def proc(path_in):
|
|
"""traite un document JSON"""
|
|
_json = None
|
|
with open(path_in,'r') as f:
|
|
_json = json.load(f)
|
|
for group in _json:
|
|
for entry in group["data"]:
|
|
entry.update({"date":from_stamp(entry["x"])})
|
|
return _json
|
|
|
|
if __name__ == "__main__":
|
|
here = pathlib.Path.cwd()
|
|
docs = tuple(here.rglob("*.json"))
|
|
if len(docs) == 0:
|
|
print("Aucun fichier JSON ('.json') trouvé.")
|
|
for i in docs:
|
|
if i.exists() and i.stat().st_size > 0:
|
|
data = proc(i)
|
|
if data:
|
|
with open(i,'w') as f:
|
|
f.write(json.dumps(data,
|
|
sort_keys=False, ensure_ascii=False, indent=2))
|
|
print(f"INFO: {i.name} modifié")
|
|
|