YOUTUBE API WITH MICROPYTHON AND ESP32
Hello to all the developers and programmers, I present to you a small project which is quite easy to do but above all very funny and interesting to do, I will try to explain to you as best as possible, in advance I am sorry for the possible blunders that I would make in this post.
I will already explain the goal of this project to you: In this project we will only use the ESP32 as will be mentioned a little below, we will use the google API system to retrieve 3 very interesting data from any YouTube channel the total subscribers, the number of views and the quantity of videos.
We start with the most important, with the parts used in this project. In fact just with an ESP32 you can do everything we will do in this tutorial, but if you have the ESP8266 you just have to pay attention to the compatibility of the code.
In this tutorial I would use the ESP32
Let's not forget that of course the ESP32 will be flash with micropython because otherwise it will not be able to work :)
So the second thing that we need to make this project work is the library which contains the API to be able to retrieve the 3 data that we need, I put the library below:
In this library as explained in we will need only 3 files
Or only those which I have underlined in green the "config.json" the "micropython_youtube_api.py" and finally the "urequests.py"
When you are going to create the project in your favorite code editor (ehem, VScode) you will have to put these 3 files in the same folder.
Following that you will tell me that in the config.json we are asked for a GoogleAPIKEY but also the ID of a YT chain, do not worry, I will show you how to do it right away.
In first time we will have to create the KEY API is the most important thing to make this project work, we will go to this link:
https://console.developers.google.com
Following that you go to identifiers
And you click in create an identifier, and API key, you will have a long code which will be your API key, especially do not show it to anyone, even if it does not matter it is better to reserve this code will look something like this : AIzaSyBOsto ******** HGrMkydpyV **********
The little stars are numbers and letters. You have your key but this is not enough you have to connect it to the API of yt because it alone is strictly useless as said previouslyYou need to link it to this API:
https://console.developers.google.com/apis/api/youtube
It's pretty easy just a button and choose your API, no need to put images to guide you :)
Following that we need the ChannelID, there are methods and methods to make this one appear but the best is:
Go to any video:
And do not click on the channel but just right click and copy the address of the link, you will have
https://www.youtube.com/channel/UCSJ4gkVC6NrvII8umztf0Ow, something like that, you will just have to take the numbers and letters after the last parenthesis is UCSJ4gkVC6NrvII8umztf0Ow, and voila you have your Channel ID.
Let's get down to the code.First of all we must of course import the library or do
import micropython_youtube_api
After that :
with open('config.json') as f:
config = json.load(f)
with YoutubeAPI( config["channelid"], config["appkeyid"], config["query_interval_sec"] ) as data:
print ("Subs {}".format( data.subs ) )
print ("Views {}".format( data.views ) )
print ("Videos {}".format( data.videos ) )
Well that's too much code once I know, I'll explain the first part allows you to take the information that we will enter in the "config.json" and assign it to the variable "config" and finally the second part will display the quentute of subs, views and videos that has a channel.
We return to "config.json"
{
"ssid": "x",
"ssid_password": "x",
"appkeyid": "x",
"channelid": "x",
"query_interval_sec": 60
}
You will have to put the info in this config first the SSID of your WIFI and the code of your WIFI too, then we will enter the API we created and the channel id, and that's it, easy no ?
So we're almost done, but you can imagine without connecting to the internet all this is not useful so we do:
import network, json, time
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print ("Connecting to WiFi: {}".format( config['ssid'] ) )
wlan.connect( config['ssid'], config['ssid_password'])
# Wait until wifi is connected
while not wlan.isconnected:
pass
And of course, you have to add it to the code we did just before otherwise it won't workLet's move on to the final result:
You will have something that looks a lot like this give them that you see it is from the LofiGirl channel.
Now that we are done I wish you a pleasant day and if you have a problem do not hesitate to post it in the commentsSorry for the bad English, I'm French and you'll guess most of the text comes from google translate :)
Comments
Please log in or sign up to comment.