-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (156 loc) · 7.11 KB
/
main.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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import path_fix
import os
import webapp2
import pytz
import datetime
from BeautifulSoup import BeautifulSoup
import json
import urllib2
from pytz import timezone
from icalendar import Calendar, Event, LocalTimezone
from datetime import datetime, timedelta
import jinja2
import logging
from roomlookup import roomlookupdict
EVENING_TALK_CODE = 40
MEMBERSHIP_EVENT_CODE = 45
DAILY_TOUR_CODE = 41
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class caldict(dict):
def pk(self):
return self['uid']
def days_delta(n):
return timedelta(days=n)
def cal_demo(queryset):
cal = Calendar()
cal.add('version', '2.0')
cal.add('prodid', '-//V and A upcoming events//www.vam.ac.uk.com/whatson/')
cal.add('X-WR-CALNAME','V and A Events Calendar' )
# lt = LocalTimezone() # we append the local timezone to each time so that icalendar will convert
# to UTC in the output
lt = pytz.timezone('Europe/London')
for ent in queryset:
event = Event()
event.add('summary', ent['event_name'])
event.add('dtstart', datetime.combine(ent['event_date'],ent['start_time']).replace(tzinfo=lt))
event.add('dtend', datetime.combine(ent['stop_date'],ent['stop_time']).replace(tzinfo=lt))
event.add('dtstamp', ent['updated_on'].replace(tzinfo=lt))
event['uid'] = ent.pk # should probably use a better guid than just the PK
event.add('description', ent['description'])
cal.add_component(event)
return cal.to_ical()
def get_first_int_in_list(src_list):
for i in src_list:
try:
return(int(i))
except ValueError:
pass
def get_id_from_url(path):
links_eles= path.split('/')
return (get_first_int_in_list(links_eles))
def output_urls(idlist):
r= ''
for id in idlist:
r += '<li><a href="http://www.vam.ac.uk/whatson/event/%s/">%s</a></li>' % (id,id)
r = '<ul>%s</ul>' % r
return r
class MainHandler(webapp2.RequestHandler):
''' prints to screen the cal
'''
def get(self):
for daycount in range(0,28):
scandate = (datetime.now() + days_delta(daycount)).strftime("%Y%m%d")
self.response.write('<h2>%s</h2>' % scandate )
urlpath = "http://www.vam.ac.uk/whatson/json/events/day/%s/" % scandate
response = urllib2.urlopen(urlpath)
data = json.load(response)
for d in data:
if d['fields']['event_type'] not in [EVENING_TALK_CODE, DAILY_TOUR_CODE, MEMBERSHIP_EVENT_CODE, 24 ]:
continue
if 'TOUR' in d['fields']['short_description']:
continue
# self.response.write(d['pk'])
# self.response.write(d)
self.response.write('<img src="http://www.vam.ac.uk/whatson/media/%s" style="width:250px;"><br>' % d['fields']['image'])
for f in ['name','first_slot','last_slot', 'short_description','event_type',
'free','image','location']:
self.response.write('%s: %s' % (f, d['fields'][f]))
dur = datetime.strptime(d['fields']['last_slot'], '%Y-%m-%d %H:%M:%S') - datetime.strptime(d['fields']['first_slot'], '%Y-%m-%d %H:%M:%S')
self.response.write('<br>' )
pass
self.response.write('<br>duration: %s' % dur )
try:
self.response.write('<br>location: %s' % roomlookupdict[str(d['fields']['location'])]['loc_name'] )
except KeyError:
self.response.write('location: ?')
# if dur > timedelta(days=5):
# self.response.write('<br>long event!' )
self.response.write('<hr>')
class CalHandler(webapp2.RequestHandler):
def get(self):
qs = []
for daycount in range(0,31):
scandate = (datetime.now() + days_delta(daycount)).strftime("%Y%m%d")
# self.response.write('<h2>%s</h2>' % scandate )
urlpath = "http://www.vam.ac.uk/whatson/json/events/day/%s/" % scandate
response = urllib2.urlopen(urlpath)
data = json.load(response)
for d in data:
if d['fields']['event_type'] not in [EVENING_TALK_CODE, DAILY_TOUR_CODE, MEMBERSHIP_EVENT_CODE ]:
continue
if 'tour' in d['fields']['short_description'].lower():
continue
if 'tour' in d['fields']['name'].lower():
continue
dur = datetime.strptime(d['fields']['last_slot'], '%Y-%m-%d %H:%M:%S') - \
datetime.strptime(d['fields']['first_slot'], '%Y-%m-%d %H:%M:%S')
if dur > timedelta(days=5):
continue
e1 = caldict()
e1['uid'] = d['fields']['peo_code']
e1['event_name'] = d['fields']['name']
logging.info(e1['event_name'])
logging.info('dur %s' % dur)
logging.info(d['fields']['first_slot'])
logging.info(d['fields']['last_slot'])
url = 'http://www.vam.ac.uk/whatson/event/%s' % d['pk']
try:
loc = roomlookupdict[str(d['fields']['location'])]['loc_name']
except KeyError:
loc = 'code: %s' % d['fields']['location']
e1[ 'event_date'] = datetime.strptime(d['fields']['first_slot'], '%Y-%m-%d %H:%M:%S')
e1[ 'description'] = '%s \n %s \n %s' % (d['fields']['long_description'], url, loc)
e1[ 'start_time'] =datetime.strptime(d['fields']['first_slot'], '%Y-%m-%d %H:%M:%S').time()
e1[ 'stop_date'] = datetime.strptime(d['fields']['last_slot'], '%Y-%m-%d %H:%M:%S')
e1[ 'stop_time'] =datetime.strptime(d['fields']['last_slot'], '%Y-%m-%d %H:%M:%S').time()
if e1['start_time'] == e1[ 'stop_time']:
logging.info('no duration')
e1[ 'stop_time'] = (e1['stop_date'] + timedelta(hours=1)).time()
e1[ 'updated_on'] =datetime.now()
qs.append(e1)
self.response.write(cal_demo(qs))
class HomeHandler(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('templates/main.html')
self.response.out.write(template.render({}))
app = webapp2.WSGIApplication([
('/cal', MainHandler),
('/ical', CalHandler),
('.*', HomeHandler)
], debug=True)