-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_playlist_file.py
More file actions
152 lines (128 loc) · 4.61 KB
/
create_playlist_file.py
File metadata and controls
152 lines (128 loc) · 4.61 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
import requests
import json
import time
# === CONFIGURATION ===
SPOTIFY_ACCESS_TOKEN = ""
YOUTUBE_ACCESS_TOKEN = ""
SPOTIFY_PLAYLIST_ID = "" # Only the ID, not full URL
MAX_SONGS = 100 # Adjust as needed
SPOTIFY_API_BASE = "https://api.spotify.com/v1"
YOUTUBE_API_BASE = "https://www.googleapis.com/youtube/v3"
# === STEP 1: Get Songs from a Spotify Playlist ===
def get_spotify_playlist_songs(playlist_id, max_songs=None):
headers = {"Authorization": f"Bearer {SPOTIFY_ACCESS_TOKEN}"}
url = f"{SPOTIFY_API_BASE}/playlists/{playlist_id}/tracks?limit=100"
songs = []
while url:
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Failed to fetch playlist songs: {response.status_code} — {response.text}")
break
data = response.json()
for item in data.get('items', []):
track = item.get('track')
if track:
name = track['name']
artists = ', '.join(artist['name'] for artist in track['artists'])
query = f"{artists} {name}"
songs.append(query)
# Optional cap
if max_songs and len(songs) >= max_songs:
return songs
url = data.get('next') # Spotify handles pagination via `next`
time.sleep(0.1) # Throttle to be API-friendly
return songs
# === STEP 2: Search YouTube Video ===
def search_youtube_video(query):
headers = {"Authorization": f"Bearer {YOUTUBE_ACCESS_TOKEN}"}
params = {
'part': 'snippet',
'q': query,
'maxResults': 1,
'type': 'video'
}
response = requests.get(f"{YOUTUBE_API_BASE}/search", headers=headers, params=params)
if response.status_code != 200:
print(f"Failed YouTube search: {response.status_code} — {query}")
return None
items = response.json().get('items', [])
if items:
return items[0]['id']['videoId']
return None
# === STEP 3: Create YouTube Playlist ===
def create_youtube_playlist(title, description="Imported from Spotify Playlist"):
headers = {
"Authorization": f"Bearer {YOUTUBE_ACCESS_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"snippet": {
"title": title,
"description": description
},
"status": {
"privacyStatus": "private"
}
}
response = requests.post(
f"{YOUTUBE_API_BASE}/playlists?part=snippet,status",
headers=headers,
data=json.dumps(payload)
)
if response.status_code != 200:
print(f"Failed to create playlist: {response.status_code} — {response.text}")
return None
return response.json().get("id")
# === STEP 4: Add Video to YouTube Playlist ===
def add_video_to_playlist(video_id, playlist_id):
headers = {
"Authorization": f"Bearer {YOUTUBE_ACCESS_TOKEN}",
"Content-Type": "application/json"
}
payload = {
"snippet": {
"playlistId": playlist_id,
"resourceId": {
"kind": "youtube#video",
"videoId": video_id
}
}
}
response = requests.post(
f"{YOUTUBE_API_BASE}/playlistItems?part=snippet",
headers=headers,
data=json.dumps(payload)
)
if response.status_code not in (200, 201):
print(f"Failed to add video {video_id} to playlist: {response.status_code}")
return False
return True
# === MAIN EXECUTION ===
def main():
print("▶ Fetching songs from Spotify playlist...")
songs = get_spotify_playlist_songs(SPOTIFY_PLAYLIST_ID)
print(f"🎵 Found {len(songs)} songs in playlist.")
with open("playlist.txt", "a") as f:
for line in songs:
f.write(f"{line}\n")
# print("📺 Creating YouTube playlist...")
# playlist_title = "Spotify Playlist to YouTube"
# playlist_id = create_youtube_playlist(playlist_title)
# if not playlist_id:
# print("❌ Could not create YouTube playlist.")
# return
print("🔁 Searching on YouTube and adding to playlist...")
for idx, song in enumerate(songs, 1):
print(f"[{idx}/{len(songs)}] Searching: {song}")
# video_id = search_youtube_video(song)
# if video_id:
# if add_video_to_playlist(video_id, playlist_id):
# print(" ✅ Added successfully")
# else:
# print(" ❌ Failed to add")
# else:
# print(" ❌ No video found")
# time.sleep(1.1) # Be gentle with YouTube's API
print("✅ Done!")
if __name__ == "__main__":
main()