
YouTube is a video-sharing website where users can view and upload videos. For both web and app development, sometimes we need to access information from YouTube videos.
You may also like: Build a Spring Boot API Using Spring Data JPA and Hibernate
In this case, we have to use the API's provided by Google developers. The main goal of this article is to introduce how we can use YoutTube APIs with Java. Here, we will use Spring Boot with Hibernate JPA and libraries from Google to implement the example.
Pre-Conditions
To access the YouTube Data API, we have to follow some basic instructions, like create a Google account, a project, credentials, etc. Maybe, it would be better to follow these instructions listed here . If we follow the steps from this link, we first need the ClientId, API key, client secret, etc.
Next, we can start our implementation. We need to put our credentials into the application.properties file. To run the GitHub example (link is given below), we also need to set up Java, MySQL, and Gradle.
API Docs
In this article, we are mainly concerned with video information. A YouTube video property mainly contains a channel, duration, description, and statistics (likes, dislikes, etc.). These properties are available in different resources of the YouYube API, such as video, channel, activity, etc. These resources can also perform list, insert, update, and delete operations.
The last part of this tutorial will be to customize the YouYube API so that we can define what we want. Just like the statistics part only contains the number of likes, dislikes, pageviews, etc., the
ContentDetails
part only contains video definitions, captions, country restrictions, etc. — and all of these components contain nested objects. Combined, we can get all the properties of a video, but here, we will be focusing on important parts only.Implementation
With that said, we will be implementing the YouTube Data API in a bit of different way. We will use a REST API to save extracted data to the database. For database entities, we will create a corresponding JPA entity. We have divided most of the video properties depending on a different part. Entities are:
Additionally, we will have a BaseEntity for common properties, and another entity — CrawlingInfo — to keep track of crawled data.
We will collect the following data for the channel information:
For video statistics, we will collect the following data:
And for common video properties, we will collect the following data:
Now, we will fetch video information through the YouTube Data API. So, we need some libraries provided by the Google team.
Other libraries are also used for Spring Boot and database support. We can find these libraries at gradle file .
At first, we have to initialize YouTube (v3) properties which supports core YouTube features such as uploading videos, creating and managing playlists, searching for content, and much more. But here, we will first implement some basic properties.
When I was creating my credentials, I had to create an application name
YouTubeVideoInfo
that is assigned as application name. The property youtube.apikey is located in the application.properties file. We have to replace youtube.apikey's value with our ones.
Now, we will fetch some information using this. Initially, we use
id, snippet
to fetch some basic data.
Here,
queryTerm
will be our search key, and video
will be the data type so that it will return video type data information. In a youtube API list request, maximum 50 videos information returns as response.
Here, the
search.execute()
method will return the expected result. So, we have to parse this response. And another thing: CrawlingInfo
class will be used to maintain the next and previous page. The YouTube API returns a maximum of 50 pieces of data in a request with the next page token. If we want to access next page we have to add nextPageToken
at pageToken
properties on request, then it will move to the next page. That's why we are keeping this track using crawlingInfo
entity.
We will use more queries on the remaining code to extract channel information, content details, statistics, etc. for every video and then save to the database.
If we take a look on the previous code snippet, we can see that, for
statistics
and contentDetails
, we are using videos resources and list operation. And for channel information, we are using channel resources and list operation. Here, I have represented all list operations of the YouTube Data API. And with similar approach, we can use other operations smoothly.
The code examples from this article are available here.
0 Comments