-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
89 lines (69 loc) · 2.39 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
import os, csv, cStringIO
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from django.utils import simplejson
class CalculatePage(webapp.RequestHandler):
def get(self, number, duration):
self.response.headers['Content-Type'] = 'text/plain'
key = ''
# Check the parameters
if not number:
self.response.out.write('{"status": "ERROR"}')
return
if not duration:
duration = 1
# Search longest matching prefix
for i in range(1, len(number)):
if memcache.get(number[0:i]):
key = number[0:i]
if not key:
self.response.out.write('{"status": "ERROR"}')
return
data = simplejson.loads(memcache.get(key))
data['status'] = 'OK'
data['duration'] = duration
data['total_rate'] = str(float(duration) * float(data['rate']))
self.response.out.write(simplejson.dumps(data))
class MainPage(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, {}))
class UpdatePage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
rates_url = 'http://www.twilio.com/resources/rates/international-rates.csv'
rates_data = ''
# Get the data from Twilio
try:
remote_data = urlfetch.fetch(rates_url)
if remote_data.status_code != 200:
raise Exception('')
rates_csv = remote_data.content
rates_data = csv.reader(cStringIO.StringIO(rates_csv))
except:
self.response.out.write('{"status": "ERROR"}')
return
# Save the data into memcache (expire in 10 days)
for row in rates_data:
country = row[0]
rate = row[1]
numbers = row[2].split(' ')
d = {}
for number in numbers:
d[number] = '{"country": "' + country + '", "rate": "' + rate + '"}'
#memKey = number
#memVal = '{"country": "' + country + '", "rate": "' + rate + '"}'
#memcache.set(key = memKey, value = memVal, time = 864000)
memcache.set_multi(d, time = 864000)
self.response.out.write('{"status": "OK"}')
application = webapp.WSGIApplication([('/update', UpdatePage),
(r'/calculate/([0-9]*)/?([0-9]*)', CalculatePage),
('/.*', MainPage)
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()