2024-06-04 15:18:41 +01:00

78 lines
1.8 KiB
Python

from requests import get
from bs4 import BeautifulSoup
from chardet import detect
import csv
from json import dumps
ROOT = "http://192.168.2.20"
# Turns a csv reader into a list of dictionaries(json)
def reader_to_json(reader:csv.DictReader) -> list[dict]:
arr = []
for row in reader:
# No whitespaces
row = {k.strip():v.strip() for k,v in row.items()}
arr.append(row)
return arr
# Gets a csv file from the server and turns it into a list of dictionaries(json)
def get_json(url:str) -> list[dict]:
# get the file
raw = get(url).content
print(raw)
# Figure out the encoding
encoding = detect(raw)['encoding']
raw = raw.decode('utf-8')
# turn it into json
reader = csv.DictReader(raw)
arr = reader_to_json(reader)
return arr
# DEBUG function. Loads a local file
def local_file(path:str) -> list[dict]:
with open(path, "r") as i:
reader:list = reader_to_json(csv.DictReader(i))
with open("out.json", "w") as o:
o.write(dumps(reader, indent=2))
return reader
# Gets file from the server
def get_file(entries:int) -> dict:
url = f"{ROOT}/Portal/Portal.mwsl?PriNav=DataLog&RecentCount={entries}"
soup = BeautifulSoup(get(url).content, "html.parser")
link = soup.find(id="link1")
if link:
link = link.get("href")
link = f"{ROOT}{link}"
return get_json(link)
raise ValueError("No link found")
# DEBUG: Get file locally
# return local_file("test.csv")
# Saves a json file
def save_file(json:list[dict], filename:str) -> None:
try:
with open(filename, "w") as o:
o.write(dumps(json, indent=2))
except Exception as exc:
raise IndexError("Error saving file") from exc
def main():
file = get_file(10)
# DEBUG: local test file
#file = local_file("test.csv")
# Save file
save_file(file, "out.json")
if __name__ == "__main__":
main()