I'm trying to transfer my playlists from spotify to Youtube music through API but getting quota exceeded Error!
I see youtube only allows 10K queries quota per day which is way to less to achieve this as
For each song:
- Search YouTube for a song – This operation makes one API call to the search endpoint to find the song.
- Add a song to the YouTube playlist – This operation makes another API call to the playlistItems endpoint to add the song to the playlist.
Each song in the script would typically require two API calls:
One for searching the song.One for adding the song to the playlist.
Here's is the complete script
import spotipyfrom spotipy.oauth2 import SpotifyOAuthfrom googleapiclient.discovery import buildimport webbrowserimport osimport google_auth_oauthlib.flowimport googleapiclient.discoveryimport googleapiclient.errorsimport google.auth.transport.requests# Spotify credentialsSPOTIFY_CLIENT_ID = 'a762........16fa8'SPOTIFY_CLIENT_SECRET = '2568e1......d2f72'SPOTIFY_REDIRECT_URI = 'https://www.exapmle.me'# Authenticate with Spotifydef authenticate_spotify(): scope = "playlist-read-private" sp_oauth = SpotifyOAuth(client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET, redirect_uri=SPOTIFY_REDIRECT_URI, scope=scope) # Open the authorization URL manually if the web browser fails to launch auth_url = sp_oauth.get_authorize_url() print(f"Opening this URL in your browser to authorize the app: {auth_url}") webbrowser.open(auth_url) # Prompt the user to enter the URL they were redirected to after authorization redirect_response = input("Paste the full redirect URL here: ") # Get the access token using the authorization code from the redirect response token_info = sp_oauth.get_access_token( code=sp_oauth.parse_response_code(redirect_response)) if token_info: sp = spotipy.Spotify(auth=token_info['access_token']) return sp else: print("Could not retrieve token. Check your credentials and OAuth setup.") return None# authenticate with youtubedef authenticate_youtube(): # Disable OAuthlib's HTTPS verification when running locally. CLIENT_SECRETS_FILE = '/Users/spotifyToYTMusic/oauth_client_secret.json' # Specify the scopes you need to access YouTube Data API SCOPES = ["https://www.googleapis.com/auth/youtube.force-ssl"] # *DO NOT* leave this option enabled in production. os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" # Get credentials and create an API client flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file( CLIENT_SECRETS_FILE, SCOPES) credentials = flow.run_local_server(port=8080) youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials) return youtube# Fetch all Spotify playlistsdef get_spotify_playlists(sp): playlists = [] results = sp.current_user_playlists(limit=50) playlists.extend(results['items']) # Handle pagination (in case the user has more than 50 playlists) while results['next']: results = sp.next(results) playlists.extend(results['items']) return playlists# Fetch tracks from a specific playlistdef get_spotify_playlist_tracks(sp, playlist_id): tracks = [] results = sp.playlist_tracks(playlist_id, limit=100) tracks.extend(results['items']) # Handle pagination for tracks while results['next']: results = sp.next(results) tracks.extend(results['items']) return tracks# Search for songs on YouTubedef search_youtube(youtube, query): request = youtube.search().list( part="snippet", q=query, maxResults=1, type="video" ) response = request.execute() return response['items'][0]['id']['videoId'] if response['items'] else None# Add song to YouTube playlistdef add_song_to_youtube_playlist(youtube, video_id, playlist_id): request = youtube.playlistItems().insert( part="snippet", body={"snippet": {"playlistId": playlist_id,"resourceId": {"kind": "youtube#video","videoId": video_id } } } ) response = request.execute() return response# Create a YouTube playlistdef create_youtube_playlist(youtube, title, description=""): request = youtube.playlists().insert( part="snippet,status", body={"snippet": {"title": title,"description": description },"status": {"privacyStatus": "private" } } ) response = request.execute() return response['id']def main(): # Step 1: Authenticate with Spotify sp = authenticate_spotify() # Step 2: Get all the user's Spotify playlists playlists = get_spotify_playlists(sp) # Step 3: Authenticate YouTube # youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY) youtube = authenticate_youtube() # Step 4: Loop through each playlist for playlist in playlists: print(f"Processing Spotify playlist: {playlist['name']}") # Step 5: Fetch all tracks in the current playlist tracks = get_spotify_playlist_tracks(sp, playlist['id']) # Step 6: Create a corresponding YouTube playlist youtube_playlist_id = create_youtube_playlist( youtube, playlist['name']) print(f"Created YouTube playlist: {playlist['name']}") # Step 7: For each track, search for it on YouTube and add it to the playlist for item in tracks: track = item['track'] track_name = track['name'] artist_name = track['artists'][0]['name'] query = f"{track_name} {artist_name}" print(f"Searching YouTube for {query}...") video_id = None try: video_id = search_youtube(youtube, query) except Exception as e: print( f"An error occurred while searching YouTube for {query}.") print(e) continue if video_id: try: add_song_to_youtube_playlist( youtube, video_id, youtube_playlist_id) print( f"Added {track_name} by {artist_name} to YouTube playlist.") except Exception as e: print( f"An error occurred while adding {track_name} by {artist_name} to the YouTube playlist.") print(e) else: print( f"Could not find {track_name} by {artist_name} on YouTube.")if __name__ == "__main__": main()Breakdown from the Output:
For the first playlist (Playlist 1):
30 successful searches and additions.2 failed additions (these still consumed search API calls).Total queries for this playlist: 32 searches + 30 additions = 62 API queries.
For the second playlist (Playlist 2):
30 successful searches and additions.1 failed addition and 1 failed search (these still consumed search API calls).Total queries for this playlist: 31 searches + 30 additions = 61 API queries.Total Queries:Combining both playlists, the total queries before the quota was exceeded:
62 API queries for the first playlist.61 API queries for the second playlist.
Thus, the script made 123 API queries in total.
YouTube API v3 typically offers a quota of 10,000 units per day, with each search request consuming 100 units and each playlist insertion (adding a video) consuming 50 units. Therefore, based on the query types:
Searches: 63 searches (31 + 32) → 6,300 units.Additions: 60 successful additions → 3,000 units.
This means the script used 9,300 units, which explains why the quota was nearly exceeded.
Anyone have any idea how can I get around this quota limit please let me know any help is appreciated, Thanks!
P.S. : I've applied for more quota but have not received any response from google team yet