-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.py
50 lines (46 loc) · 1.96 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Speichert die Daten, die tankstellen_crawler extrehiert hat,
in station_data.json.gz (stammdaten) und timestamp.gz (marktdaten)
"""
import time
import json
import gzip
def save_data(station_data_filename, spritpreis_daten):
added = 0
modified = 0
deleted = 0
history_only = {link: {"history": details["history"], "timestamp": details["timestamp"]}
for (link, details) in spritpreis_daten.items()}
print("History extracted")
filename = "{}".format(time.time()) + ".json.gz"
with gzip.open(filename, 'wt', encoding="ascii") as outfile:
json.dump(history_only, outfile)
print("History dumped")
try:
with gzip.open(station_data_filename, 'rt', encoding='ascii') as infile:
old_station_data = json.load(infile)
except FileNotFoundError:
old_station_data = {}
print("Old data loaded")
station_data = {link: {details["timestamp"]: details["station_data"]}
for (link, details) in spritpreis_daten.items()}
print("Station Data extracted")
for link in old_station_data.keys():
if link not in station_data:
max_available_ts = max(old_station_data[link].keys())
old_station_data[link][max_available_ts]["deleted"] = "deleted"
deleted += 1
print("Deleted: ", deleted)
for link, station_detail in station_data.items():
if link in old_station_data:
max_available_ts = max(old_station_data[link].keys())
if old_station_data[link][max_available_ts] != list(station_detail.values())[0]:
old_station_data[link][list(station_detail.keys())[0]] = list(station_detail.values())[0]
modified += 1
else:
old_station_data[link] = station_detail
added += 1
print("Modified: ", modified)
print("Added: ", added)
with gzip.open(station_data_filename, 'wt', encoding='ascii') as outfile:
json.dump(old_station_data, outfile)