-
Notifications
You must be signed in to change notification settings - Fork 1
/
Zuerich.py
71 lines (54 loc) · 1.79 KB
/
Zuerich.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
import feedparser
from park_api.geodata import GeoData
# Falls das hier jemals einer von den Menschen
# hinter OpenDataZürich lesen sollte: Ihr seid so toll <3
geodata = GeoData(__file__)
def parse_html(xml_data):
feed = feedparser.parse(xml_data)
last_updated = feed["entries"][0]["updated"]
data = {
"lots": [],
# remove trailing timezone for consensistency
"last_updated": last_updated.replace("Z", "")
}
for entry in feed["entries"]:
summary = parse_summary(entry["summary"])
title_elements = parse_title(entry["title"])
lot_identifier = (title_elements[2] + " " + title_elements[0]).strip()
lot = geodata.lot(lot_identifier)
data["lots"].append({
"name": title_elements[0],
"address": title_elements[1],
"id": lot.id,
"state": summary[0],
"free": summary[1],
"total": lot.total,
"coords": lot.coords,
"forecast": False,
"lot_type": title_elements[2]
})
return data
def parse_summary(summary):
"""Parse a string from the format 'open / 41' into both its params"""
summary = summary.split("/")
summary[0] = summary[0].strip()
if "?" in summary[0]:
summary[0] = "nodata"
try:
summary[1] = int(summary[1])
except ValueError:
summary[1] = 0
return summary
def parse_title(title):
"""
Parse a string from the format 'Parkgarage am Central / Seilergraben'
into both its params
"""
types = ["Parkhaus", "Parkplatz"]
name = title.split(" / ")[0]
address = title.split(" / ")[1]
type = ""
if name.split(" ")[0] in types:
type = name.split(" ")[0]
name = " ".join(name.split(" ")[1:])
return name, address, type