-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
277 lines (204 loc) · 7.49 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
from flask import Flask, g, flash, request, jsonify, redirect, render_template
from flask_restful import Resource, Api
from pymongo import MongoClient
from flask import send_file
from flask.ext.httpauth import HTTPBasicAuth
import soundcloud
from key import client_id
from pprint import pprint
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import json
from get_from_azure import get_from_azure
import random
from decorator import crossdomain
app = Flask(__name__)
api = Api(app)
# for session management
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
print id
return User.get(id)
mongoclient = MongoClient('mongodb://localhost:27017/')
sinder = mongoclient.sinder
# create a pclient object with your app credentials
client = soundcloud.Client(client_id=client_id)
@app.before_request
def before_request():
g.user = current_user
class User(UserMixin):
_id = 0
username = ""
password = ""
# is_auth = False
def __init__(self, username, password):
self._id = username
self.username = username
self.password = password
# self.is_auth = False
@classmethod
def get(cls, id):
obj = sinder.userdb.find_one({'_id': id})
return User(obj['username'], obj['password'])
def get_id(self):
print("returning this from get_id" + unicode(self._id))
return unicode(self._id)
@app.route('/', methods=['GET'])
def see():
return render_template('index.html')
@app.route('/getTrack', methods=['GET'])
@crossdomain(origin='*')
def get_stream_url(username=""):
"""
:param track_no: track number as on sound cloud
:return: fetch and return image, stream url, artist info from sound cloud
"""
track_no = request.args['track_no']
track = client.get('/tracks/' + str(track_no))
# get the tracks streaming URL
stream_url = client.get(track.stream_url, allow_redirects=False)
return jsonify({'stream_url': stream_url.location}),200
@app.route('/swipe/')
@login_required
def swipe():
return render_template('random.html', song_dict=get_track_details())
@app.route('/track/', methods=['GET', 'OPTIONS'])
@crossdomain(origin='*')
def rate_track():
"""
:return: save info for user and track and calls get track for next track
"""
rating = request.args['dir']
username = request.args['username']
print request.args
track_no = request.args['track_no']
sinder.entries.update({'track_no': track_no, 'username': username}, {'username': username, 'track_no': track_no, 'dir': 1 if rating == "right" else 0}, upsert=True)
if rating == "right":
insert_track = sinder.tracklist.find_one({'track_no': track_no})
sinder.playlist.update({'track_no': track_no, 'username': username}, {'username': username,
'track_no': track_no, 'title': insert_track['title'],
'artwork_url': insert_track['artwork_url'],
'artist': insert_track['artist']}, upsert=True)
return get_track(username)
@app.route('/playlist/', methods=['GET'])
@login_required
def get_playlist():
userid = g.user.get_id()
list_songs = []
songs = sinder.playlist.find({'username': userid})
for song in songs:
if not song.has_key('artwork_url'):
song['artwork_url'] = 'http://i.imgur.com/bLrm4qD.jpg?1'
list_songs.append(song)
print "reached ehre"
return render_template('playlist.html', list_songs=list_songs)
def get_track(username):
"""
get track info from azure recommendation engine for particular user
"""
return jsonify(get_track_details(username)), 200
def get_track_details(username=""):
"""
:param username: bit of a hack in username
XXX(psdh) Figure out how do usernames work across AJAX requests
:return: fetch and return image, stream url, artist info from sound cloud
"""
try:
userid = g.user.username
except:
userid = username
print userid
username = userid
get_songs = sinder.songsdb.find({'username': username, 'heard': 0})
if get_songs.count() > 0:
track = get_songs[0]
track_no = track['track_no']
track['heard'] = 1
sinder.songsdb.update({'_id': track['_id']}, track)
else:
track_nos = get_from_azure(sumchar(userid))
print len(track_nos)
for track_no in track_nos:
print "inserting" + track_no + " + now"
if sinder.songsdb.find({'track_no': track_no, 'username': userid}).count() == 0:
sinder.songsdb.insert_one({'track_no': track_no, 'username': userid, 'heard': 0})
track_no = track_nos[0]
sinder.songsdb.update({'track_no': track_no, 'username': userid}, {'track_no': track_no, 'username': userid, 'heard': 1})
# fetch track to stream
try:
track = client.get('/tracks/' + str(track_no))
except:
# reaching here implies that get track points to a bad url,
# such songs must be removed from everywhere
sinder.removesongs.add({'track_no': track_no})
return get_track_details(username)
# get the tracks streaming URL
stream_url = client.get(track.stream_url, allow_redirects=False)
artwork_url = str(track.artwork_url).replace("large", "t300x300")
title = track.title
user = track.user
if artwork_url == 'None':
artwork_url = 'http://i.imgur.com/bLrm4qD.jpg?1'
ret = {
'track_no': str(track_no),
'artwork_url': artwork_url,
'artist': user['username'],
'title': title,
}
copy = {
'track_no': str(track_no),
'artwork_url': artwork_url,
'artist': user['username'],
'title': title,
}
sinder.tracklist.insert_one(copy)
ret['stream_url'] = stream_url.location
pprint(ret)
return ret
@app.route('/register', methods=['POST'])
def new_user():
username = request.form.get('signup_username')
password = request.form.get('signup_password')
pprint(request.form)
pprint (username)
pprint(password)
if sinder.userdb.find({'username': username}).count() > 0:
# pain bro, username already exists
return jsonify({'error': 'User already exists', 'username': username, 'status': 0}), 200
user = User(username=username, password=password)
print user.__dict__
login_user(user)
sinder.userdb.insert_one(user.__dict__)
return jsonify({'status': 1}), 200
@app.route('/logout/')
@login_required
def logout():
logout_user()
return redirect('/')
@app.route('/login', methods=['POST'])
def user_login():
print request
username = request.form.get('username')
password = request.form.get('password')
check_user = sinder.userdb.find({'username': username})
if check_user.count() <= 0:
return redirect('/')
else:
check_user = check_user[0]
if check_user['username'] == username and check_user['password'] == password:
# login successful
user = User(username=username, password=password)
login_user(user)
return redirect('/swipe')
else:
# TODO Add reader for these message on main html pages !!
return jsonify({'error': 'Invalid Credentials', 'status': 0}), 200
def sumchar(s):
ret = 0
for ch in s:
ret += ord(ch)
return ret
if __name__ == '__main__':
app.secret_key = 'shuffle bro kaam kar jae bas'
app.run(debug=True)