This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·88 lines (72 loc) · 3.05 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
#!/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.
#
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import os
import datetime
import logging
import icalendar
from pytz.gae import pytz
import tweepy
ical_url = "http://www.arsenal.com/_scripts/ical.ics?tid=1006&sid=118"
london = pytz.timezone("Europe/London")
class MainHandler(webapp.RequestHandler):
def get(self):
result = urlfetch.fetch(ical_url)
cal = icalendar.Calendar.from_string(result.content)
games = []
today = False
for c in cal.walk('VEVENT'):
try:
if c["LOCATION"] == "Emirates Stadium" and c["DTSTART"].dt.date() >= datetime.date.today():
games.append({"StartTime":c["DTSTART"].dt.astimezone(london), "Summary": c["SUMMARY"].replace("(H)","")})
if c["DTSTART"].dt.date() == datetime.date.today():
today = True
except KeyError:
pass
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, {"games":games, "today":today}))
class TweetHandler(webapp.RequestHandler):
def get(self):
logging.info('Daily tweet')
result = urlfetch.fetch(ical_url)
cal = icalendar.Calendar.from_string(result.content)
games = []
today = False
for c in cal.walk('VEVENT'):
try:
if c["LOCATION"] == "Emirates Stadium" and c["DTSTART"].dt.date() == datetime.date.today():
self.tweet("Arsenal home game today at %s (%s)" % (c["DTSTART"].dt.astimezone(london).strftime("%H:%M"), c["SUMMARY"].replace(" (H)","")))
except KeyError:
pass
self.response.set_status(204)
return self.response
def tweet(self, message):
logging.debug("TWEET: %s", message)
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
auth.set_access_token(twitter_access_key, twitter_access_secret)
api = tweepy.API(auth)
api.update_status(message)
def main():
logging.getLogger().setLevel(logging.DEBUG)
application = webapp.WSGIApplication([('/', MainHandler), ('/tweet/matchday', TweetHandler)],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()