@@ -31,24 +31,35 @@ def connect_to_spotify():
31
31
redirect_uri = 'https://open.spotify.com/' ,
32
32
scope = scope ,
33
33
username = username )
34
+
34
35
if auth_manager :
35
36
return (spotipy .Spotify (auth_manager = auth_manager ), auth_manager )
37
+
36
38
print (f"Can't get token for { username } " )
37
39
sys .exit ()
38
40
39
41
42
+ def get_auth_token ():
43
+ auth_token = auth_manager .get_cached_token ()
44
+
45
+ if auth_token :
46
+ return auth_token
47
+
48
+ return auth_manager .get_access_token (as_dict = True )
49
+
50
+
40
51
def get_title_and_artist (music_dir ):
41
52
"""Recursively reads local files in indicated music_dir and yields a string 'song - artist'"""
42
53
if len (music_dir ) == 0 or not os .path .isdir (music_dir ):
43
54
while True :
44
55
music_dir = input ("Please paste the path to your music directory:" )
56
+
45
57
if os .path .isdir (music_dir ):
46
58
break
47
- else :
48
- print (
49
- "The provided path is not valid. Please try again or type "
50
- "in the path directly into the source code if there's "
51
- "issues\n (use Ctrl + C to exit the program)" )
59
+
60
+ print ("The provided path is not valid. Please try again or type "
61
+ "in the path directly into the source code if there's "
62
+ "issues\n (use Ctrl + C to exit the program)" )
52
63
else :
53
64
print (f"Found valid path. Commencing search in { music_dir } " )
54
65
@@ -58,18 +69,20 @@ def get_title_and_artist(music_dir):
58
69
try :
59
70
audiofile = TinyTag .get (os .path .join (subdir , file ))
60
71
except :
61
- pass
62
- else :
63
- files_read += 1
64
- yield (f"track:{ audiofile .title } artist:{ audiofile .artist } " , f"{ audiofile .artist } - { audiofile .title } " )
65
- # NOTE: Query being in double quotes makes it stick to the
66
- # given word order instead of matching a bunch of possibilities
67
- # Use it (by writing \" at the beginning and end of the string)
68
- # if you are not happy with the matches found
72
+ continue
73
+
74
+ files_read += 1
75
+ yield (f"track:{ audiofile .title } artist:{ audiofile .artist } " , f"{ audiofile .artist } - { audiofile .title } " )
76
+ # NOTE: Query being in double quotes makes it stick to the
77
+ # given word order instead of matching a bunch of possibilities
78
+ # Use it (by writing \" at the beginning and end of the string)
79
+ # if you are not happy with the matches found
80
+
69
81
if files_read == 0 :
70
82
print ("\n No files found at the specified location."
71
83
"Please check the path to the directory is correct." )
72
84
sys .exit ()
85
+
73
86
print (f"\n Read { files_read } files. Make sure to check for any possible "
74
87
"unread files due to \" Lame tag CRC check failed\" or similar.\n "
75
88
"Those come from an external library and this software cannot "
@@ -92,12 +105,12 @@ def create_new_playlist():
92
105
try :
93
106
date = datetime .now ().strftime ("%d %b %Y at %H:%M" ) # 1 Jan 2020 at 13:30
94
107
playlist_id = sp .user_playlist_create (
95
- username , "SpotifyMatcher" ,
108
+ username ,
109
+ "SpotifyMatcher" ,
96
110
description = "Playlist automatically created by SpotifyMatcher "
97
111
f"from my local files on { date } . "
98
112
"Try it at https://github.com/BoscoDomingo/SpotifyMatcher!" )["id" ]
99
- print (
100
- f"Find it at: https://open.spotify.com/playlist/{ playlist_id } " )
113
+ print (f"Find it at: https://open.spotify.com/playlist/{ playlist_id } " )
101
114
return playlist_id
102
115
except :
103
116
print ("\n WARNING: \n "
@@ -111,25 +124,19 @@ def add_tracks_to_playlist(track_ids):
111
124
spotify_limit = 100
112
125
while len (track_ids ) > 0 :
113
126
try :
114
- sp .user_playlist_add_tracks (
115
- username , playlist_id , track_ids [:spotify_limit ])
127
+ sp .user_playlist_add_tracks (username , playlist_id , track_ids [:spotify_limit ])
116
128
except : # API rate limit reached
117
129
sleep (0.2 )
118
130
else :
119
131
del track_ids [:spotify_limit ]
120
132
121
133
122
134
if __name__ == "__main__" :
123
- """
124
- TO-DO: Allow users to pick between 2 modes: direct like or create playlist
125
- direct_like_scope = 'user-library-modify'
126
- create_playlist_scope = 'playlist-modify-public'
127
- """
128
135
scope = 'playlist-modify-public user-library-modify'
129
136
music_dir = ""
130
137
# Write the dirpath directly here to avoid having to do it through terminal.
131
138
# Make sure to escape backslashes. Examples:
132
- # '/Users/John/Music/My Music'
139
+ # 'C: /Users/John/Music/My Music'
133
140
# "C:\\Users\\John\\Music\\My Music"
134
141
135
142
username , playlist_id = get_user_data ()
@@ -138,7 +145,7 @@ def add_tracks_to_playlist(track_ids):
138
145
# Needed to get the cached authentication if missing
139
146
dummy_search = sp .search ("whatever" , limit = 1 )
140
147
141
- token_info = auth_manager . get_cached_token () if auth_manager . get_cached_token () else auth_manager . get_access_token ( as_dict = True )
148
+ token_info = get_auth_token ( )
142
149
track_ids = []
143
150
failed_song_names = []
144
151
searched_songs = 0
@@ -148,17 +155,19 @@ def add_tracks_to_playlist(track_ids):
148
155
for query_song_pair in get_title_and_artist (music_dir ):
149
156
if auth_manager .is_token_expired (token_info ):
150
157
token_info = auth_manager .refresh_access_token (token_info ["refresh_token" ])
158
+
151
159
searched_songs += 1
152
160
print (f"{ searched_songs } : { query_song_pair [1 ]} " )
161
+
153
162
try :
154
- result = sp .search (query_song_pair [0 ], limit = 1 )[
155
- "tracks" ]['items' ][0 ]['id' ]
163
+ result = sp .search (query_song_pair [0 ], limit = 1 )["tracks" ]['items' ][0 ]['id' ]
156
164
except :
157
165
print ("\t *NO MATCH*" )
158
166
failed_matches_file .write (f"{ query_song_pair [1 ]} \n " )
159
167
failed_song_names .append (query_song_pair [1 ])
160
168
else :
161
169
track_ids .append (result )
170
+
162
171
success_rate = "{:.2f}" .format (len (track_ids )/ (searched_songs - 1 )* 100 )
163
172
print (
164
173
f"\n ***TOTAL SONGS SEARCHED: { searched_songs } "
@@ -167,6 +176,7 @@ def add_tracks_to_playlist(track_ids):
167
176
playlist_id = ensure_playlist_exists (playlist_id )
168
177
number_of_matches = len (track_ids )
169
178
add_tracks_to_playlist (track_ids )
179
+
170
180
print (f"\n Successfully added { number_of_matches } songs to the playlist.\n "
171
181
"Thank you for using SpotifyMatcher!" )
172
182
print (f"\n { searched_songs - number_of_matches } UNMATCHED SONGS (search "
0 commit comments