mySpindel/LittleBock/add_dates.py
2022-05-18 12:02:06 +02:00

45 lines
1.1 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
"""
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)))
#print(f" {dt} : {sdt}")
return sdt
def jsproc(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()
jsdocs = tuple(here.rglob("*.json"))
if len(jsdocs) == 0:
print("Aucun fichier JSON ('.json') trouvé.")
for i in jsdocs:
if i.exists() and i.stat().st_size > 0:
data = jsproc(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é")