-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
165 lines (132 loc) · 4.97 KB
/
main.py
File metadata and controls
165 lines (132 loc) · 4.97 KB
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
try:
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import yt_dlp
import os
import configparser
import concurrent.futures
import signal
import sys
from tqdm import tqdm
except Exception as e:
print(e)
print("Please install the required packages using: pip install -r requirements.txt")
exit(1)
# Read config file
config = configparser.ConfigParser()
config.read("config.ini")
# Load Spotify API credentials
try:
SPOTIFY_CLIENT_ID = config["spotify"]["client_id"]
SPOTIFY_CLIENT_SECRET = config["spotify"]["client_secret"]
SPOTIFY_REDIRECT_URI = config["spotify"]["redirect_uri"]
THREAD_COUNT = int(config["settings"]["thread_count"]) # Read thread count from config
PLAYLIST_NAME = config["settings"]["playlist_name"] # Read playlist name
except KeyError:
raise ValueError("❌ Missing settings in config.ini")
# Spotify API Authentication
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
client_id=SPOTIFY_CLIENT_ID,
client_secret=SPOTIFY_CLIENT_SECRET,
redirect_uri=SPOTIFY_REDIRECT_URI,
scope="user-library-read playlist-read-private"
))
# Ensure downloads folder exists
os.makedirs("downloads", exist_ok=True)
# Safe exit handler
executor = concurrent.futures.ThreadPoolExecutor(max_workers=THREAD_COUNT)
def graceful_exit(signal, frame):
print("\n🛑 Stopping downloads safely...")
executor.shutdown(wait=True) # Wait for active threads to finish
sys.exit(0)
signal.signal(signal.SIGINT, graceful_exit)
# Fetch liked songs
def get_liked_songs():
liked_songs = []
results = sp.current_user_saved_tracks()
while results:
for item in results['items']:
track = item['track']
song_name = track['name']
artist_name = track['artists'][0]['name']
liked_songs.append(f"{song_name} - {artist_name}")
results = sp.next(results) if results['next'] else None # Pagination
return liked_songs
# Fetch songs from a specific playlist
def get_playlist_songs(playlist_name):
playlists = sp.current_user_playlists(limit=50) # Get all playlists
playlist_id = None
for playlist in playlists["items"]:
if playlist["name"].lower() == playlist_name.lower():
playlist_id = playlist["id"]
break
if not playlist_id:
print(f"❌ Playlist '{playlist_name}' not found! Defaulting to liked songs.")
return get_liked_songs()
songs = []
results = sp.playlist_tracks(playlist_id)
while results:
for item in results['items']:
track = item['track']
song_name = track['name']
artist_name = track['artists'][0]['name']
songs.append(f"{song_name} - {artist_name}")
results = sp.next(results) if results['next'] else None # Pagination
return songs
# Search for a song on YouTube
def search_youtube(query):
ydl_opts = {
'quiet': True,
'default_search': 'ytsearch',
'format': 'bestaudio/best'
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
result = ydl.extract_info(f"ytsearch:{query}", download=False)
if 'entries' in result:
return result['entries'][0]['webpage_url']
return None
# Download the song as MP3 (with retry logic)
def download_song(song, retries=3):
file_path = f"downloads/{song}.mp3"
if os.path.exists(file_path):
print(f"✅ {song} already exists. Skipping download.")
return
video_url = search_youtube(song)
if not video_url:
print(f"❌ No match found for {song}")
return
print(f"⬇ Downloading: {song} from {video_url}")
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': file_path.replace(".mp3", ".%(ext)s"), # Correct file format
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}],
'socket_timeout': 30 # Increase timeout to prevent failures
}
for attempt in range(retries):
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
print(f"✅ Downloaded {song}")
return
except Exception as e:
print(f"⚠️ Error downloading {song}: {e}")
if attempt < retries - 1:
print("🔄 Retrying...")
else:
print("❌ Failed after multiple attempts.")
# Main function: Fetch, Search, Download
def main():
if PLAYLIST_NAME.lower() == "liked songs":
songs = get_liked_songs()
else:
songs = get_playlist_songs(PLAYLIST_NAME)
print(f"🎶 Found {len(songs)} songs from '{PLAYLIST_NAME}'. Downloading with {THREAD_COUNT} threads...\n")
with concurrent.futures.ThreadPoolExecutor(max_workers=THREAD_COUNT) as executor:
list(tqdm(executor.map(download_song, songs), total=len(songs), desc="Downloading Songs"))
print("✅ All downloads complete!")
if __name__ == "__main__":
main()