-
Notifications
You must be signed in to change notification settings - Fork 0
/
YourMusic.py
152 lines (145 loc) · 4.86 KB
/
YourMusic.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
from googlesearch import search
import pytube
from pytube import YouTube
import webbrowser
import shutil
import os
import time
import pyfiglet
Logo = pyfiglet.figlet_format("Music")
print("\n")
print("\n")
print(Logo)
print("\n")
def main():
with open("searches.txt", "r") as searchesopen:
searchesread = searchesopen.readlines()
savedsongs = os.listdir(f"{os.getcwd()}\Songs")
if savedsongs == "":
print("You currently don't have any saved song.")
else:
print("------------")
print("Saved Songs:")
print("------------")
savedsongslist = []
z = 0
while z < len(savedsongs):
savedsongslist.append(savedsongs[z].replace(".mp3", ""))
print(savedsongslist[z])
z += 1
print("\n")
if len(searchesread) > 7:
newsearchesfile = open("searches.txt", "w")
newsearches = []
newsearches.append(searchesread[2])
newsearches.append(searchesread[3])
newsearches.append(searchesread[4])
newsearches.append(searchesread[5])
newsearches.append(searchesread[6])
newsearches.append(searchesread[7])
j = 0
while j < len(newsearches):
newsearchesfile.write(newsearches[j])
j += 1
else:
print("------------------")
print("Previous Searches:")
print("------------------")
y = 0
while y < len(searchesread):
print(searchesread[y].strip("\n"))
y += 1
print("\n")
def search_song():
inputsearch = input("Input the song name: ")
outputsearch = open("searches.txt", "a")
outputsearch.write("\n" + inputsearch)
query = "youtube" + inputsearch
results = ""
result = []
for results in search(query, tld="co.in", num=1, stop=1, pause=2):
result.append(results)
link = result[0]
song = pytube.YouTube(link)
print("Song Found")
print(f"Title: {song.title}")
print(f"Publish Date: {song.publish_date.strftime('%Y-%m-%d')}")
print(f"Duration: {int(song.length/60)}:{song.length%60}")
mp3filename = song.title + ".mp3"
song.streams.filter(abr="160kbps", progressive=False).first().download(filename=mp3filename)
currentdirectory = f"{os.getcwd()}\{mp3filename}"
destinationdirectory = f"{os.getcwd()}\Songs\{mp3filename}"
shutil.move(currentdirectory, destinationdirectory)
webbrowser.open(f"{os.getcwd()}\Songs\{mp3filename}")
return main()
def play_a_saved_song():
savedsongsget = os.listdir(f"{os.getcwd()}\Songs")
savedsongs = []
g = 0
while g < len(savedsongsget):
savedsongs.append(savedsongsget[g].replace(".mp3", ""))
g += 1
if savedsongs == "":
print("You currently have no saved song.")
else:
print("-------------------")
print("Listing Saved Songs")
print("-------------------")
s = 0
while s < len(savedsongs):
print(f"Number {s} - {savedsongs[s]}")
s += 1
print("\n")
inputplay = int(input("Input the number of the song you want to play: "))
webbrowser.open(f"{os.getcwd()}\Songs\{savedsongsget[inputplay]}")
return main()
def delete_a_saved_song():
savedsongsget = os.listdir(f"{os.getcwd()}\Songs")
h = 0
while h < len(savedsongsget):
print("Number ", h, savedsongsget[h].replace(".mp3", ""))
h += 1
print("\n")
inputdelete = int(input("Input the number of the song you want to delete: "))
os.remove(f"{os.getcwd()}\Songs\{savedsongsget[inputdelete]}")
return main()
print("-------")
print("Options")
print("-------")
print("[1] Search for a New Song")
print("[2] Play a Saved Song")
print("[3] Delete a Saved Song")
print("\n")
print("[99] Exit")
process = input("--->")
if process == "1":
try:
search_song()
except KeyboardInterrupt:
print("Command Cancelled.")
time.sleep(2)
main()
elif process == "2":
play_a_saved_song()
elif process == "3":
try:
delete_a_saved_song()
except KeyboardInterrupt:
print("Command Cancelled.")
time.sleep(2)
main()
elif process == "99":
exit()
else:
print("Error, available options: 1, 2, 3, 99.")
time.sleep(3)
return main()
try:
main()
except KeyboardInterrupt:
print("Command Cancelled.")
time.sleep(2)
main()
except Exception as e:
print("Errore Generico:")
print(e)