-
Notifications
You must be signed in to change notification settings - Fork 0
/
huset-bio-ical.py
239 lines (192 loc) · 7.29 KB
/
huset-bio-ical.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import requests
import io
from lxml import html
import datetime
import pytz
from icalendar import Calendar, Event, Timezone, TimezoneDaylight, TimezoneStandard
import re
import os.path
import subprocess
__version__ = '2.1'
def add_vtimezone(cal):
tzc = Timezone()
tzc.add('tzid', 'Europe/Copenhagen')
tzc.add('x-lic-location', 'Europe/Copenhagen')
tzs = TimezoneStandard()
tzs.add('tzname', 'CET')
tzs.add('dtstart', datetime.datetime(1970, 10, 25, 3, 0, 0))
tzs.add('rrule', {'freq': 'yearly', 'bymonth': 10, 'byday': '-1su'})
tzs.add('TZOFFSETFROM', datetime.timedelta(hours=2))
tzs.add('TZOFFSETTO', datetime.timedelta(hours=1))
tzd = TimezoneDaylight()
tzd.add('tzname', 'CEST')
tzd.add('dtstart', datetime.datetime(1970, 3, 29, 2, 0, 0))
tzs.add('rrule', {'freq': 'yearly', 'bymonth': 3, 'byday': '-1su'})
tzd.add('TZOFFSETFROM', datetime.timedelta(hours=1))
tzd.add('TZOFFSETTO', datetime.timedelta(hours=2))
tzc.add_component(tzs)
tzc.add_component(tzd)
cal.add_component(tzc)
base_url = r'https://huset-kbh.dk/'
params = {'taxonomyId':'274', 'page_nr':'10', 'lang':'en'}
ics_filename = 'Huset-Bio.ics'
##search_url = base_url
##oversigt = html.parse(search_url)
rex = re.compile(r'^(\d{2})\.(\d{2}).*?At.*?(\d{1,2}).(\d{2}).*')
def parsetime(timestr):
print ('PT[{}]'.format(timestr))
m = rex.match(timestr)
print ('mm:', m)
print ('groups:', m.lastindex, ':', m.groups())
return (int(x) for x in m.groups())
headers = {
'User-Agent': 'Mozilla/5.0'
}
r = requests.get(base_url, params=params, headers=headers)
def get_element(movie, cssselector):
return movie.cssselect(cssselector)[0]
def get_element_value(movie, cssselector):
return get_element(movie, cssselector).text_content().strip()
def set_movie_status(movie, event):
event_status = movie.cssselect('.ticket-replace-status')
if type(event_status) == list and len(event_status) > 0:
event_status = get_element_value(movie, '.ticket-replace-status')
if event_status == None:
return None
elif event_status == 'Cancelled':
summary = event.get('SUMMARY')
event['SUMMARY'] = '(CANCELLED) ' + summary
event.add('STATUS', 'CANCELLED')
elif event_status == 'Limited Tickets':
summary = event.get('SUMMARY')
event['SUMMARY'] = summary + ' (Limited Tickets)'
elif event_status == 'New date':
None
elif event_status == 'New stage':
None
elif event_status == 'Sold out':
summary = event.get('SUMMARY')
event['SUMMARY'] = '(SOLD OUT) ' + summary
return None
tree = html.fromstring(r.text)
#results = tree.xpath(xpath)'
results = tree.cssselect('#widgets-wrapper')[0].getchildren()
#print ('hugo HUGO', len(results))
cal = Calendar()
#cal = new CalendCalendarar(None)
calName = 'Husets-Bio All Shows'
calTimezone = 'Europe/Copenhagen'
##cal.add('PRODID', '7703f8e4-7a23-402f-bd1d-047656ee3cc7')
cal.add('PRODID', '-//HUSETS-BIOGRAF//NOSGML V' + __version__ + '//EN')
cal.add('version', '2.0')
cal.add('CLASS', 'PUBLIC')
cal.add('url', 'https://services.husets-biograf.dk/calendar/all-shows')
cal.add('name', calName)
cal.add('X-WR-CALNAME', calName)
#DESCRIPTION:A description of my calendar
#X-WR-CALDESC:A description of my calendar
cal.add('TIMEZONE-ID', calTimezone)
cal.add('X-WR-TIMEZONE', calTimezone)
cal.add('REFRESH-INTERVAL;VALUE=DURATION', 'PT2H')
cal.add('X-PUBLISHED-TTL', 'PT2H')
#COLOR:34:50:105
cal.add('CALSCALE', 'GREGORIAN')
##cal.add('METHOD', 'PUBLISH')
local_tz = pytz.timezone(calTimezone)
calSequence = 0
updateSeq = False
idict = {}
if os.path.isfile(ics_filename):
with io.open(ics_filename, encoding="utf-8") as text_file:
#text_file = open(ics_filename, encoding="utf-8")
istr = text_file.read()
#text_file.close()
gcal = Calendar.from_ical(istr)
tmpseq = gcal.get('SEQUENCE')
if not tmpseq is None:
calSequence = tmpseq;
print ('exist SEQ', tmpseq)
for component in gcal.walk():
if component.name == "VEVENT":
uid=component.get('uid')
seq=component.get('sequence')
idict[uid] = seq
count = 0
last_date = 'NO_LAST*DATE'
for movie in results:
#movie = results[1]
event_genre = get_element_value(movie, '.event-genre')
if not event_genre in ['Familie/Børn, Samlingspunkt Indre By', 'Samlingspunkt Indre By']:
#print(html.tostring(movie))
print ()
count += 1
movie_id = movie.get('data-id')
movie_time = get_element_value(movie, '.event-time')
event_name = get_element_value(movie, '.event-name')
event_url = get_element(movie, '.event-desc-text a').get('href')
elem_description = get_element_value(movie, '.event-desc-text p')
event_picture_url = get_element(movie, '.img-responsive').get('src')
#.img-responsive
#.event-desc-text
#.ticket-price
#.ticket-status
print ('ID', movie_id)
print ('TIME', movie_time)
last_date = movie_time
da, md, hh, mm = parsetime(movie_time)
print('GENRE', event_genre)
print('NAME', event_name)
print ('DESC', elem_description)
print ('IMG', event_picture_url)
print ('URL', event_url)
event = Event()
uid = 'Husets-Bio-' + movie_id
seq = 0
if uid in idict:
seq = idict[uid]
seq = seq + 1
updateSeq = True
event.add ('UID', uid)
event.add ('SEQUENCE', seq)
print('UID', uid)
print('SEQUENCE', seq)
event.add('LOCATION', 'Husets-Biograf; Rådhusstræde 13, 1466 København K')
year = int(datetime.datetime.now().strftime("%Y"))
currentmonth = int(datetime.datetime.now().strftime("%m"))
if (currentmonth - 2) > md:
year = year + 1
event.add('dtstart', datetime.datetime(year, md, da, hh, mm, 0, tzinfo=local_tz))
#event.add('dtend', datetime.datetime(2018, 4, 4, 10, 0, 0, tzinfo=local_tz))
event.add('dtstamp', datetime.datetime.now(local_tz))
event.add('LAST-MODIFIED', datetime.datetime.now(pytz.utc))
event.add ('CATEGORIES', [event_genre])
#event.add('COMMENT' 'Kommentar')
event.add ('DESCRIPTION', elem_description)
event.add ('URL', event_url)
event.add ('SUMMARY', event_name)
event_status = set_movie_status(movie, event)
print('STATUS', event_status)
cal.add_component(event)
add_vtimezone(cal)
if updateSeq == True:
calSequence = calSequence + 1
#cal.add('sequence', calSequence)
print ()
print ('##################')
print ('COUNT', count)
print ('last_date', last_date)
print ('DATETIME', datetime.datetime.now())
istr = cal.to_ical()
#print (istr)
t = local_tz.localize(datetime.datetime.now())
t_utc = t.astimezone(pytz.UTC)
print ('t:', t)
print ('t_utc:', t_utc)
text_file = open(ics_filename, 'wb')
text_file.write(istr)
text_file.close()
print()
print('aws s3 cp Huset-Bio.ics s3://husets-bio/Huset-Bio.ics --acl public-read')
rc = subprocess.run(['aws', 's3', 'cp', 'Huset-Bio.ics', 's3://husets-bio/Huset-Bio.ics', '--acl', 'public-read'])
print('rc', rc)
print('validate', 'https://icalendar.org/validator.html?url=https://services.husets-biograf.dk/calendar/all-shows')