Intro.mp4
A Python web application has been created using Flask that allows users to search for YouTube videos based on a given topic and retrieves relevant video data such as video Id, video title, channel name, view count, likes count, duration and the published date. The YouTube API gives us access to YouTube’s data in a more comprehensive, scalable way than standalone YouTube embed codes would. The data is then saved to a CSV file for further analysis and can be downloaded by the user.
Source: Visualmodo
To interact with the YouTube API from the Flask application, we need the google-api-python-client library.
pip install google-api-python-client
Firstly sign in here Google Cloud Platform with your google account. Click on select a project and then click New Project.
You will be then redirected to new project page. Name your project and click create.
Now you need to enable your API in the Dashboard window and click on explore and enable APIs.
Next, click on ENABLE APIS AND SERVICES. And then choose Youtube Data API V3 from API library
After that, the informations of Youtube Data API V3 will be visible to you. You need click on Enable option there.
Then you will be redirected to API overview window and you have to click on create credentials option. After clicking on it, you can see credential type
where you you have to select credential type as Youtube Data API V3 and check the Public Data box and then click Next to finish. The API Key will finally be visible to you.
The application has three routes:
The first route is the home page, which displays the index.html template(with main.css file). The second route is the search results page, which is the output page, displays the result.html template(with style.css file). The third route is the download file page, which helps to download the relevant informations in a CSV format.
When the user enters a search query on the home page and submits the form, the search results page is loaded with the search results based on the user's query.
The code first stores the API Informations of Youtube API V3(such as service name, version and API Key).The build function from google api client Construct a Resource for interacting with an API. Then we have created an instance of the Flask class and sets the template and static folders. Then, it defines the three routes using the @app.route decorator.
In the code, I have used while loop. Without the while loop, the code would just make a single request to get the first page of results and return whatever videos are returned in that one request. We have nextpagetoken to get unique results or a new set of results everytime the API make requests.
The code then retrieves the video statistics and content details for each video in the search results and stores the relevant information (video ID, title, channel name, views, likes, duration, and published datetime) in a dictionary called 'info'. It keeps making API requests using nextPageToken to get the next page of results until it has collected at least 50 videos.If we don't use the nextPageToken to iterate through the pages and the API returns fewer than 50 results, the code will not enter the while loop and will continue to execute the remaining code. Though the API has no expiration but it has a limited quota usage. The quota allocation for everyone is of 10,000 units per day. The consumption of this units is based on the methods you execute and the result size does not matter(the parameter maxresults is basically the maximum number of items per query, which is 50 and by default 5).
Finally, the code renders the result.html template, passing in the 'info' dictionary as a parameter to be used for displaying the search results.