-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspotify-downloader.py
69 lines (55 loc) · 2.59 KB
/
spotify-downloader.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
import os
from dotenv import load_dotenv
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
# Load environment variables from .env file
load_dotenv()
# Get the absolute path of the current script
script_directory = os.path.dirname(os.path.abspath(__file__))
# Create or define the Music directory
music_directory = os.path.join(script_directory, "Music")
os.makedirs(music_directory, exist_ok=True)
def parse_spotify_item(url):
# Determine whether the URL is an album or playlist
url_type = url.split('/')[-2]
# Extract the ID from the URL
id = url.split('/')[-1].split('?')[0]
# Authenticate with the Spotify API using client credentials
client_id = os.environ.get('SPOTIFY_CLIENT_ID')
client_secret = os.environ.get('SPOTIFY_CLIENT_SECRET')
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
# Get the details from the API
if url_type == "album":
album = sp.album(id)
artist = album['artists'][0]['name']
album_name = album['name']
return [(artist, album_name, id, url_type)] # Returns album id for albums
elif url_type == "playlist":
playlist = sp.playlist(id)
return [(track['track']['artists'][0]['name'], track['track']['album']['name'], track['track']['id'], url_type) for track in playlist['tracks']['items']]
else:
raise ValueError("URL must be an album or playlist")
# Read the file containing the list of URLs
file_path = os.path.join(script_directory, 'urls.txt')
with open(file_path, 'r') as file:
urls = file.read().splitlines()
# Process each URL
for url in urls:
items = parse_spotify_item(url)
for artist, album_name, id, url_type in items:
# Create the artist folder inside the Music folder
artist_folder = os.path.join(music_directory, artist)
os.makedirs(artist_folder, exist_ok=True)
# Create the album folder inside the artist folder
album_folder = os.path.join(artist_folder, album_name)
os.makedirs(album_folder, exist_ok=True)
# Run a bash command inside the album folder
os.chdir(album_folder)
# if album, download album once, if playlist, download tracks
if url_type == "album":
os.system(f"spotdl download 'https://open.spotify.com/album/{id}'") # Download the entire album
else:
os.system(f"spotdl download 'https://open.spotify.com/track/{id}'") # Download specific track
# Move back to the Music directory after each iteration
os.chdir(music_directory)