-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp-dlp.py
executable file
·178 lines (144 loc) · 6.31 KB
/
sp-dlp.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
178
#!/usr/bin/env python
import os
import requests
import shutil
import yt_dlp
import spotipy
from configparser import ConfigParser
from spotipy.oauth2 import SpotifyClientCredentials
from youtubesearchpython import VideosSearch
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC, error
# Ask user for playlist link
playlist_id = input("Your Playlist Link: ")
cfg_file_path = "config.cfg"
# Check if the configuration file exists
if not os.path.exists(cfg_file_path):
# Define the template configuration file content with placeholders
cfg_template = """
[Spotify]
client_id = {client_id}
client_secret = {client_secret}
"""
# Get user input for each configuration value
client_id = input("Enter your spotify client id: ")
client_secret = input("Enter your spotify client secret: ")
# Replace placeholders in the template with user input
cfg_content = cfg_template.format(client_id=client_id, client_secret=client_secret)
# Write the configuration content to a cfg file
with open(cfg_file_path, "w") as cfg_file:
cfg_file.write(cfg_content)
print("Configuration saved to 'config.cfg'")
else:
print("Configuration file already exists. Skipping input prompts.")
# Parse config from config.cfg
config = ConfigParser()
config.read('config.cfg')
# Get user-specific variables from config.cfg
client_id = config.get('Spotify', 'client_id')
client_secret = config.get('Spotify', 'client_secret')
# Set up Spotify client
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Function to get playlist metadata from Spotify
def get_playlist_metadata(playlist_id):
playlist = sp.playlist(playlist_id)
playlist_data = []
for item in playlist["tracks"]["items"]:
track = item["track"]
track_info = {
"track_name": track["name"],
"artist_name": [artist["name"] for artist in track["artists"]][0],
"album_name": track["album"]["name"],
"release_date": track["album"]["release_date"],
"album_cover_url": track["album"]["images"][0]["url"] if track["album"]["images"] else None,
}
playlist_data.append(track_info)
return playlist_data
# Function to search, download, and add metadata to the audio file
def download_and_embed_metadata(track):
search_query = f"{track['track_name']} {track['artist_name']} official audio"
videos_search = VideosSearch(search_query, limit=1)
search_results = videos_search.result()
if search_results["result"]:
video_url = f"https://www.youtube.com/watch?v={search_results['result'][0]['id']}"
filename = f"{track['track_name']} - {track['artist_name']}.mp3"
# Download the YouTube video as an MP3
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': f"{track['track_name']} - {track['artist_name']}",
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
print(f"Downloading {track['track_name']} by {track['artist_name']} from YouTube: {video_url}")
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
# Add metadata to the downloaded MP3 file
add_metadata(filename, track)
# Function to add metadata to the downloaded MP3 file
def add_metadata(filename, track):
audio = EasyID3(filename)
audio["title"] = track["track_name"]
audio["artist"] = track["artist_name"]
audio["album"] = track["album_name"]
audio["date"] = track["release_date"]
audio.save()
# If album cover URL is available, download and add it as album art
if track["album_cover_url"]:
try:
response = requests.get(track["album_cover_url"])
response.raise_for_status() # Check if the request was successful
album_cover_data = response.content
audio = ID3(filename)
audio["APIC"] = APIC(
encoding=3, # UTF-8
mime="image/jpeg", # MIME type for JPEG images
type=3, # Cover (front)
desc="Cover",
data=album_cover_data
)
audio.save(v2_version=3)
print(f"Metadata and album art added to {filename}")
except (requests.RequestException, error) as e:
print(f"Could not add album cover: {e}")
def moveToDownloads():
# Define the current directory and the new folder name
playlist = sp.playlist(playlist_id)
playlist_name = playlist["name"]
# Define the name for the Downloads directory and the subdirectory
downloads_directory = "Downloads"
subdirectory = playlist_name
# Get the current working directory
current_directory = os.getcwd()
# Create the full path for the Downloads directory
path_downloads_directory = os.path.join(current_directory, downloads_directory)
# Create the Downloads directory
os.makedirs(path_downloads_directory, exist_ok=True)
# Create the full path for the subdirectory inside Downloads
path_subdirectory = os.path.join(path_downloads_directory, subdirectory)
# Create the subdirectory
os.makedirs(path_subdirectory, exist_ok=True)
# Move all mp3 files from the current directory to the subdirectory
for filename in os.listdir(current_directory):
if filename.endswith('.mp3'):
# Construct full file path
file_path = os.path.join(current_directory, filename)
# Construct the destination file path
destination_file_path = os.path.join(path_subdirectory, filename)
# Check if the file already exists at the destination
if os.path.exists(destination_file_path):
# If it exists, remove the existing file
os.remove(destination_file_path)
# Move the mp3 file to the subdirectory
shutil.move(file_path, path_subdirectory)
# Main function
def download_spotify_playlist(playlist_id):
playlist_data = get_playlist_metadata(playlist_id)
for track in playlist_data:
download_and_embed_metadata(track)
moveToDownloads()
# Call main function
download_spotify_playlist(playlist_id)