-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
288 lines (252 loc) · 8.69 KB
/
app.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
from flask import Flask,render_template,flash,redirect,url_for,session,logging,request,send_file,current_app,send_from_directory,g
from flask_mail import Mail,Message
from flask_mysqldb import MySQL
from wtforms import Form,StringField,TextAreaField,PasswordField,validators
from passlib.hash import sha256_crypt
from functools import wraps
from itsdangerous import URLSafeTimedSerializer,SignatureExpired
import os
from bs4 import BeautifulSoup
import requests
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
from tinytag import TinyTag
import sqlite3 as sql
from tinydb import TinyDB, Query
app=Flask(__name__)
name1=""
usernname1=""
email1=""
password1=""
username2=""
app.secret_key='secret123'
app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_USER']='root'
app.config['MYSQL_PASSWORD']='farook100'
app.config['MYSQL_DB']='my_music'
app.config['MYSQL_CURSORCLASS']='DictCursor'
app.config['UPLOAD_FOLDER']='/music'
app.config['MAX_CONTENT_PATH']=5000000
app.config.from_pyfile('config.cfg')
mail=Mail(app)
s=URLSafeTimedSerializer('secret123')
mysql=MySQL(app)
@app.route('/')
def index():
return render_template('index.html')
class RegisterForm(Form):
name=StringField('Name',[validators.Length(min=1,max=50)])
username=StringField('Username',[validators.Length(min=4,max=25)])
email=StringField('Email',[validators.Length(min=6,max=50)])
password=PasswordField('Password',[validators.DataRequired(),validators.EqualTo('confirm',message='Password do not match')])
confirm=PasswordField('confirm Password')
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
@app.route('/register',methods=['GET','POST'])
def register():
form =RegisterForm(request.form)
if request.method=='POST' and form.validate():
name=form.name.data
email=form.email.data
username=form.username.data
password=sha256_crypt.encrypt(str(form.password.data))
global usernname1,name1,email1,password1
usernname1=username
name1=name
email1=email
password1=password
database = 'database.db'
conn=sql.connect(database)
conn1=sql.connect(database)
token=s.dumps(email,salt='email-confirm')
cur = conn.cursor()
cur1= conn1.cursor()
result=cur.execute("SELECT * FROM users WHERE username=?",(username,))
result2=cur1.execute("SELECT * FROM users WHERE email=?",(email,))
conn.commit()
records=cur.fetchall()
records1=cur1.fetchall()
# for row in cur.fetchall():
# print(row)
if len(records)>0:
error='User name already exists,please try another user name'
return render_template('register.html',form=form,error=error)
if len(records1)>0:
error='Email already exists,please try another Email'
return render_template('register.html',form=form,error=error)
else:
cur.execute("INSERT INTO users(name,email,username,password) VALUES(?,?,?,?)",(name1,email1,usernname1,password1))
conn.commit()
conn.close()
flash('Successfully created','success')
return redirect(url_for('login'))
return render_template('register.html',form=form)
#login
@app.route('/login',methods=['GET','POST'])
def login():
if request.method=='POST':
username=request.form['username']
password_candidate=request.form['password']
database = 'database.db'
conn = sql.connect(database)
cur = conn.cursor()
result=cur.execute("SELECT * FROM users WHERE username=?",(username,))
record=cur.fetchall()
conn.commit()
if len(record)>0:
data=record
password=data[0][4]
if sha256_crypt.verify(password_candidate,password):
session['logged_in']=True
session['username']=username
session['id']=data[0][0]
flash('login successful','success')
return redirect(url_for('dashboard'))
else:
error='wrong password'
return render_template('login.html',error=error)
cur.close()
else:
error='Username not found'
return render_template('login.html',error=error)
return render_template('login.html')
#to prevent using of app without login
def is_logged_in(f):
@wraps(f)
def wrap(*args,**kwargs):
if 'logged_in' in session:
return f(*args,**kwargs)
else:
flash('unauthorised,please login','danger')
return redirect(url_for('login'))
return wrap
#logout
@app.route('/logout')
def logout():
session.clear()
flash('you are now logout','success')
return redirect(url_for('login'))
@app.route('/dashboard')
@is_logged_in
def dashboard():
database = 'database.db'
conn = sql.connect(database)
cur = conn.cursor()
share_url=app.config['APP_BASE_URL']+':'+app.config['APP_PORT']+'/play_playlist/'
result=cur.execute("SELECT * from songs WHERE user_id = ?",(session['id'],))
songs=cur.fetchall()
if len(songs)>0:
return render_template('dashboard.html',songs=songs,share_url=share_url)
else:
msg="NO PLAYLIST FOUND "
return render_template('dashboard.html',msg=msg,share_url=share_url)
cur.close()
@app.route('/upload_song')
@is_logged_in
def upload():
return render_template('upload.html')
@app.route('/uploader', methods = ['GET', 'POST'])
@is_logged_in
def upload_file():
if request.method == 'POST':
f = request.files['file']
file_name = f.filename
path=os.getcwd()+'\\static\\music\\'
path1=path.replace('\\','/')
path=path1+file_name
f.save(os.path.join(path1,file_name))
username=session['username']
database = 'database.db'
conn=sql.connect(database)
cur=conn.cursor()
row1=cur.execute("SELECT * FROM users WHERE username = ?",[username])
result1=cur.fetchone()
tag = TinyTag.get(path)
album=tag.album
title=tag.title
artist=tag.artist
id=result1[0]
if title==None:
title=path
title=title.rsplit('/', 1)[-1]
i_sql="insert into songs(title,path,album,artist,user_id) values (?,?,?,?,?)"
val = (title,path,album,artist,id)
cur.execute(i_sql, val)
conn.commit()
conn.close()
return redirect(url_for('dashboard'))
else:
return redirect(url_for('dashboard'))
@app.route('/play_playlist/<string:idd>')
@is_logged_in
def play_playlist(idd):
res=""
playl=[]
data=[]
database = 'database.db'
conn = sql.connect(database)
cur = conn.cursor()
cur.execute("SELECT * FROM songs WHERE id =?",(idd,))
result=cur.fetchone()
res=result[2]
res=res.rsplit('/', 1)[-1]
if res is None:
flash("no song in playlist",'danger')
return redirect(url_for('dashboard'))
else:
return render_template('playlist.html',albu=res)
@app.route('/download/<string:idd>')
def downloadFile (idd):
res=""
playl=[]
data=[]
database = 'database.db'
conn = sql.connect(database)
cur = conn.cursor()
cur.execute("SELECT * FROM songs WHERE id =?",(idd,))
result=cur.fetchone()
res=result[2]
res=res.rsplit('/', 1)[-1]
if res is None:
flash("no song in playlist",'danger')
return redirect(url_for('dashboard'))
else:
return render_template('download.html',path=res)
@app.route('/delete_playlist/<string:idd>')
@is_logged_in
def delete_playlist(idd):
database = 'database.db'
conn = sql.connect(database)
cur = conn.cursor()
cur.execute("delete FROM songs WHERE id =?",(idd,))
conn.commit()
cur.close()
flash("Playlist successfully deleted",'success')
return redirect(url_for('dashboard'))
@app.route('/search',methods=['POST'])
@is_logged_in
def new():
string=""
co=request.form['give']
song=co
# song_name=co+'.mp3'
database = 'database.db'
conn = sql.connect(database)
cur = conn.cursor()
result=cur.execute("SELECT * FROM songs WHERE title like ? or artist like ? or album like ? ",('%'+song+'%','%'+song+'%','%'+song+'%',))
songs=cur.fetchall()
if len(songs)>0:
return render_template('search.html',songs=songs)
else:
msg="NO PLAYLIST FOUND "
return render_template('search.html',msg=msg)
cur.close()
@app.route("/")
def main():
return render_template('index.html')
if __name__ == "__main__":
app.secret_key='secret123'
app.run()