This repository has been archived by the owner on Dec 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 482
/
api.py
352 lines (328 loc) · 14.2 KB
/
api.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import logging
import string
import random
import base64
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from v2ex.babel import Member
from v2ex.babel import Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply
from v2ex.babel import Note
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.ext.cookies import Cookies
from django.utils import simplejson as json
template.register_template_library('v2ex.templatetags.filters')
from topic import TOPIC_PAGE_SIZE
class ApiHandler(webapp.RequestHandler):
def write(self, output):
if output is None:
output = ''
callback = self.request.get('callback', None)
if callback:
if not isinstance(output, unicode):
output = output.decode('utf-8')
self.response.headers['Content-type'] = 'application/javascript; charset=utf-8'
output = '%s(%s)' % (callback, output)
else:
self.response.headers['Content-type'] = 'application/json; charset=utf-8'
self.response.out.write(output)
# Site
# /api/site/stats.json
class SiteStatsHandler(ApiHandler):
def get(self):
template_values = {}
template_values['topic_max'] = GetKindByName('Counter', 'topic.max')
template_values['member_max'] = GetKindByName('Counter', 'member.max')
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'site_stats.json')
output = template.render(path, template_values)
self.write(output)
# /api/site/info.json
class SiteInfoHandler(ApiHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'site_info.json')
output = template.render(path, template_values)
self.write(output)
# Nodes
# /api/nodes/all.json
class NodesAllHandler(ApiHandler):
def get(self):
output = memcache.get('api_nodes_all')
if output is None:
site = GetSite()
template_values = {}
template_values['site'] = site
nodes = memcache.get('api_nodes_all')
if nodes is None:
nodes = db.GqlQuery("SELECT * FROM Node")
memcache.set('api_nodes_all', nodes, 3600)
template_values['nodes'] = nodes
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'nodes_all.json')
output = template.render(path, template_values)
memcache.set('api_nodes_all', output, 86400)
self.write(output)
# /api/nodes/show.json
class NodesShowHandler(ApiHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
method_determined = False
parameter_id = self.request.get('id')
if parameter_id:
method_determined = True
if method_determined is not True:
parameter_name = self.request.get('name')
if parameter_name:
method_determined = True
if method_determined is True:
if parameter_id:
node = GetKindByNum('Node', int(parameter_id))
else:
node = GetKindByName('Node', str(parameter_name))
if node is not False:
template_values['node'] = node
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'nodes_show.json')
output = template.render(path, template_values)
self.write(output)
else:
template_values['message'] = 'Node not found'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.write(output)
else:
template_values['message'] = "Required parameter id or name is missing"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.write(output)
# Topics
# /api/topics/latest.json
class TopicsLatestHandler(ApiHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
topics = memcache.get('api_topics_latest')
if topics is None:
topics = db.GqlQuery("SELECT * FROM Topic ORDER BY created DESC LIMIT 20")
memcache.set('api_topics_latest', topics, 120)
template_values['topics'] = topics
template_values['topics_count'] = topics.count()
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'topics_latest.json')
output = template.render(path, template_values)
self.write(output)
# /api/topics/show.json
class TopicsShowHandler(ApiHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
method_determined = False
parameter_id = self.request.get('id')
parameter_username = False
parameter_node_id = False
parameter_node_name = False
if parameter_id:
method_determined = True
if method_determined is False:
parameter_username = self.request.get('username')
if parameter_username:
method_determined = True
if method_determined is False:
parameter_node_id = self.request.get('node_id')
if parameter_node_id:
method_determined = True
if method_determined is False:
parameter_node_name = self.request.get('node_name')
if parameter_node_name:
method_determined = True
if method_determined is False:
template_values['message'] = "Required parameter id, username, node_id or node_name is missing"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(400, 'Bad Request')
self.write(output)
else:
topics = False
topic = False
if parameter_id:
try:
topic = GetKindByNum('Topic', int(parameter_id))
if topic is not False:
topics = []
topics.append(topic)
template_values['topic'] = topic
except:
topics = False
if topics is False:
if parameter_username:
one = GetMemberByUsername(parameter_username)
if one is not False:
topics = db.GqlQuery("SELECT * FROM Topic WHERE member_num = :1 ORDER BY created DESC LIMIT 20", one.num)
template_values['topics'] = topics
if topics is False:
try:
if parameter_node_id:
node = GetKindByNum('Node', int(parameter_node_id))
if node is not False:
topics = db.GqlQuery("SELECT * FROM Topic WHERE node_num = :1 ORDER BY last_touched DESC LIMIT 20", node.num)
template_values['topics'] = topics
except:
topics = False
if topics is False:
if parameter_node_name:
node = GetKindByName('Node', str(parameter_node_name))
if node is not False:
topics = db.GqlQuery("SELECT * FROM Topic WHERE node_num = :1 ORDER BY last_touched DESC LIMIT 20", node.num)
template_values['topics'] = topics
if topic or topics:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'topics_show.json')
output = template.render(path, template_values)
self.write(output)
else:
template_values['message'] = "Failed to get topics"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(400, 'Bad Request')
self.write(output)
# /api/topics/create.json
class TopicsCreateHandler(webapp.RequestHandler):
def post(self):
authenticated = False
if 'Authorization' in self.request.headers:
auth = self.request.headers['Authorization']
decoded = base64.b64decode(auth[6:])
authenticated = True
if authenticated:
self.response.out.write('OK')
else:
site = GetSite()
template_values = {}
template_values['site'] = site
template_values['message'] = "Authentication required"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(401, 'Unauthorized')
self.response.headers['Content-type'] = 'application/json'
self.response.headers['WWW-Authenticate'] = 'Basic realm="' + site.domain + '"'
self.response.out.write(output)
# Replies
# /api/replies/show.json
class RepliesShowHandler(ApiHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
method_determined = False
topic_id = self.request.get('topic_id')
page = self.request.get('page', 1)
page_size = TOPIC_PAGE_SIZE
if topic_id:
page_start = (int(page) - 1) * page_size
replies = db.GqlQuery("SELECT * FROM Reply WHERE topic_num = :1 ORDER BY created ASC LIMIT " + str(page_start) + "," + str(page_size), int(topic_id))
if replies:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'replies_show.json')
template_values['replies'] = replies
output = template.render(path, template_values)
self.write(output)
else:
template_values['message'] = "Failed to get replies"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(400, 'Bad Request')
self.write(output)
else:
template_values['message'] = "Required parameter topic_id is missing"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(400, 'Bad Request')
self.write(output)
# Members
# /api/members/show.json
class MembersShowHandler(ApiHandler):
def get(self):
site = GetSite()
template_values = {}
template_values['site'] = site
username = self.request.get('username')
if username:
one = GetMemberByUsername(username)
if one is not False:
if one.avatar_mini_url:
if (one.avatar_mini_url[0:1] == '/'):
one.avatar_mini_url = 'http://' + site.domain + one.avatar_mini_url
one.avatar_normal_url = 'http://' + site.domain + one.avatar_normal_url
one.avatar_large_url = 'http://' + site.domain + one.avatar_large_url
template_values['member'] = one
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'members_show.json')
output = template.render(path, template_values)
self.write(output)
else:
template_values['message'] = "Member not found"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(400, 'Bad Request')
self.write(output)
else:
template_values['message'] = "Required parameter username is missing"
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'error.json')
output = template.render(path, template_values)
self.response.set_status(400, 'Bad Request')
self.write(output)
class CurrencyHandler(ApiHandler):
def get(self):
codes = ['EUR', 'JPY', 'CNY', 'CHF', 'AUD', 'TWD', 'CAD', 'GBP', 'HKD', 'MYR', 'NZD', 'PHP', 'SGD', 'THB']
template_values = {}
o = memcache.get('currency.json')
if o is not None:
pass
else:
for code in codes:
url = 'http://www.google.com/ig/calculator?hl=en&q=1USD=?' + code
response = urlfetch.fetch(url)
m = re.findall('rhs: "([0-9\.]+)', response.content)
if len(m) > 0:
value = m[0].strip().replace(' ', '')
else:
value = 0
template_values[code.lower()] = value
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'currency.json')
o = template.render(path, template_values)
memcache.set('currency.json', o, 86400)
self.write(o)
def main():
application = webapp.WSGIApplication([
('/api/site/stats.json', SiteStatsHandler),
('/api/site/info.json', SiteInfoHandler),
('/api/nodes/all.json', NodesAllHandler),
('/api/nodes/show.json', NodesShowHandler),
('/api/topics/latest.json', TopicsLatestHandler),
('/api/topics/show.json', TopicsShowHandler),
('/api/topics/create.json', TopicsCreateHandler),
('/api/replies/show.json', RepliesShowHandler),
('/api/members/show.json', MembersShowHandler),
('/api/currency.json', CurrencyHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()