-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
93 lines (77 loc) · 3.07 KB
/
database.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
from pathlib import Path
from json import load
import sqlite3
def load_json(file):
f = open(file, encoding="utf8")
return load(f)
station_path = (
Path(__file__)
.parent.absolute()
.joinpath("./out/json/brands/stations_ARAL Tankstelle_min.json")
)
output_path = Path(__file__).parent.absolute().joinpath("./out/json/other/aral.db")
station_query = (
"CREATE TABLE IF NOT EXISTS `stations`(id INTEGER PRIMARY KEY, name TEXT NOT NULL, lat FLOAT NOT "
"NULL, lng FLOAT NOT NULL, address TEXT NOT NULL, city TEXT NOT NULL, state TEXT, postcode INTEGER "
"NOT NULL, country_code TEXT NOT NULL, telephone TEXT NOT NULL, site_brand TEXT NOT NULL, "
"watchlist_id INTEGER NOT NULL, website TEXT NOT NULL, fuel TEXT NOT NULL, facilities TEXT NOT NULL);"
)
fuel_query = (
"CREATE TABLE IF NOT EXISTS `fuel`(id INTEGER PRIMARY KEY, name TEXT NOT NULL);"
)
facilities_query = "CREATE TABLE IF NOT EXISTS `facilities`(id INTEGER PRIMARY KEY, name TEXT NOT NULL);"
connection = sqlite3.connect(output_path)
cursor = connection.cursor()
cursor.execute(station_query)
cursor.execute(fuel_query)
cursor.execute(facilities_query)
connection.commit()
def export_facilities():
station_data = load_json(station_path)
unique_facilities = []
for station in station_data:
for facility in station["facilities"]:
if facility not in unique_facilities:
unique_facilities.append(facility)
cursor.execute("INSERT INTO facilities (name) VALUES(?)", (facility,))
def export_fuel():
station_data = load_json(station_path)
unique_fuel = []
for station in station_data:
for fuel in station["products"]:
if fuel not in unique_fuel:
unique_fuel.append(fuel)
cursor.execute("INSERT INTO fuel (name) VALUES(?)", (fuel,))
def export_stations():
station_data = load_json(station_path)
for station in station_data:
try:
cursor.execute(
"INSERT INTO stations (id,name,lat,lng,address,city,state,postcode,country_code,telephone,site_brand,"
"watchlist_id,website,fuel,facilities) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
(
station["watchlist_id"],
station["name"],
station["lat"],
station["lng"],
station["address"],
station["city"],
station["state"],
station["postcode"],
station["country_code"],
station["telephone"],
station["site_brand"],
station["watchlist_id"],
station["website"],
", ".join(station["products"]),
", ".join(station["facilities"]),
),
)
except sqlite3.IntegrityError:
print(f"error while inserting {station['id']}")
if __name__ == "__main__":
export_facilities()
export_fuel()
export_stations()
connection.commit()