mySpindel/LittleBock/extract.py

38 lines
1011 B
Python
Raw Normal View History

2022-05-06 17:33:17 +02:00
#!/usr/bin/python3
"""
extracteur de données iSpindel JSON entreposées par Little Bock en HTML
"""
import json
try:
import lxml.html as LX
except ModuleNotFoundError as e:
import sys
print("Le module 'lxml' est nécessaire.\n http://pypi.org/lxml")
sys.exit()
import pathlib
def proc(path):
s, js = None, None
h = LX.parse(path.name)
x = h.xpath("//*[@id='fermentation_log_chart']")
if x: s = x[0].get('data-chart-options')
if s:
js = json.dumps(
json.loads(s).pop('series'), indent=4, sort_keys=True) or None
if js:
p = path.with_suffix('.json')
with open(p,'w') as f:
f.write(js)
print(f"INFO: extraction des données dans {p.name}.")
if __name__ == "__main__":
here = pathlib.Path.cwd()
hdocs = tuple(here.glob("*.html"))
if len(hdocs) == 0:
print("Aucun fichier HTML ('.html') trouvé.")
for i in hdocs:
if i.exists() and i.stat().st_size > 0:
proc(i)