in this new series of tutorials, we will see some useful projects that we can make in python. for this first project we will see how to make a Youtube Subscribers counter using Python and the Youtube API from Google, so let's start it.
login to google console for developers
the first thing that we will need is to log to the google console for the developer, to be able to use the Youtube Data API. for that, go to this link: google console for developers
if you haven't signed up then you should sign up and login and this window should appear
for me, I have already created a project, but if it's your first time with the Google Console, create a new project like this:
after clicking on create a new window will appear
choose a name for the project than click on create.
after you have created your first project, press the library button
and then search for Youtube Data v3 API
Click on it and then enable the API
after you have enabled the Youtube Data API, return to the Console and then click on Credentials
and then on create credentials
then choose API key.
then, the key should appear and after that, you should click on Restrict Key
after selecting on that button, click on restrict key ( like the image below ) and choose the API
and then click on save
now that we have configured our API, all what we will need is to copy the key and add it to our python script. for that click on the copy icon like this image:
now let's start Coding our small app.
Python Code
open your favorite text editor or IDE and copy this code
import jsonimport urllib.requestkey = "Pase Your Key here"username = input("put your favorite youtuber: ")data = urllib.request.urlopen("https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername="+username+"&key="+key).read()subscribers = json.loads(data)["items"][0]["statistics"]["viewCount"]print(username," has ",subscribers," subscribers")
don't forget to paste your own key inside the variable " key ". now let's see together how our code works.
we will need to import JSON and urlib.request libraries, these libraries will allow us to connect to any URL and read their content.
as you can see we have used this URL inside the urlopen method. this URL is where we will get all the information about the user's youtube channel, and if you copy and paste it inside your browser this is what it will look like.
all this information we can take it by the JSON.loads() function.
now that you know how the codes work let's test it. And it works
this is the output of our small app after running it.
now that you know how the codes work let's test it. And it works
this is the output of our small app after running it.
I hope that this series of tutorials will be helpful if you have any idea for an app that you want us to make just tell us in the comment.
also if you haven't understood something, just tell us and we will answer you. Thank you
also if you haven't understood something, just tell us and we will answer you. Thank you
Make a Youtube Subscriber Counter Using Python
Reviewed by Medics
on
July 16, 2019
Rating:
hi sir i am try it but its not working i dont know why
ReplyDeletepls contact to me with this gmail bestarewa@gmail.com pls sir i need your help
ReplyDelete