-
Notifications
You must be signed in to change notification settings - Fork 1
/
zimmerberg.py
executable file
·137 lines (122 loc) · 3.07 KB
/
zimmerberg.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import csv
import re
import logging
from openerzpy.download import download as dl
from openerzpy.parse import parse_ics
from openerzpy.file import csv_file
__location__ = os.path.realpath(
os.path.join(
os.getcwd(),
os.path.dirname(__file__)
)
)
# Logging
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
logging.captureWarnings(True)
municipalities = {
'adliswil': {
'zip': '8134',
},
'horgen': {
'zip': '8810',
},
'kilchberg': {
'zip': '8802',
},
'langnau': {
'zip': '8135',
},
'richterswil': {
'zip': '8805',
},
'rueschlikon': {
'zip': '8803',
},
'waedenswil': {
'zip': '8820',
},
'oberrieden': {
'zip': '8942',
},
'thalwil': {
'zip': '8800',
},
}
waste_type_map = {
'Kehricht': 'waste',
'Grüngut': 'organic',
'Metall': 'metal',
'Papier': 'paper',
'Karton': 'cardboard',
'Sonderabfallmobil': 'special',
'Grubengut': 'incombustibles',
'Häckseldienst': 'chipping_service',
}
other_categories = [
'Repair',
'Repair Café',
'Repair-Café',
'Herbst-Flohmarkt',
'Frühlings-Flohmarkt',
'Büchermarkt',
'Kinderkleiderbörse Frühling/Sommer',
'Kinderkleiderbörse Herbst/Winter',
'Spielsachenbörse',
'Velobörse',
'Velorbörse',
'Bring- und Holtag',
'Wertstoffsammelstelle',
'Abfälle Räbenchilbi',
'Christbäume',
]
def waste_type(in_type):
try:
return waste_type_map[in_type]
except KeyError:
# try to match the first word
m = re.match(r'(?P<first>[\w]*)(?P<rest>.*)?', in_type)
if not m:
raise
return waste_type_map[m['first']]
def generate_muni_csv(muni, config):
# iCal Download URL
url = f"https://{muni}.entsorglos.swiss/calendar.ics"
cal_path = os.path.join(__location__, f'{muni}_calendar.ics')
log.info(f"Download URL: {url}")
dl.download_file(url, cal_path)
events = parse_ics.parse_file(cal_path)
output_rows = []
for event in events:
m = re.match(r'(?P<art>.*?)( (Zone|Tour) (?P<zone>\w+?)(?P<rest>.*))?$', event['summary'])
if m['art'] in other_categories:
continue
area = ''
if m['zone']:
area = m['zone']
out = {
'region': muni,
'area': area,
'zip': config['zip'],
'col_date': event['start_date'].date().isoformat(),
'waste_type': waste_type(m['art']),
'description': '',
}
output_rows.append(out)
csv_path = os.path.join(__location__, '..', muni, f'{muni}.csv')
csv_file.write_calendar_to_csv(csv_path, output_rows)
try:
for muni, config in municipalities.items():
generate_muni_csv(muni, config)
except Exception:
log.exception("Error in zimmerberg.py")
sys.exit(1)