-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
93 lines (72 loc) · 2.54 KB
/
generator.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 datetime import datetime
from pathlib import Path
from json import load, dumps
import argparse
empty_sitemap_start = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"""
empty_sitemap_end = "</urlset>"
empty_site = """
<url>
<loc>{}</loc>
<lastmod>{}</lastmod>
<changefreq>weekly</changefreq>
</url>"""
base_url = "https://mein-auto-tanken.de/station/{}"
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/sitemap.xml")
output_path_facilities = (
Path(__file__).parent.absolute().joinpath("./out/json/other/facilities.json")
)
output_path_fuel = (
Path(__file__).parent.absolute().joinpath("./out/json/other/fuel.json")
)
def generate_sitemap():
station_data = load_json(station_path)
now = datetime.now().strftime("%Y-%m-%dT00:00:00+00:00")
with open(output_path, "w+") as f:
f.write(empty_sitemap_start)
for station in station_data:
f.write(empty_site.format(base_url.format(station["id"]), now))
f.write(empty_sitemap_end)
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)
with open(output_path_facilities, "w+") as f:
f.write(dumps(unique_facilities, indent=4))
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)
with open(output_path_fuel, "w+") as f:
f.write(dumps(unique_fuel))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="generator")
parser.add_argument("-s", "--sitemap", help="generate sitemap", action="store_true")
parser.add_argument(
"-f", "--facilities", help="generate unique facilities", action="store_true"
)
parser.add_argument(
"-ff", "--fuel", help="generate unique fuel types", action="store_true"
)
args = parser.parse_args()
if args.sitemap:
generate_sitemap()
if args.facilities:
export_facilities()
if args.fuel:
export_fuel()