Get Currently Playing Track with Spotify API (Python Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone and welcome to the video this week i actually spent some time getting vs code up and running for python now if you know me you know that i usually use pytram or sublime and they're like my favorite editors including vim2 the last time i tried vs code for python it didn't go too well and i ended up raising a few issues on the github page since then they've made a lot of changes and it's working so far so let's see how the week goes now not long ago i made a tutorial of taking a music video on youtube and putting it into your spotify playlist using python now if you haven't already do check the video out i'll link to it above but in that video a few of you mentioned that you want to see a different tutorial one of you said that you're thinking about building a discord bot and you want to be able to get the current playing track on spotify i tinkered around to see if it was possible and it turns out the spotify api does offer this so in this video tutorial today we're going to walk through the entire process of writing a python program that gets the current playing song on spotify we're also going to up it a notch so if a user changes what they're listening to so for example if they skip a song if they go back we get updated and our python program then prints that out now please do smash that like button i'm trying my best to grow this community so i can hopefully teach you programming in a fun and more interesting way with that said i just want to give a quick shout out to jackson and nick who's been a longtime subscriber of mine he's continued to leave feedback on my community channel so thank you jackson um hopefully my content is helping you to learn programming with that said buckle your seatbelts and let's get coding [Applause] right so the first thing we're going to do is we're going to have our classic if name main check and essentially this is just going to tell python where to start the program so uh we'll create a function called main and then up above here i'll leave it empty for now the first thing we're going to have is a variable called current track info and what this is going to do is it's going to call a function or store the results of a function called get current track and get contract is only going to take one argument and that is going to be the spotify access token and the idea is that this current track info uh we're going to make it a json or a dictionary and this guaran get current track uh function is going to return that dictionary and so to give you the idea of what that dictionary is going to look like or what we want it to look like i'm going to code this up is id and this is going to be the unique id that comes back from spotify identifying the song that's currently playing and then we're going to have a name for the song so that's let's just say the song is called num and then the artists because you can have one or more arches for a song but in this case num only has one artist and that's linkin park so the idea again is just that we're gonna have a dictionary um and get current track is gonna return that and we're gonna store that in current track info so let me get rid of this and what we'll do is in this program we're just going to print out using p print p print stands for pretty print and it's a nicer way of printing out dictionaries and so i quite i use it quite often actually and i'm going to pass in another argument called indent and i'll give it the value for and essentially what this is going to do is going to print out the dictionary in a nicely formatted way and we're going to make sure the indent or the keys are indented by four spaces so i'm going to import preprint from above and that's it and that looks good really now maybe in the tutorial what we'll do is we'll set up a while loop to run this every second i'll give you a preview of what that looks like but for now this is just gonna uh get the current track that's playing as a one-off now this spotify access token we're gonna define above um usually you put this in the environment variable um but again this is just gonna be a script that we're running locally so uh you know security can be a bit loose and in this case we're just gonna have it at the top of the file and we're gonna get that a bit later on in the tutorial i'll show you how you can get an access token so now it's time to implement this uh method called get current track and i'm gonna create above here so def get or get current track and again this is just going to take one argument called access token and i'm going to leave it empty for now now the way to get current track is going to work is we're going to issue out a request to the spotify api we're going to give it the access token we're going to get back the data and then what we're going to need to do is we're going to need to process that data because it's you know it's as you can imagine it's in a format that we're going to need to make a bit more friendly for the purposes of this program so the first thing we're going to do is we're going to create a variable called response and that's going to store the result of uh calling out to the spotify api and we're going to use the request library to do that so i'm going to import request above let's do that request above and then request and then the type of request that we're going to issue is a get request and this is going to call out to a url which we'll call spotify get current track url and this one we're going to define above and we're going to need to get that from a documentation so we'll get that in a second to pass in the authorization we're going to pass in the headers or value for the headers parameter and this takes a dictionary and it's a number of headers that you can pass in well when you pass one which is the authorization and the value of that is going to be equal to i'm going to set it equal to an f string because it's nice um and then it's going to be bearer space the access token and this is where the f string quite cool because you can just pass in the braces you pass in the access token and python will substitute that into the string for you so that looks good really that's really the basis of our request and the next thing that we want to do is we want to pass the response right and so the next thing we want to do given that it's a json response and it's quite handy we're going to make use of a function that's provided by the request library so first of all we're going to create a variable called json it's shorthand for a response jason and whenever you make a or whenever you make an api call using the requests library so if it's request.get post whatever it gives you back a response object and that's really handy because on that response object you have a method called dot json right so again i've stored the the result of request.get in this variable called response and then on that you have a method called.json which essentially will give you back a json response and it's quite nice because essentially it's a python dictionary that you can work with and it just makes it easier to process right so i've switched over to the spotify documentation because i want to show you uh the method that we're going to use so the endpoint we're going to call and more importantly the response that we get back because we need to understand the structure so that again we can you know write the code that's going to be able to process that response uh accordingly and then the next thing we want to do is we want to scroll down a bit now i've clicked try it which allows you to be able to you know execute a request on this web console and see the day that you get back so you don't actually need to write any code here it's quite handy and as you can tell i've got this response i've got a song currently playing on spotify on my phone and as you can tell it's got a bit of information about the device so currently i'm playing a song on my iphone and so it tells you a bit more about the device that's playing the song and then here you can sort of get an idea of like um the shuffle and the repeat state so shuffles on and uh more importantly you've got a key called item and this is essentially where it has all the data about the current playing song now as you can tell within that you have another dictionary so as you can tell there's a lot of nesting going on and but in here you have an album but we don't really we're not really bothered about that what we're looking for is essentially an id so we've got an idea here that's quite cool um and then we've also got a key artist so that's going to be handy because we want to be able to see all the different artists for a current song so as you can tell um for this song there are a couple of artists uh so that's good to know because we're going to need to uh join or we're going to need to be able to you know join all those artists together and put it into a string that we get back and then that's about it bar one more thing of course which is probably the most important thing which is the name so that's the name of the song and then what we might do actually is we might also make use of the uh href or the uh external euro actually the external heroes probably makes more sense because this is a link out to the song so what we'll do is we'll get the current playing song and we'll also print the link out to that song so that we can click it and open it just to look so that's given us a good idea of exactly what the structure of the response looks like let's get back to the code editor where we're going to start passing the response and then build up that dictionary that's going to have all the details about the current playing song right so i'm back here and what we're going to do is we're going to start off with the kind of dictionary that we want to return so we're going to call this current track info and then this is going to be a dictionary and that's going to have the id and the idea is that's going to um correspond to the track id we're also going to have the name of the track so i'll put name here and that's going to be track name and then the next thing that we're gonna have here is the artist so again as i mentioned you're gonna have multiple artists right so we're gonna need to be able to return back let's let's return back the name of the artist i think that makes sense uh for this program i think that's good enough and then the last thing is the link and again this is going to correspond to the link that spotify use to uh point to the song cool so this gives you an idea of what we want so now let we're gonna have to actually do some processing here uh so we can get all of these uh lovely variables right so some of these are quite easy to get hold of so the track id again we saw um earlier on that it's in the response so i'm going to copy that here response json and it's under item followed by id so you know that's quite straightforward um track name is quite similar um too in that it's under item name right so getting artists is actually a bit tricky and this is because um a song can have more than one artist right so if we head back to the spotify docs as you can tell artists here is actually an array and it's an array of objects and in each object you have the artist so it tells you type artist and then you have the name of the artist and you have the id and then you also have a link a spotify link directly to the artist so that's quite cool so heading back to the code editor what we want to do is we want to get the name for each one of the artists and i think a nice way so here we've got artist's name actually that should be called artist's name um essentially this should be something like i think it'll be nice if it's something like you know lincoln park and then jc so it's essentially a comma separated uh string and and what that's going to do is if a song has multiple artists you know you it's comma separated and so i think that'll be quite nice because again when we print out we can clearly uh see that uh that given song has uh more than one artist so to actually make this happen let's get rid of this and the first thing we're going to do is we're going to have a variable called artists and this is going to be uh it's going to be the response jason the item artists and again switching back to the spotify documentation as you can tell here it's an array and so this is going to be a reign of artist objects and then for each artist object what we want to do is we want to store it into in a string right and so the way you do that is quite handy in python you have a nice way um so i'm going to call this artist name i'm going to get that from down below here artist name and then we're gonna make use of the join function so the drawing function the way it works is you give it a character so here i'm giving a comma followed by space and then you call dot join on the string and then you give it an array so i'm going to give it artists i'm going to give it artist i'm going to a list comprehension and that's going to be artist name for artists in artists right so it's a bit of a mouthful there um and that might not be the easiest thing to see so i'll put on a separate line um so let's go through this so join takes an array um and in this case we're passing it an array of artist names and what that's going to do is it's going to join each one of those with this string which is comma and a space and it's quite cool because it also handles the case where the last artist isn't going to have this leading comma and space at the end so that's quite handy it's a it's a common function used in python when you have a list of things that you want to print out in a comma separated and so drawings quite handy for that and the reason why we're doing the list comprehension here uh in all fairness what i could do is i could make a variable above for artist name but i think this is quite clear i mean well it's easier to see what's going on and i think it works well for this program so again artists um actually there should be quite artist names and then i'm going to copy that down below here and i think that looks good right and so the last one is going to be link and link is going to be a link to the song uh the spotify link to the song and again we can find that in json or response json and then in that you have item and remember it was in external urls and that had a key spotify i believe we can double check that anyway so let me just copy that if i double check that here um it's in the artists but here yeah as you can tell so you've got external urls here and then in there you have spotify uh the key and then you've got the link to the song so that looks good so that's a link really and then we can get rid of that extra line and i think we're good right so the last thing we need to do before we can get this program running actually is to get the access token and again we've left it empty here so if you head to um the link that i've put in the description below if you are choosing to code with me today um go to that link and then you'll be on the console and you'll be in this method here and here you have an option to get token and by the way if you're unfamiliar with the spotify api getting started authorization you know all those different things i did make a video explaining all of that um so feel free to check that out i'll put a link to that video above but um if you click the link in the video description you'll end up here and here you see the section of token and if i click get token um required scopes for this endpoint so just make sure you tap that which is the scope needed to be able to get the current playing song for the user and then you click request token that will refresh and this field will now be populated with the token so if i copy that and then what i'll do is i'll paste that in the spotify access token variable up above and now we can give this program a run right so what i will do is i'll get that open and then i'll open spotify on the right hand side and as you can tell here i'm playing it on my iphone actually probably could play on here but yeah it will still work so in any case um i've got this uh song playing and um yeah as you can tell it's playing it's on 240 at the moment and then on the left hand side i've got the program so let's give this a run so if i type python main dot py and i tap enter and as you can tell it's uh printed out so here we've got the artist thomas ferguson two steps from hell we've got the id we've got the link and then we've got the name and maybe if i let's switch to another song so um i wish by stevie wonder python main dot py we execute it again here you can see it's updated so he does get the latest song that's playing and here in this case it just has one artist and the link to the song and the name and i can click that link and it will open that up in the web browser um it's a bit slow so far isn't it but in any case i can go back let's go back and let's uh let's give this one more run and then as you can tell here again it will keep updating with the latest song that's playing so that's quite handy now i said i might show you how to put this all in the while loop so let's actually do that so what i'll do is i'll put this in a while loop so we'll do wow true get the current track we print it out and so we'll put that all in the while loop and then let let's get this to run every two seconds so you know let's not go too crazy but i think running every two seconds uh that'll be quite nice and then what i'll do um again we're using the time library for that there's a function called sleep that takes a number of seconds that you essentially want to pause and not do anything um so again two seconds is fine and then i'll need to import the time library so we'll do that here and let's head back to the terminal and again we'll have spotify open on this side so as you can tell we're playing this song here let me switch it to another song and so what we do now if we run the program tap enter as you can tell it's going to execute this every two seconds and so right now on the left hand side you've got you know the song that's currently playing and as i switch hopefully this will get the latest song and as you can tell it's updated right and let me switch to another one and you can see it's getting the latest song so yeah it works that's pretty cool right right that's the end of the video and i'm sure you learned an incredible amount of how to reach out to the spotify api get the current playing track and have it update if the user changes what they're listening to if you did enjoy the video and you're learning python then do consider subscribing because i'm going to continue to make more fun python project tutorials like this until then have a lovely weekend and i'll see you next time peace
Info
Channel: Imdad Codes
Views: 4,640
Rating: 4.9470201 out of 5
Keywords: get currently playing track spotify api, get currently playing track sptfy api, get current track spotify api, get current song spotify api, current song spotify api, currently playing spotify api, getting started with spotify api, getting started with sptfy api, spotify api, spotify api authorization, spotify api react, how to use spotify api, spotify api tutorial python, python spotify api tutorial, spotify api python example, spotify api example, spotify api oauth, python
Id: yKz38ThJWqE
Channel Id: undefined
Length: 17min 56sec (1076 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.