forked from simonw/json-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsontime.py
74 lines (64 loc) · 2.39 KB
/
jsontime.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
from google.appengine.ext import webapp
import logging, datetime
def remove_www(handler):
if handler.request.url.startswith('http://www.'):
handler.redirect(handler.request.url.replace('http://www.', 'http://'))
def makeStatic(template_name, context={}):
class Static(webapp.RequestHandler):
def get(self):
remove_www(self)
self.response.headers['Content-Type'] = 'text/html'
self.response.out.write(
render(template_name, context)
)
return Static
import re
# Allow 'abc' and 'abc.def' but not '.abc' or 'abc.'
valid_callback = re.compile('^\w+(\.\w+)*$')
import pytz
class JsonTime(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/javascript'
callback = self.request.get('callback', default_value='')
tz = self.request.get('tz', default_value = 'UTC')
try:
timezone = pytz.timezone(tz)
json = (
"{'datetime': '%s', 'tz': '%s', 'error': false}" % (
datetime.datetime.utcnow().replace(
tzinfo = pytz.utc
).astimezone(timezone).strftime(
'%a, %d %b %Y %H:%M:%S %z'
),
timezone.zone
)
)
except pytz.UnknownTimeZoneError:
json = "{'error': 'Unknown Timezone'}"
if callback and valid_callback.match(callback):
json = '%s(%s)' % (callback, json)
self.response.out.write(json)
class Http404Page(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/html'
self.error(404)
self.response.out.write(render('404.html', {
'path': self.request.path,
}))
def main():
logging.getLogger().setLevel(logging.DEBUG)
from wsgiref.handlers import CGIHandler
application = webapp.WSGIApplication([
('/', makeStatic('index.html')),
('/time.json', JsonTime),
('/.*$', Http404Page),
], debug=True)
CGIHandler().run(application)
import os
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
from google.appengine.ext.webapp import template
def render(template_name, context={}):
path = os.path.join(template_dir, template_name)
return template.render(path, context)
if __name__ == "__main__":
main()