-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
114 lines (99 loc) · 3.79 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
#!/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.
#
__author__ = 'Waseem Ahmad <[email protected]>'
import datetime
import json
import logging
import utils
import urllib
import webapp2
from google.appengine.ext import db
from google.appengine.api import taskqueue
class Routes(db.Model):
time_stamp = db.DateTimeProperty()
data = db.TextProperty()
class Buses(db.Model):
time_stamp = db.DateTimeProperty()
data = db.TextProperty()
class RoutesRecorder(webapp2.RequestHandler):
def post(self):
data = urllib.urlopen('http://bus.rice.edu/json/routes.php').read()
now_rounded = utils.roundTime(datetime.datetime.now(), roundTo=1)
Routes(time_stamp=now_rounded, data=data).put()
class RoutesHandler(webapp2.RequestHandler):
def get(self):
# Fetch routes data from a minute ago
routes = None
minute_ago = utils.roundTime(
datetime.datetime.now() - datetime.timedelta(seconds=60),
roundTo=1)
retries = 0
while not routes and retries < 10:
routes = Routes.gql('WHERE time_stamp=:1', minute_ago).get()
minute_ago += datetime.timedelta(seconds=1)
retries += 1
if routes:
self.response.headers['Content-Type'] = ' application/json'
self.response.out.write(routes.data)
else:
self.response.out.write('Couldn\'t find data. Sorry!')
class BusesRecorder(webapp2.RequestHandler):
def post(self):
data = urllib.urlopen('http://bus.rice.edu/json/buses.php').read()
now_rounded = utils.roundTime(datetime.datetime.now(), roundTo=1)
Buses(time_stamp=now_rounded, data=data).put()
class BusesHandler(webapp2.RequestHandler):
def get(self):
# Fetch buses data from a minute ago
buses = None
minute_ago = utils.roundTime(
datetime.datetime.now() - datetime.timedelta(seconds=60),
roundTo=1)
retries = 0
while not buses and retries < 10:
buses = Buses.gql('WHERE time_stamp=:1', minute_ago).get()
minute_ago += datetime.timedelta(seconds=1)
retries += 1
if buses:
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(buses.data)
else:
self.response.out.write('Couldn\'t find data. Sorry!')
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello curious person!')
self.response.write('There\'s not much here! Goto https://github.com/'
'rice-university/rice-bus-tracker')
class RecorderScheduler(webapp2.RequestHandler):
"""
Schedules 60 recording tasks per recorder every minute through cron job,
so that a record is made every second.
"""
def get(self):
for i in range(60):
for url in ['/record/routes', '/record/buses']:
taskqueue.add(url=url,
countdown=i)
logging.info('Enqueued 60 record tasks.')
app = webapp2.WSGIApplication([
('/', MainHandler),
('/buses', BusesHandler),
('/routes', RoutesHandler),
('/record/routes', RoutesRecorder),
('/record/buses', BusesRecorder),
('/tasks/recorder', RecorderScheduler)
], debug=True)