-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathes.py
158 lines (143 loc) · 5.54 KB
/
es.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python3
# SPDX-FileCopyrightText: 2025 Cédric Bosdonnat <[email protected]>
#
# SPDX-License-Identifier: MIT
import click
from datetime import datetime
import sys
import requests
import os.path
import tempfile
import xml.etree.ElementTree as ET
import writer
def parse_xml(xml_file, out_dir):
os.makedirs(out_dir, exist_ok=True)
writers = {
"Precio_x0020_Gasoleo_x0020_A": writer.KmlWriter(
os.path.join(out_dir, "diesel.kml"),
"Spain - Diesel",
"https://sedeaplicaciones.minetur.gob.es",
"yellow"
),
"Precio_x0020_Gasoleo_x0020_Premium": writer.KmlWriter(
os.path.join(out_dir, "diesel-premium.kml"),
"Spain - Diesel Premium",
"https://sedeaplicaciones.minetur.gob.es",
"yellow"
),
"Precio_x0020_Biodiesel": writer.KmlWriter(
os.path.join(out_dir, "biodiesel.kml"),
"Spain - Biodiesel",
"https://sedeaplicaciones.minetur.gob.es",
"yellow"
),
"Precio_x0020_Bioetanol": writer.KmlWriter(
os.path.join(out_dir, "e85.kml"),
"Spain - E85",
"https://sedeaplicaciones.minetur.gob.es",
"cyan"
),
"Precio_x0020_Gasolina_x0020_98_x0020_E10": writer.KmlWriter(
os.path.join(out_dir, "e10.kml"),
"Spain - SP98E10",
"https://sedeaplicaciones.minetur.gob.es",
"lime"
),
"Precio_x0020_Gasolina_x0020_98_x0020_E5": writer.KmlWriter(
os.path.join(out_dir, "sp98.kml"),
"Spain - SP98",
"https://sedeaplicaciones.minetur.gob.es",
"green"
),
"Precio_x0020_Gasolina_x0020_95_x0020_E10": writer.KmlWriter(
os.path.join(out_dir, "sp95e10.kml"),
"Spain - SP95E10",
"https://sedeaplicaciones.minetur.gob.es",
"green"
),
"Precio_x0020_Gasolina_x0020_95_x0020_E5": writer.KmlWriter(
os.path.join(out_dir, "sp95.kml"),
"Spain - SP95",
"https://sedeaplicaciones.minetur.gob.es",
"green"
),
"Precio_x0020_Gasolina_x0020_95_x0020_E5_x0020_Premium": writer.KmlWriter(
os.path.join(out_dir, "sp95-premium.kml"),
"Spain - SP95 Premium",
"https://sedeaplicaciones.minetur.gob.es",
"green"
),
"Precio_x0020_Gases_x0020_licuados_x0020_del_x0020_petróleo": writer.KmlWriter(
os.path.join(out_dir, "gplc.kml"),
"Spain - GPLc",
"https://sedeaplicaciones.minetur.gob.es",
"blue"
),
"Precio_x0020_Gas_x0020_Natural_x0020_Licuado": writer.KmlWriter(
os.path.join(out_dir, "lng.kml"),
"Spain - LNG",
"https://sedeaplicaciones.minetur.gob.es",
"blue"
),
"Precio_x0020_Gas_x0020_Natural_x0020_Comprimido": writer.KmlWriter(
os.path.join(out_dir, "cng.kml"),
"Spain - CNG",
"https://sedeaplicaciones.minetur.gob.es",
"blue"
),
"Precio_x0020_Hidrogeno": writer.KmlWriter(
os.path.join(out_dir, "h.kml"),
"Spain - Hydrogen",
"https://sedeaplicaciones.minetur.gob.es",
"blue"
),
}
# Loop over all stations
ns="{http://schemas.datacontract.org/2004/07/ServiciosCarburantes}"
update_time = ""
for _, element in ET.iterparse(xml_file):
if element.tag == ns+"Fecha":
dt = datetime.strptime(element.text, "%d/%m/%Y %H:%M:%S")
update_time = dt.isoformat()+"Z"
if element.tag == ns+"EESSPrecio":
lat = float(element.find(ns+"Latitud").text.replace(",", "."))
lon = float(element.find(ns+"Longitud_x0020__x0028_WGS84_x0029_").text.replace(",", "."))
timetable = element.find(ns+"Horario").text
vending = timetable == "L-D: 24H"
if element.find(ns+"Tipo_x0020_Venta").text != "P":
# Ignore shops not for individuals
continue
for tag_name, w in writers.items():
price = element.find(ns+tag_name)
if price.text != None:
w.writeStation(price.text, update_time, lon, lat, vending)
for w in writers.values():
w.close()
@click.command()
@click.option("-i", "file_in", help="Gas price XML file to use instead of downloading")
@click.option("-o", "out", help="Directory to write the KML files to", default=".")
def main(file_in, out):
if file_in is None:
with requests.get(
"https://sedeaplicaciones.minetur.gob.es/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestres/",
headers={"Accept": "text/xml"},
stream=True,
) as req:
if req.status_code != 200:
print(f"failed to download data: {req.status_code}", file=sys.stderr)
sys.exit(1)
fd, tmp_xml = tempfile.mkstemp()
try:
with os.fdopen(fd, "wb") as tmp_file:
for chunk in req.iter_content(chunk_size=1024):
if chunk:
tmp_file.write(chunk)
with open(tmp_xml, "r") as xml_file:
parse_xml(xml_file, out)
finally:
os.unlink(tmp_xml)
else:
with open(file_in, "r") as xml_file:
parse_xml(xml_file, out)
if __name__ == '__main__':
main()