Go to the Spotify Developer Dashboard and log in with your Spotify account.
Click on the "Create an App" button and fill out the necessary information, such as the name and description of your application.
Once you've created the app, you'll be taken to the app dashboard. Here, you'll find your client ID and client secret, which are used to authenticate your application with the Spotify API.
classSpotifyClient:def__init__(self)->None:client_id="<your_client_id>"client_secret="<your_client_secret>"auth_manager=SpotifyClientCredentials(client_id=client_id,client_secret=client_secret)self.spotify=spotipy.Spotify(auth_manager=auth_manager)defget_playlist(self,id:str):playlist=self.spotify.playlist(id)queries=[]tracks=playlist['tracks']['items']fortrackintracks:track_name=track['track']['name']artists=', '.join([artist['name']forartistintrack['track']['artists']])queries.append(f'{track_name} by {artists}')return(Playlist(playlist['name'],playlist['description'],queries))
On class initialization, login into the Spotify using credentials and get_playlist method fetch the playlist name, description and track name for search query and return a Playlist.
YouTube part
Go to the Google Cloud Console, sign in with your Google account, and create a new project.
Once your project is created, select it from the project dropdown menu in the top navigation bar.
Go to the Credentials page in the API & Services section of the left sidebar.
Click on the "Create Credentials" button and select "OAuth client ID".
After creating select edit button in the OAuth 2.0 Client IDs, Under 'Authorized JavaScript origins' add this URI http://localhost:8080 and under "Authorized redirect URIs
" add this URI http://localhost:8080/ and then click save.
Click the download button to download the credentials in your project directory. Rename the file to client_secret.json
Go to the OAuth consent screen in the API & Services section of the left sidebar. Under test user add your Gmail id.
On class initialization, login into the YouTube using OAuth. Method create_playlist just create playlist with given name and description and add_song_playlist method add video to the given playlist.
Driving Code
Finally to drive a code, create new file, main.py
importsysimporttimefromspotify_clientimportSpotifyClientfromyoutube_clientimportYouTubeClientspotify=SpotifyClient()youtube=YouTubeClient()spotify_playlist_id=sys.argv[1]spotify_playlist=spotify.get_playlist(spotify_playlist_id)youtube_playlist_id=youtube.create_playlist(spotify_playlist.name,spotify_playlist.description)['id']fortrackinspotify_playlist.tracks:print(f"Searching for {track}")id=youtube.search_video(track)youtube.add_song_playlist(youtube_playlist_id,id)print("Added...")time.sleep(1)print("Done ๐")print(f"https://www.youtube.com/playlist?list={youtube_playlist_id}")YougetplaylistidfromtheplaylistURLofspotify.Inthisscript,theplaylistidgetascommandlineargs.
Run this script
python main.py <playlist_id>
This will open a browser window to login into the google account. Just login into it. After execution of script it will print the playlist link.