Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[Settings]
client_id =yourclientid
client_secret =yourclientsecret
username =yourusername
client_secret =yourclientsecret
65 changes: 31 additions & 34 deletions spotify_to_mp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,43 @@
def write_tracks(text_file: str, tracks: dict):
# This includins the name, artist, and spotify URL. Each is delimited by a comma.
with open(text_file, 'w+', encoding='utf-8') as file_out:
while True:
for item in tracks['items']:
if 'track' in item:
track = item['track']
else:
track = item
try:
track_url = track['external_urls']['spotify']
track_name = track['name']
track_artist = track['artists'][0]['name']
album_art_url = track['album']['images'][0]['url']
csv_line = track_name + "," + track_artist + "," + track_url + "," + album_art_url + "\n"
try:
file_out.write(csv_line)
except UnicodeEncodeError: # Most likely caused by non-English song names
print("Track named {} failed due to an encoding error. This is \
most likely due to this song having a non-English name.".format(track_name))
except KeyError:
print(u'Skipping track {0} by {1} (local only?)'.format(
track['name'], track['artists'][0]['name']))
# 1 page = 50 results, check if there are more pages
if tracks['next']:
tracks = spotify.next(tracks)
for item in tracks:
if 'track' in item:
track = item['track']
else:
break
track = item
try:
track_url = track['external_urls']['spotify']
track_name = track['name']
track_artist = track['artists'][0]['name']
album_art_url = track['album']['images'][0]['url']
csv_line = track_name + "," + track_artist + "," + track_url + "," + album_art_url + "\n"
try:
file_out.write(csv_line)
except UnicodeEncodeError: # Most likely caused by non-English song names
print("Track named {} failed due to an encoding error. This is \
most likely due to this song having a non-English name.".format(track_name))
except KeyError:
print(u'Skipping track {0} by {1} (local only?)'.format(
track['name'], track['artists'][0]['name']))


def write_playlist(username: str, playlist_id: str):
results = spotify.user_playlist(username, playlist_id, fields='tracks,next,name')
playlist_name = results['name']
def write_playlist(playlist_id: str):
playlist_name = spotify.playlist(playlist_id, fields='name')['name']
text_file = u'{0}.txt'.format(playlist_name, ok='-_()[]{}')
print(u'Writing {0} tracks to {1}.'.format(results['tracks']['total'], text_file))
tracks = results['tracks']

results = spotify.playlist_tracks(playlist_id)
tracks = results["items"]
while results["next"]:
results = spotify.next(results)
tracks.extend(results["items"])

print(u'Writing {0} tracks to {1}.'.format(len(tracks), text_file))
write_tracks(text_file, tracks)

imgURLs = [];
imgURLs = []
for item in tracks['items']:
imgURLs.append(item['track']['album']['images'][0]['url']);
imgURLs.append(item['track']['album']['images'][0]['url'])
return playlist_name, imgURLs

def find_and_download_songs(reference_file: str):
Expand Down Expand Up @@ -234,18 +233,16 @@ def enable_multicore(autoenable=False, maxcores=None, buffercores=1):
config.read("config.ini")
client_id = config["Settings"]["client_id"]
client_secret = config["Settings"]["client_secret"]
username = config["Settings"]["username"]
else:
client_id = input("Client ID: ")
client_secret = input("Client secret: ")
username = input("Spotify username: ")
playlist_uri = input("Playlist URI/Link: ")
if playlist_uri.find("https://open.spotify.com/playlist/") != -1:
playlist_uri = playlist_uri.replace("https://open.spotify.com/playlist/", "")
multicore_support = enable_multicore(autoenable=False, maxcores=None, buffercores=1)
auth_manager = oauth2.SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
spotify = spotipy.Spotify(auth_manager=auth_manager)
playlist_name, albumArtUrls = write_playlist(username, playlist_uri)
playlist_name, albumArtUrls = write_playlist(playlist_uri)
reference_file = "{}.txt".format(playlist_name)
# Create the playlist folder
if not os.path.exists(playlist_name):
Expand Down