This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotdl_cli.py
177 lines (134 loc) · 5.09 KB
/
spotdl_cli.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
from os import system
import sys
from threading import Thread
try:
import spotdl
except:
if input("spotdl not found, Download Now? (y/n): ").lower() == 'y':
system('pip install -U spotdl')
else:
sys.exit()
from spotdl.authorize.services import AuthorizeSpotify
from spotdl import Spotdl, util
from spotdl.helpers.spotify import SpotifyHelpers
class BColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# -- As per Spotfiy API documentation --
# Make sure that you safeguard your application Client Secret at all times.
# Be aware, for example, that if you commit your code to a public repository like GitHub
# you will need to remove the Client Secret from your code before doing so.
u_client_id = ''
u_client_secret = ''
try:
# Attempt to locate spotify secret keys from a local spotify_keys.txt file
with open("spotify_keys.txt", "r+") as keys:
contents = keys.readlines()
if contents:
contents = [key.strip() for key in contents] # Removal of newlines
try:
u_client_id = contents[0]
u_client_secret = contents[1]
print(BColors.OKGREEN +
"Success: Found local spotify keys!" + BColors.ENDC)
except IndexError:
raise FileNotFoundError
else:
raise FileNotFoundError
except FileNotFoundError:
# If keys are not found, allow the user to obtain the keys from spotify
print(BColors.WARNING + "Warning: You are missing the client_id/secret which is required for the album/playlist features" + BColors.ENDC)
print(BColors.WARNING +
"You can obtain these keys by creating a quick app with Spotify" + BColors.ENDC)
print("https://developer.spotify.com/dashboard/applications\n")
# User is able to proceed without keys, which will limit some features
if(input("Enter keys manually? (y/n): ").lower()[0] == "y"):
# Note: Create a first call to the /autorize endpoint to validate if an API token was retrieved?
u_client_id = input("Enter your client id:")
u_client_secret = input("Enter your client secret:")
# Keys will be saved for the future in a local text file
with open("spotify_keys.txt", "w") as keys:
keys.writelines([u_client_id + "\n", u_client_secret])
print(BColors.OKGREEN +
"Success: Your keys were saved for future use!" + BColors.ENDC)
else:
print(BColors.WARNING +
"Warning: Cannot proceed without the keys! Exiting Now..." + BColors.ENDC)
sys.exit()
except:
raise
helper_instance = SpotifyHelpers(spotify=AuthorizeSpotify(
client_id=u_client_id, client_secret=u_client_secret))
spotdl_instance = Spotdl()
def logs():
print(util.install_logger(level='INFO'))
log = Thread(target=logs)
def song(link):
log.start()
def download():
spotdl_instance.download_track(link)
downloader = Thread(target=download)
downloader.start()
def album(link):
log.start()
alb = helper_instance.fetch_album(link)
helper_instance.write_album_tracks(alb, './album_tracks.txt')
def download():
spotdl_instance.download_tracks_from_file('album_tracks.txt')
downloader = Thread(target=download)
downloader.start()
def playlist(link):
log.start()
playlist = helper_instance.fetch_playlist(link)
helper_instance.write_playlist_tracks(playlist, '.\playlist_tracks.txt')
def download():
spotdl_instance.download_tracks_from_file('playlist_tracks.txt')
downloader = Thread(target=download)
downloader.start()
def textlist(directory):
log.start()
def download():
spotdl_instance.download_tracks_from_file(directory)
downloader = Thread(target=download)
downloader.start()
def scan(link):
if "spotify.com/track" in link.lower():
song(link)
elif "spotify.com/playlist" in link.lower():
playlist(link)
elif ".txt" in link.lower():
textlist(link)
elif "spotify.com/album" in link.lower():
album(link)
else:
song(link)
def main():
system('cls')
print(
"""
======================================================
| |
| 1) Simple Usage. 2) Manual (Advanced) Usage. |
| |
======================================================
"""
)
type = input("Selcet an Option (1/2): ")
if type == '1':
system('cls')
scan(input(
"Enter A Song/playlist/album link or Enter the path to a list:\n"))
elif type == '2':
system('cls')
__import__('adv_spotdl_cli').main()
else:
print("Invalid Input")
sys.exit()
if __name__ == '__main__':
main()