I have 5000+ mp3 files stored in my windows computer. I have writen a python script that comunicates with Youtube Music and almost succesfully creates a playlist with the songs (See script below). I say almost succesfully because the gigantic issue is the YouTube Data API v3 daily quota (10000).
According to what I understand, after circa 60-70 songs, I hit the daily quota so the script stops including songs into the playlist. It seems that I am so close, but I have no idea how to solve this. Here is the code:
import time
import os
import re
import eyed3
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
#import request
#from google.auth.transport.request
# API key from your Google Cloud Console project
api_key = "include_key_here"
# OAuth 2.0 flow
#flow = InstalledAppFlow.from_client_secrets_file(
# "client_secrets.json", scopes=["https://www.googleapis.com/auth/youtube"]
#)
# Check if client_secrets.json exists in the same directory as the script
secrets_file_path = "client_secrets3.json"
if not os.path.exists(secrets_file_path):
# If not, prompt the user to provide the path manually
secrets_file_path = input("Enter the path to client_secrets.json: ")
flow = InstalledAppFlow.from_client_secrets_file(secrets_file_path, scopes=["https://www.googleapis.com/auth/youtube"])
credentials = flow.run_local_server(port=0)
# Build the YouTube service
youtube = build("youtube", "v3", credentials=credentials)
# Songs to add to the playlist
def get_song_info(file_path):
try:
audiofile = eyed3.load(file_path)
if audiofile is None:
raise ValueError("Failed to load audio file")
title = audiofile.tag.title
artist = audiofile.tag.artist
return {"title": title, "artist": artist}
except (ValueError, Exception) as e:
print(f"Error processing file {file_path}: {e}")
return None
def process_folder(folder_path):
songs = []
not_identified_songs = []
for filename in os.listdir(folder_path):
if filename.endswith(".mp3"):
file_path = os.path.join(folder_path, filename)
song_info = get_song_info(file_path)
if song_info:
songs.append(song_info)
else:
not_identified_songs.append(file_path)
return songs, not_identified_songs
# Replace 'path_to_your_folder' with the actual path to your folder containing MP3 files
folder_path = 'G:\My Drive\desktop\Camilo\Todas mis músicas'
songs, not_identified_songs = process_folder(folder_path)
# Create a new playlist
playlist_response = youtube.playlists().insert(
part="snippet,status",
body={
"snippet": {
"title": "mp3 files",
"description": "yeaah",
},
"status": {"privacyStatus": "PUBLIC"}, # Set privacy to public
},
).execute()
playlist_id = playlist_response["id"]
failed_songs = [] # List to store songs that couldn't be added
for song in songs:
try:
search_response = youtube.search().list(
part="snippet",
q=f"{song['title']} {song['artist']}",
type="VIDEO",
maxResults=1,
).execute()
# Check if there are items in the search response
if "items" in search_response and search_response["items"]:
video_id = search_response["items"][0]["id"]["videoId"]
youtube.playlistItems().insert(
part="snippet",
body={
"snippet": {
"playlistId": playlist_id,
"resourceId": {"kind": "youtube#video", "videoId": video_id},
}
},
).execute()
print(f"Added {song['title']} by {song['artist']} to the playlist.")
else:
print(f"No video found for {song['title']} by {song['artist']}. Skipping.")
failed_songs.append(song)
except IndexError:
print(f"Error: No videoId found for {song['title']} by {song['artist']}. Skipping.")
failed_songs.append(song)
except Exception as e:
print(f"Error adding {song['title']} by {song['artist']} to the playlist: {e}")
failed_songs.append(song)
print("Playlist creation completed.")
# Print the list of songs that couldn't be added
if failed_songs:
print("Songs that couldn't be added to the playlist:")
for failed_song in failed_songs:
print(f"- {failed_song['title']} by {failed_song['artist']}")