youLike.py - A script to like Youtube videos

It's been a long time since my last post here, the reason being lack of time in my schedule. But, recently due to COVID I got some free time and thought of restarting the blog by posting at least one blog per day. These blogs are going to be more like a diary entry of what I have learned/built in a day. I will try to be as concise as possible.
So, first in the series is "youLike.py". It is a script I started working on the day before yesterday which changes the rating(👍 / 👎) of 20 latest videos of a channel as specified in the code by using the channel id of the channel whose video's ratings you want to change.
This idea came to my mind when my mother insisted me to like my sister's newly created youtube channel. As I am a lazy guy🥱 and not interested to open each video and hit the like button, so, I thought of writing a script that would do the same for me.
So, first of all, I started with reading the documentation of the python-youtube module but wasn't able to come up with a format to create a script. Anyways, Youtube Data API was needed in all cases, so, I started reading it's documentation and found that they have whole scripts written in multiple languages for performing different operations using the API, which anyone can find on GitHub. So, I searched through the Python scripts and found a script named, like_video.py, which was performing the same operation that I needed so, I downloaded and ran the script. The script had a default videoId whose rating it changes to 'like'. But, not by simply running the script as there was a field called CLIENT_SECRET which needed some kind of client_secret.json file. After reading the comments I understood that I needed to create Auth credentials and insert them is .json format. SO, I went straight to the Google Console and created a new project named, you guessed it, "youLike". And created my new OAuth2.0 client and downloaded the .json file, moved it to the project directory and BOOOOM!!!💣 
I got another error, URI mismatch error
and at that time I was getting really sleepy😴(it was 1 a.m.) so, I closed my laptop and went to sleep🛌. The next day, I woke up early🌇 and started finding the solution for URI mismatch error. And with the help of some googling and searching on StackOverflow I was able to figure out that I need to have the same redirect URIs in both my Auth credentials and the script, so, I updated the redirect URI in Google console and BAMMMM!!!✨ the script was running.
But what it was doing was, it was asking me to pass videoId every time I ran the script, and also, the operation was performed only once. But as I told I am very lazy🥱, so why would I do the same thing in CLI which I was trying to avoid on GUI??
So, the easiest way to resolve this issue that came to my mind was to loop the whole process a  no. of times without explicitly typing the videoId. So, for that, I needed a list with all the videoId stored in it. And for that, I needed to scrape the channel data. And that's where the Youtube Data API came for my rescue and I created an API key {your_api_key} and added channel Id and API key to the URL 


'https://www.googleapis.com/youtube/v3/searchkey={tour_api_key}&channelId={channelId}&part=snippet,id&order=date&maxResults=20' 

and here it was - whole data of the channel. The only limitation was I was able to GET max 50 videoId(latest) but as my sister's channel is newly created it was not an issue. Although there's a solution for this issue i.e., 'pageToken'. I didn't need it so, I didn't study details about it and moved on to refining the scraped data.
Then, once the data was refined and stored in a list I passed the list as an argument to the arg parser, and the script started running. But, again got stuck due to limitation of youtube API which allows only one API call per Auth Token and I was stuck in this issue for a long time🕑 and then, after lots of Googling and head-scratching, I found 'pickle' module which gave me a little hope. But, the day had passed and I was again feeling sleepy😴 and unable to understand the documentation😕. So, you guessed it, I went to sleep🛌.
The next day I was determined to finish off the script ASAP, so, I posted my doubts in a Python community called PyJaipur for a quick response and started reading some quick guides about the pickle module. Soon, I came to understand the working of the module but was unable to identify why I am getting this error



Traceback (most recent call last):
File "youLike.py", line 98, in 
youtube = get_authenticated_service()
File "youLike.py", line 63, in get_authenticated_service
credentials = pickle.load(token)
EOFError: Ran out of input 

Yeah, you guessed it right💯, it was due to empty token.pickle file. But at that moment it didn't come to my mind till I explained my whole approach and the error I was getting to the admins of PyJaipur community. And, after explaining I soon realized my mistake and resolved the issue. This reminded me of rubber duck debugging. After reviewing my code the admins of PyJaipur community asked



CLI args are only computed once. I don't see why you've put them inside a loop.

And it came to my mind that I can remove the arg parser altogether as I am using the videoId list only as input so, I modified the code accordingly and was done with the script😇.
Now, I had 8 hrs. to complete my college assignment😒 so, I procrastinated😪 over it a bit and finally started writing it and was done with it just 15min. before the deadline.

Comments

Popular posts from this blog

Data Science 

Learning Path for Deep Learning in 2019

Day 12, 13 and 14 - Daily Dev Diary