-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuelwatchwa-to-json.py
60 lines (52 loc) · 1.67 KB
/
fuelwatchwa-to-json.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
# Import required libraries
import datetime
import json
import xmltodict
import urllib.request
import shutil
# Store dates
todayDate = datetime.date.today()
tomorrowDate = todayDate + datetime.timedelta(days=1)
# Define FuelWatch RSS feed variables
rssBase = "http://www.fuelwatch.wa.gov.au/fuelwatch/fuelWatchRSS"
Product = {
"Unleaded Petrol": 1,
"Premium Unleaded": 2,
"Diesel": 4,
"LPG": 5,
"98 RON": 6,
"E85": 10,
"Brand diesel": 11
}
StateRegion = {
"Gascoyne": 1,
"Goldfields-Esperance": 2,
"Great Southern": 3,
"Kimberley": 4,
"Mid-West": 5,
"Peel": 6,
"Pilbara": 7,
"South-West": 8,
"Wheatbelt": 9,
"Metro": 98
}
Day = {
"today": "today",
"tomorrow": "tomorrow", # Only available after 2:30PM
"yesterday": "yesterday"
}
# Loop through all Products and State Regions and get tomorrow's prices
for x in Product:
for y in StateRegion:
rssURL = rssBase + "?Product=" + str(Product[x]) + "&StateRegion=" + str(StateRegion[y]) + "&Day=" + Day["tomorrow"]
# Open FuelWatch RSS feed and save as XML file
pathXML = "files/xml/" + str(tomorrowDate) + "/fuelWatchRSS_" + x + "_" + y + ".xml"
with urllib.request.urlopen(rssURL) as response, open(pathXML, "wb") as saveXML:
shutil.copyfileobj(response, saveXML)
# Open FuelWatch XML file and save as JSON file
pathJSON = "files/json/" + str(tomorrowDate) + "/fuelWatchRSS_" + x + "_" + y + ".json"
with open(pathXML) as inXML:
inJSON = json.dumps(xmltodict.parse(inXML.read()), indent=4)
saveJSON = open(pathJSON, "w")
saveJSON.write(inJSON)
saveJSON.close()