-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
209 lines (174 loc) · 6.62 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
#!/usr/bin/env python3
import json
from wsgiref import simple_server
import falcon
import youtube_dl
from pymongo import MongoClient
from pymongo.errors import DuplicateKeyError
from bson.json_util import dumps
from bson.objectid import ObjectId
from bson.errors import InvalidId
from config import *
"""
MusicSync backend http api implementation
"""
class BSONDumps(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, ObjectId):
return str(obj)
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)
dumps = BSONDumps().encode
class BaseResponse():
"""Base resource class"""
def __init__(self, db):
self.db = db
class PlaylistsCollection(BaseResponse):
"""Operations on playlists collection"""
def on_get(self, req, resp):
"""Returns list of all playlists"""
resp.status = falcon.HTTP_200
resp.body = dumps(list(self.db["playlists"].find()))
def on_post(self, req, resp):
"""Create a new playlist"""
data = json.loads(req.stream.read().decode('utf-8'))
try:
info = youtube_dl.YoutubeDL({}).extract_info(data["_id"], download=False, process=False)
pl = {
"_id": info["id"],
"title": info["title"],
"type": "YoutubePlaylist"
}
except Exception as ex:
pl = {
"title": data["title"],
"type": "SimplePlaylist"
}
result = self.db["playlists"].insert_one(pl)
pl["_id"] = str(result.inserted_id)
resp.status = falcon.HTTP_201
resp.body = json.dumps(pl, indent=2)
class PlaylistResource(BaseResponse):
"""Operations on one playlist"""
def on_get(self, req, resp, playlist_id):
"""Return a playlist info"""
try:
_id = ObjectId(playlist_id)
except InvalidId:
_id = playlist_id
playlist = self.db["playlists"].find_one({"_id":_id})
if playlist != None:
resp.body = dumps(playlist)
resp.status = falcon.HTTP_200
else:
resp.status = falcon.HTTP_404
def on_delete(self, req, resp, playlist_id):
"""Delete this playlist"""
try:
_id = ObjectId(playlist_id)
except InvalidId:
_id = playlist_id
playlist = self.db["playlists"].find_one({"_id":_id})
if playlist != None:
self.db["songs"].remove({"playlist":_id})
self.db["playlists"].remove({"_id":_id})
resp.status = falcon.HTTP_200
else:
resp.status = falcon.HTTP_404
class SongsCollection(BaseResponse):
"""Operations on a songs collection in a playlist"""
def on_get(self, req, resp, playlist_id, extention=".json"):
"""Return list of songs in a playlist"""
try:
playlist_id = ObjectId(playlist_id)
except InvalidId:
playlist_id = playlist_id
songs = list(self.db["songs"].find({"playlist":playlist_id}))
for song in songs:
song["url"] = "http://s3.storage.ms.wut.ee/"+str(song["_id"])
if songs != None and len(songs):
if extention == ".m3u":
resp.body = "\n".join([song["url"] for song in songs])
resp.content_type = "audio/mpegurl"
else:
resp.body = dumps(songs)
resp.status = falcon.HTTP_200
else:
resp.status = falcon.HTTP_404
def on_post(self, req, resp, playlist_id):
"""Add a song to a playlist"""
try:
playlist_id = ObjectId(playlist_id)
except InvalidId:
playlist_id = playlist_id
data = json.loads(req.stream.read().decode('utf-8'))
try:
info = youtube_dl.YoutubeDL({}).extract_info(data["_id"], download=False, process=False)
pl = {
"_id": info["id"],
"title": info["title"],
"type": "Youtube"
}
except Exception as ex:
pl = {
"title": data["title"],
"type": "Simple"
}
pl["playlist"] = str(playlist_id)
result = self.db["songs"].insert_one(pl)
pl["_id"] = str(result.inserted_id)
resp.status = falcon.HTTP_201
resp.body = json.dumps(pl, indent=2)
resp.status = falcon.HTTP_201
class SongResource(BaseResponse):
"""Operations on a one song"""
def on_get(self, req, resp, playlist_id, song_id):
"""Return a one song"""
try:
playlist_id = ObjectId(playlist_id)
except InvalidId:
playlist_id = playlist_id
songs = list(self.db["songs"].find({"playlist":playlist_id, "_id":song_id}))
for song in songs:
song["url"] = "http://s3.storage.ms.wut.ee/"+str(song["_id"])
if songs != None and len(songs):
resp.body = dumps(songs)
resp.status = falcon.HTTP_200
else:
resp.status = falcon.HTTP_404
def on_delete(self, req, resp, playlist_id, song_id):
"""Delete that song from playlist"""
resp.status = falcon.HTTP_204
class GCMResource(BaseResponse):
"""Registration of Google Cloud Messaging clients"""
def on_post(self, req, resp):
token = req.stream.read().decode()
print("Registering device with token ..."+(token[-20:]))
try:
self.db["gcm"].insert_one({"_id": token})
except DuplicateKeyError as err:
resp.status = falcon.HTTP_409
else:
resp.status = falcon.HTTP_201
class ReadOnly():
"""Make everything readonly"""
allowed_methods = ("GET", "HEAD")
def __init__(self, readonly=True):
self.readonly = readonly
def process_request(self, req, resp):
if self.readonly and req.method not in self.allowed_methods:
raise falcon.HTTPMethodNotAllowed(self.allowed_methods)
client = MongoClient()
db = client["MusicSync"]
app = falcon.API(middleware=[ReadOnly(READ_ONLY_API)])
app.add_route('/playlists/', PlaylistsCollection(db))
app.add_route('/playlists/{playlist_id}/', PlaylistResource(db))
app.add_route('/playlists/{playlist_id}/songs/', SongsCollection(db))
app.add_route('/playlists/{playlist_id}/songs{extention}', SongsCollection(db))
app.add_route('/playlists/{playlist_id}/songs/{song_id}', SongResource(db))
app.add_route('/gcm', GCMResource(db))
# a debug server
if __name__ == '__main__':
httpd = simple_server.make_server('127.0.0.1', 8000, app)
print("Running a dev server at http://{}:{}".format(*httpd.server_address))
httpd.serve_forever()