mySpindel/LittleBock/add_dates.py

46 lines
1.2 KiB
Python
Raw Normal View History

2022-05-18 12:02:06 +02:00
#!/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
2022-05-18 18:29:57 +02:00
TODO: rassembler les dates dans un groupe d'entrées
2022-05-18 12:02:06 +02:00
"""
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
2022-05-18 18:29:57 +02:00
def proc(path_in):
2022-05-18 12:02:06 +02:00
"""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()
2022-05-18 18:29:57 +02:00
docs = tuple(here.rglob("*.json"))
if len(docs) == 0:
2022-05-18 12:02:06 +02:00
print("Aucun fichier JSON ('.json') trouvé.")
2022-05-18 18:29:57 +02:00
for i in docs:
2022-05-18 12:02:06 +02:00
if i.exists() and i.stat().st_size > 0:
2022-05-18 18:29:57 +02:00
data = proc(i)
2022-05-18 12:02:06 +02:00
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é")