forked from melice/akiraguru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
286 lines (267 loc) · 12 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
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import string
import random
import base64
from google.appengine.ext import webapp
from google.appengine.api import memcache
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
template.register_template_library('v2ex.templatetags.filters')
# Site
# /api/site/stats.json
class SiteStatsHandler(webapp.RequestHandler):
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.response.headers['Content-type'] = 'application/json'
self.response.out.write(output)
# /api/site/info.json
class SiteInfoHandler(webapp.RequestHandler):
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.response.headers['Content-type'] = 'application/json'
self.response.out.write(output)
# Nodes
# /api/nodes/all.json
class NodesAllHandler(webapp.RequestHandler):
def get(self):
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)
self.response.headers['Content-type'] = 'application/json'
self.response.out.write(output)
# /api/nodes/show.json
class NodesShowHandler(webapp.RequestHandler):
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.response.headers['Content-type'] = 'application/json'
self.response.out.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.response.headers['Content-type'] = 'application/json'
self.response.out.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.response.headers['Content-type'] = 'application/json'
self.response.out.write(output)
# Topics
# /api/topics/latest.json
class TopicsLatestHandler(webapp.RequestHandler):
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.response.headers['Content-type'] = 'application/json'
self.response.out.write(output)
# /api/topics/show.json
class TopicsShowHandler(webapp.RequestHandler):
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.response.headers['Content-type'] = 'application/json'
self.response.out.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.response.headers['Content-type'] = 'application/json'
self.response.out.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.response.headers['Content-type'] = 'application/json'
self.response.out.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)
# Users
# /api/members/show.json
class MembersShowHandler(webapp.RequestHandler):
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.response.headers['Content-type'] = 'application/json'
self.response.out.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.response.headers['Content-type'] = 'application/json'
self.response.out.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.response.headers['Content-type'] = 'application/json'
self.response.out.write(output)
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/members/show.json', MembersShowHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()