-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.py
More file actions
55 lines (41 loc) · 1.48 KB
/
extractor.py
File metadata and controls
55 lines (41 loc) · 1.48 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
import os
from spotipy.oauth2 import SpotifyOAuth
import spotipy
from dotenv import load_dotenv
load_dotenv()
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
client_id=os.getenv("SPOTIPY_CLIENT_ID"),
client_secret=os.getenv("SPOTIPY_CLIENT_SECRET"),
redirect_uri=os.getenv("SPOTIPY_REDIRECT_URI"),
scope="playlist-read-private"
))
def extract_user_id(link: str):
if "open.spotify.com" in link:
return link.split("user/")[-1].split("?")[0]
elif "spotify:user:" in link:
return link.split(":")[-1]
else:
raise ValueError("Invalid Spotify user link format.")
user_link = input("Enter Spotify user link: ").strip()
user_id = extract_user_id(user_link)
print(f"\nFetching playlists for user ID: {user_id}...\n")
# Fetch public playlists
playlists = sp.user_playlists(user_id)
for playlist in playlists['items']:
print(f"🎵 Playlist: {playlist['name']}")
print(f" URL: {playlist['external_urls']['spotify']}")
ep = input("Enter the playlist name you wanna extract")
target = None
for p in playlists['items']:
if p['name'].lower() == ep.lower():
target = p
break
if target is None:
print("❌ Playlist not found. Make sure the name matches exactly.")
else:
print(f"\n🎵 Extracting: {target['name']}")
playlist_id = target['id']
tracks = sp.playlist_tracks(playlist_id)
for item in tracks['items']:
track = item['track']
print(f" - {track['name']} by {track['artists'][0]['name']}")