0

I'm new in python and want to do my first project by inserting a Youtube music video URL and creating a playlist in Spotify that contains the song in the URL.

For some reason, my script is not working. I would like to know why. Once I run the script, I got this back :

Initialising Spotify Client....

                User authentication requires interaction with your
                web browser. Once you enter your credentials and
                give authorization, you will be redirected to
                a url.  Paste that url you were directed to to
                complete the authorization.

and then I copy the url to the console (the page is not working cause it's "'http://localhost:8888/callback/'") and that's it; my script does not run any further.

THIS IS MY SCRIPT -->

import json
import requests
from youtube_dl import YoutubeDL
import spotipy.util as util
import spotipy

username = '***' #personal
client_id = '***'  #personal
client_secret = '***'  #personal
redirect_uri = 'http://localhost:8888/callback/'
scope = 'playlist-modify-private'


def init_spotify_client():
        print('Initialising Spotify Client....')
        token = util.prompt_for_user_token(username, scope,
                                           client_id=client_id,
                                           client_secret=client_secret,
                                           redirect_uri=redirect_uri)
        client_token = spotipy.Spotify(auth=token)
        print('\nClient initialised!\n')
        return client_token

class CreatePlaylist:
        def __init__(self,youtube_url):
            self.youtube_url = youtube_url

        def info(self,youtube_url):
            # use youtube_dl to collect the song name & artist name
            video = YoutubeDL().extract_info(youtube_url, download=False)
            song_name = video["track"]
            artist = video["artist"]

        def create_playlist(self):
            """Create A New Playlist"""
            request_body = json.dumps({
                "name": "Youtube Liked Vids",
                "description": "All Liked Youtube Videos",
                "public": False
            })

            query = "https://api.spotify.com/v1/users/{}/playlists".format(username)
            response = requests.post(
                query,
                data=request_body,
                headers={
                    "Content-Type": "application/json",
                    "Authorization": "Bearer {}".format(init_spotify_client())
                }
            )
            response_json = response.json()

            # playlist id
            return response_json["id"]

        def add_song_to_playlist(self,song_name,artist):
            query = "https://api.spotify.com/v1/search?query=track%3A{}+artist%3A{}&type=track&offset=0&limit=20".format(
                song_name,
                artist
            )
            response = requests.get(
                query,
                headers={
                    "Content-Type": "application/json",
                    "Authorization": "Bearer {}".format(init_spotify_client())
                }
            )
            response_json = response.json()
            songs = response_json["tracks"]["items"]

            # only use the first song
            uri = songs[0]["uri"]

            # create a new playlist
            playlist_id = self.create_playlist()

            # add all songs into new playlist
            request_data = json.dumps(uri)

            query = "https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id)

            response = requests.post(
                query,
                data=request_data,
                headers={
                    "Content-Type": "application/json",
                    "Authorization": "Bearer {}".format(init_spotify_client())
                }
            )
            response_json = response.json()
            return response_json

def main():
        init_spotify_client()
        url = "https://www.youtube.com/watch?v=Bx5QlA8xp28"
        cp = CreatePlaylist(url)
main()

Please tell me what's wrong:/

NatFar
  • 2,090
  • 1
  • 12
  • 29
Moran
  • 1

1 Answers1

0

You haven't called the add_song_to_playlist method. From my understanding you are passing the song_name and artist variables from the info method to call add_song_to_playlist. If this is the case you need to add a return statement to the info method like so:

def info(self,youtube_url):
    # use youtube_dl to collect the song name & artist name
    video = YoutubeDL().extract_info(youtube_url, download=False)
    song_name = video["track"]
    artist = video["artist"]
    return song_name, artist

In your main function you need to call the info method and use the result of that to call add_song_to_playlist:

def main():
    init_spotify_client()
    url = "https://www.youtube.com/watch?v=Bx5QlA8xp28"
    cp = CreatePlaylist(url)
    song_name, artist = cp.info()
    res = cp.add_song_to_playlist(song_name, artist)
schwab09
  • 51
  • 4
  • Thank you for your answer. i tried what you say but it's stiil not running any further than the "Initialising Spotify Client...." – Moran Feb 28 '20 at 16:36