forked from youtify/youtify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.py
107 lines (77 loc) · 3.49 KB
/
users.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
import webapp2
from google.appengine.ext.webapp import util
from model import get_youtify_user_model_by_id_or_nick
from model import get_youtify_user_struct
from model import get_playlist_structs_for_youtify_user_model
from model import get_followers_for_youtify_user_model
from model import get_followings_for_youtify_user_model
from model import get_activities_structs
import json as simplejson
class ActivitiesHandler(webapp2.RequestHandler):
def get(self, id_or_nick):
"""Get activities for user as JSON"""
youtify_user_model = get_youtify_user_model_by_id_or_nick(id_or_nick)
verbs = self.request.get('verbs', None)
type = self.request.get('type', None)
count = self.request.get('count', None)
if verbs:
verbs = verbs.split(',')
if count:
count = int(count)
if youtify_user_model is None:
self.error(404)
return
ret = get_activities_structs(youtify_user_model, verbs, type, count)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(ret))
class FollowersHandler(webapp2.RequestHandler):
def get(self, id_or_nick):
"""Get followers for user as JSON"""
youtify_user_model = get_youtify_user_model_by_id_or_nick(id_or_nick)
if youtify_user_model is None:
self.error(404)
return
ret = get_followers_for_youtify_user_model(youtify_user_model)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(ret))
class FollowingsHandler(webapp2.RequestHandler):
def get(self, id_or_nick):
"""Get followings for user as JSON"""
youtify_user_model = get_youtify_user_model_by_id_or_nick(id_or_nick)
if youtify_user_model is None:
self.error(404)
return
ret = get_followings_for_youtify_user_model(youtify_user_model)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(ret))
class PlaylistsHandler(webapp2.RequestHandler):
def get(self, id_or_nick):
"""Get playlists for user as JSON"""
youtify_user_model = get_youtify_user_model_by_id_or_nick(id_or_nick)
if youtify_user_model is None:
self.error(404)
return
ret = get_playlist_structs_for_youtify_user_model(youtify_user_model)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(ret))
class UserHandler(webapp2.RequestHandler):
def get(self, id_or_nick):
"""Get user as JSON"""
youtify_user_model = get_youtify_user_model_by_id_or_nick(id_or_nick)
youtify_user_struct = None
if youtify_user_model is None:
self.error(404)
return
youtify_user_struct = get_youtify_user_struct(youtify_user_model)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(simplejson.dumps(youtify_user_struct))
def post(self):
"""Update user"""
self.error(500)
app = webapp2.WSGIApplication([
('/api/users/(.*)/activities', ActivitiesHandler),
('/api/users/(.*)/followers', FollowersHandler),
('/api/users/(.*)/followings', FollowingsHandler),
('/api/users/(.*)/playlists', PlaylistsHandler),
('/api/users/(.*)', UserHandler),
], debug=True)