-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmusic.py
321 lines (312 loc) · 8.06 KB
/
music.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
310
311
312
313
314
315
316
317
318
319
320
321
#!/bin/python
# coding: utf-8
import os
import time
from random import *
import sys
import threading
import subprocess
playing=False
#Scan music from sdcard
#turn music off after kill process
#run python script in background
#add functionality to show previous commands entered
#Generates MP3 List
def genlist():
if getState():
os.system('termux-media-player stop')
print '\nPlease Wait !!\n\tIt may Take A While To Scan Your Phone Memory...'
print 'It may take a couple of minutes....'
os.system("find -L /sdcard -type f -ipath '*.mp3' >mp3.list")
print '\nPhone Scan Completed !!!'
ch=raw_input('Want To Scan SD Card (Y/N): ')
if ch.lower().strip()=="y":
print '\nPlease Wait !!\n\tIt may Take A While To Scan Your SDCARD Memory...'
print 'It may take a couple of minutes....'
os.system("find -L /storage/sdcard1 -type f -ipath '*.mp3' >>mp3.list")
print '\nSD Card Scan Completed'
raw_input('Press Enter To Continue...')
#Checks if music is being played
def getState():
subprocess.call("termux-media-player info >info.xx",shell=True)
f1=open("info.xx","r")
a=f1.read()
f1.close()
if len(a) >= 21:
return True
else:
return False
#Exits TPlay
def Exit():
global p
ln=open('mp3.list').read()
if len(p)!=ln.count('\n'):
print 'Please Wait While We Save Some Changes...'
f=open('mp3.list','w')
for wr in p:
f.write(wr+'\n')
f.close()
print 'Changes Saved !!!!'
print ' \t Created By SpeedX'
os.system("echo -e \"\e[1;31m\"")
os.system("toilet -f mono12 -F border SpeedX ")
os.system("echo -e \"\e[1;32m\"")
os.system('echo -e "\\e[1;34m Created By SpeedX\\e[0m"')
os.system('echo -e "\\e[4;32m This Player Was Created By SpeedX \\e[0m"')
os.system('echo -e "\\e[1;34m For Any Queries Mail Me!!!\\e[0m"')
os.system('echo -e "\\e[1;32m Mail: ggspeedx29@gmail.com \\e[0m"')
os.system('echo -e "\\e[1;31m Whatsapp: https://bit.do/speedxgit \\e[0m"')
os.system('echo -e "\\e[1;33m YouTube Page: https://www.youtube.com/c/GyanaTech \\e[0m"')
exit()
#Gets Valid Songs Paths And extracts song name
def dislist():
global p
global sname
f=open("mp3.list","r")
a=f.read()
f.close()
p=a.split("\n")
p.remove('')
sname=[]
flg=True
while flg:
flg1=True
for pr in p:
if not os.path.exists(pr):
p.remove(pr)
flg1=False
if flg1:
flg=False
cols=int(os.popen('echo $COLUMNS').read().split('\n')[0])
for pr in p:
l=pr.rfind('/')+1
if l==0:
continue
ap=pr[l:len(pr)-4]
dif=len(ap)-cols+30
if dif>0:
ap=ap[:len(ap)-dif]
sname.append(ap)
if len(p) ==0:
print "No Songs Found in here!!"
print "Songs with .mp3 extension are recognised only !!!"
Exit()
print str(len(p))+" songs Loaded !!"
print "Press Enter To Start Playing..."
raw_input()
#Sorts MP3 List path wise
def sortlist():
global p
ln=open('mp3.list').read()
if len(p)!=ln.count('\n'):
print 'Please Wait While We Save Some Changes...'
f=open('mp3.list','w')
for wr in p:
f.write(wr+'\n')
f.close()
print 'Changes Saved !!!!'
print 'Sorting List....'
os.system('sort -bfidu mp3.list -o mp3.list')
dislist()
#Removes Song From PlayList
def remove(n):
global p
global sname
p.pop(n)
sname.pop(n)
if not os.path.isfile(os.environ['PREFIX']+'/bin/termux-media-player'):
print 'Please Use Termux For This Player\n\tMake Sure You Installed Termux:API\n\t\tpkg install termux-api'
Exit()
if not os.path.isfile('mp3.list'):
genlist()
f=open("mp3.list","r")
a=f.read()
f.close()
p=a.split("\n")
p.remove('')
sname=[]
flg=True
while flg:
flg1=True
for pr in p:
if not os.path.exists(pr):
p.remove(pr)
flg1=False
if flg1:
flg=False
if len(p) ==0:
print "No Songs Found in here!!"
print "Songs with .mp3 extension are recognised only !!!"
Exit()
cols=int(os.popen('echo $COLUMNS').read().split('\n')[0])
for pr in p:
l=pr.rfind('/')+1
if l==0:
continue
ap=pr[l:len(pr)-4]
dif=len(ap)-cols+30
if dif>0:
ap=ap[:len(ap)-dif]
sname.append(ap)
print 'Songs Loaded !!!'
os.system("clear")
n=0
k=randint(0,len(p)-1)
n=k
pos=0
empl=500+len(p)*10
#lins='─'
lins='-'
while n < len(p):
fg=True
cols=int(os.popen('echo $COLUMNS').read().split('\n')[0])
lin=lins*cols
flag=True
for cl in range(0,empl):
print
os.system('clear')
os.system("echo -e \"\e[1;31m\"")
os.system("toilet -f mono12 -F border TPlay ")
os.system("echo -e \"\e[1;33m\"")
os.system("printf \"%${COLUMNS}s\\n\" \"Created By SpeedX \"")
os.system("echo -e \"\e[1;32m\"")
os.system('echo ')
for i in range(0,len(p)):
if i==n:
print str(i+1)+"\t| --> "+sname[i]+" (PLAYING)"
else:
print str(i+1)+"\t| "+sname[i]
print lin
playing=True
if not getState() or pos!=n:
pos=n
os.popen("termux-media-player play \""+p[pos]+"\"")
print 'Now Playing: '+p[pos]
ref=False
while True:
inp=''
try:
inp=raw_input('TPlay > ').strip()
except:
print 'Some Exception Occurred!!!'
os.popen("rm info.xx")
os.popen("termux-media-player stop")
print "Exiting Player...\n"
Exit()
if inp.strip().lower().find('quit')!=-1 or inp.strip().lower().find('exit')!=-1:
os.popen("rm info.xx")
os.system("termux-media-player stop")
print "Exiting Player...\n"
Exit()
elif inp.strip() == "":
if not getState():
break
elif inp.strip().lower().find("play") != -1:
try:
k=int(inp[5:len(inp)])-1
if k>=len(p) or k<0:
print 'Sorry Can\'t Play That Song !!!\n\t\t You Have Only '+str(len(p))+' Songs'
print '\tChoose Between 1 To '+str(len(p))+' Songs'
else:
n=k
fg=False
ref=True
break
except:
os.system('termux-media-player play')
elif inp.lower().find('pause') !=-1:
os.system('termux-media-player pause')
elif inp.lower().find('next') !=-1:
n=n+1
fg=False
ref=True
break
elif inp.lower().find('prev') !=-1:
n=n-1
fg=False
ref=True
break
elif inp.lower().find('rand') !=-1:
n=randint(0,len(p)-1)
ref=True
break
elif inp.lower().find('reload') !=-1:
print 'Recreating List....'
genlist()
dislist()
n=randint(0,len(p)-1)
ref=True
break
elif inp.lower().find('sort') !=-1:
nsg=p[n]
sortlist()
n=p.index(nsg)
#n=randint(0,len(p)-1)
ref=True
break
elif inp.lower().strip().find('remove') !=-1:
try:
r=int(inp[7:len(inp)])-1
remove(r)
if n==r:
os.popen('termux-media-player stop')
n=randint(0,len(p)-1)
else:
if pos>r:
pos=pos-1
n=pos
ref=True
break
except:
print 'Please Enter A Number ...'
print 'Usage:\nremove <track_nukber> - Removes Song With Respective Number From PlayList'
elif inp.lower().find('ref') !=-1:
cols=int(os.popen('echo $COLUMNS').read().split('\n')[0])
for pr in range(len(p)):
l=p[pr].rfind('/')+1
if l==0:
continue
ap=p[pr][l:len(p[pr])-4]
dif=len(ap)-cols+30
if dif>0:
ap=ap[:len(ap)-dif]
sname[pr]=ap
ref=True
break
elif inp.lower().find('info') !=-1:
subprocess.call("termux-media-player info >info.xx",shell=True)
f1=open("info.xx","r")
print f1.read()
f1.close()
elif inp.lower().find('help') !=-1:
print """
Available Commands are:---
play - Plays Paused Music
play <track_number> - Plays The Song With That Track Number ( EX- play 3 )
pause - Pauses Playing Music
next - Plays Next Song
prev - Plays Previous Song
random - Plays Random Song
quit / exit - Stops Playing Music And Exits Player
info - Gets Info of Currently Playing Song
reload - Rescans The Phone Memory For MP3 files and creates A New List
ref - Refreshes The Screen
remove <track_number> - Removes Song With Respective Number From PlayList
sort - Sort The List According To Path
"""
else:
print 'INVALID COMMAND\nType help for details...'
if not getState():
break
if n >= len(p):
n=0
elif n<0:
n=len(p)-1
if ref and getState():
continue
if getState()==False and fg:
n=n+1
if n >= len(p):
n=0
elif n<0:
n=len(p)-1