/
main.py
309 lines (239 loc) · 8.54 KB
/
main.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
from flask import Flask
from flask import session
from flask import render_template
from flask import request
from flask import redirect
from flask import render_template
from flask import jsonify , url_for
from flask import send_file
import models as dbHandler
from werkzeug import secure_filename
import os
app = Flask(__name__)
# Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
UPLOAD_FOLDER_FILE = './uploads/songs'
UPLOAD_FOLDER_IMG = './uploads/images'
app.config['UPLOAD_FOLDER_FILE'] = UPLOAD_FOLDER_FILE
app.config['UPLOAD_FOLDER_IMG'] = UPLOAD_FOLDER_IMG
###################### root ##################################################
@app.route('/')
def main():
try :
if 'username' in session :
rows1=dbHandler.genre()
rows2=dbHandler.artist()
username = session['username']
p_list = dbHandler.playlist_names(username)
return render_template('main.html', genre = rows1 , artist = rows2, playlist = p_list)
else:
rows1 = dbHandler.genre()
rows2 = dbHandler.artist()
return render_template('guest.html', genre = rows1 , artist = rows2)
except Exception as e:
return str(e)
@app.route('/guests')
def guests_tr():
try :
rows1=dbHandler.genre()
rows2=dbHandler.artist()
return render_template('guest.html', genre = rows1 , artist = rows2)
except Exception as e:
return str(e)
@app.route('/main')
def main_tr():
try :
if 'username' in session :
rows1=dbHandler.genre()
rows2=dbHandler.artist()
username = session['username']
p_list = dbHandler.playlist_names(username)
return render_template('main.html', genre = rows1 , artist = rows2, playlist = p_list)
else:
return redirect(url_for('guests'))
except Exception as e:
return str(e)
####################### login #################################################
@app.route('/login', methods=['POST', 'GET'])
def login():
try :
if 'username' in session :
return redirect(url_for('main'))
elif request.method == 'POST':
if dbHandler.authenticate(request):
session['username'] = request.form['username']
return redirect(url_for('main'))
else:
return render_template('login1.html' , message="Authentication failed.")
return render_template('login1.html', message = "")
except Exception as e:
return str(e)
######################## logout #################################################
@app.route('/logout', methods=['POST', 'GET'])
def logout():
if 'username' in session:
name = session.pop('username')
return redirect(url_for('main'))
return redirect(url_for('main'))
######################### register ################################################
@app.route('/register', methods=['POST', 'GET'])
def register():
# print "",request.method
if request.method=='POST':
if dbHandler.insertUser(request):
session['username'] = request.form['username']
msg = "success in adding user"
return redirect(url_for('main'))
else:
msg = "registration failed"
return render_template("register1.html", message=msg)
return render_template('register1.html', message = "")
####################UPDATE PASSWORD #########################
@app.route('/update_pass', methods=['POST', 'GET'])
def update_p():
if 'username' in session:
if request.method=='POST':
dbHandler.update_password(request)
return redirect(url_for('main'))
else :
return render_template('update_p.html')
else:
msg = "user dont exists"
return render_template("error.html", msg = msg)
#########################################################################
@app.route('/sow')
def sow():
rows=dbHandler.sow2()
return render_template("sows.html",rows=rows)
#########################################################################
@app.route('/play/<id>', methods=['POST', 'GET'])
def play(id):
if 'username' in session:
username=session['username']
print(id)
rows=dbHandler.playgo(id)
playlist = dbHandler.fetch_playlist(username)
return render_template("two.html",username=username,rows=rows, playlist = playlist)
else:
return redirect(url_for('login'))
###########################upload #############################
@app.route('/upload', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
img = request.files['img']
print(request.form.get('cat', ""))
print(request.form.get('art', ""))
print(request.form.get('name', ""))
for key, value in request.form.items():
print("key: {0}, value: {1}".format(key, value))
data = {'cat_id': None,'art_id': None ,'s_name': None, 'u_id':None}
data['cat_id'] = request.form.get('cat', "")
data['art_id'] = request.form.get('art', "")
data['s_name'] = request.form.get('name',"")
data['u_name']=session['username']
s_id = dbHandler.song_upload(data)
print(s_id)
print("pass1")
f.save(os.path.join(app.config['UPLOAD_FOLDER_FILE'],str(s_id)+'.mp3'))
img.save(os.path.join(app.config['UPLOAD_FOLDER_IMG'],str(s_id)+'.jpg'))
print("pass2")
return jsonify(
# summary = make_summary(f),
s_name = data["s_name"],
status = s_id
)
########################## DOWNLOAD FILE ###################
@app.route('/download_files/<type>/<id>')
def return_files(type ,id):
try:
if str(type) == 'img':
url = './uploads/images/'+str(id)+'.jpg'
print(url)
return send_file(url, attachment_filename='abc.jpg')
elif str(type) == 'mp3':
dbHandler.update_songcount(id)
url = './uploads/songs/' + str(id)+'.mp3'
return send_file(url, attachment_filename='abc.mp3')
return "not a valid file format"
except Exception as e:
return str(e)
@app.route('/add_to_playlist/<id>/<songname>/<p_name>')
def add_to_list(id, songname, p_name):
try:
if 'username' in session:
username = session['username']
result = dbHandler.playlist(username, id, songname, p_name)
return jsonify(msg = result)
except Exception as e:
return str(e)
@app.route('/fetch_playlist/<name>')
def fetch_list(name):
try:
if 'username' in session:
username = session['username']
result = dbHandler.fetch_playlist(username, name)
return render_template('playlist_songs.html',rows = result)
except Exception as e:
return str(e)
@app.route('/genre/<id>')
def get_genre(id):
try:
rows = dbHandler.fetch_genre(id)
print(rows)
return render_template("song_temp.html" , rows = rows)
except Exception as e:
return str(e)
@app.route('/artist/<id>')
def get_artist(id):
try:
rows = dbHandler.fetch_artist(id)
return render_template("song_temp.html" , rows = rows)
except Exception as e:
return str(e)
@app.route('/playlist_names')
def p_names():
try :
if 'username' in session:
username = session['username']
result = dbHandler.playlist_names(username)
return render_template('playlist_names.html' , rows = result)
else:
return str("error")
except Exception as e :
return str(e)
@app.route('/playlist_names1')
def p_names1():
try :
if 'username' in session:
username = session['username']
result = dbHandler.playlist_names(username)
return render_template('playlist_names1.html' , playlist = result)
else:
return str("error")
except Exception as e :
return str(e)
@app.route('/trending_songs')
def tr():
try:
result = dbHandler.fetch_trending()
return render_template('song_temp.html', rows = result)
except Exception as e:
return str(e)
@app.route('/devotional_songs')
def trtr():
try:
result = dbHandler.fetch_devotional()
return render_template('song_temp.html', rows = result)
except Exception as e:
return str(e)
@app.route('/hiphop_songs')
def trtrtr():
try:
result = dbHandler.fetch_hiphop()
return render_template('song_temp.html', rows = result)
except Exception as e:
return str(e)
######################## logout #################################################
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1')