Imagine that your favorite singer releases a new song on YouTube, you listen to the song and click a “like” or leave your reviews, or you go to your favorite channel to see if there are some new releases. In this blog, I’ll talk about how to get YouTube videos’ / channels’ information with YouTube Data API with Python:
- What is YouTube Data API?
- What could we get by YouTube Data API?
- How to accomplish the request by Python?
What is YouTube Data API?
The YouTube Data API is an API that provides access to YouTube data, such as videos, playlists, and channels. It lets you incorporate functions normally executed on the YouTube website into your website or application.
What could we get by YouTube Data API?
In this section, I’ll take the video of the last blog (Jay Chou Mojito) as an example and talk about how to get its ID, its statistics data, its comments, etc.
Get a video ID & channel ID with a query
With the given query (“Jay Chou Mojito”), we can search the video with search
.
A search result contains information about a YouTube video, channel, or playlist
that matches the search parameters specified in an API request. While a search
result points to a uniquely identifiable resource, like a video, it does not
have its persistent data. The API supports the list
method for search, it
returns a collection of search results that match the query parameters specified
in the API request. By default, a search result set identifies matching video,
channel, and playlist resources, but you can also configure queries to only
retrieve a specific type of resource.
HTTP request:
You’ll find all parameters here.
Example
Search the video with “Jay Chou Mojito” query:
where
- the
part
(string) parameter specifies a comma-separated list of one or more search resource properties that the API response will include. Set the parameter value tosnippet
. - the
q
(string) parameter specifies the query term to search for.
then we can get the result in “json” format:
…
We got 103131 results in total, what we need is only the official video which is
released by “周杰倫 Jay Chou”, so we take the videoId
(-biOGdYiF-I) and
channelId
(UC8CU5nVhCQIdAGrFFp4loOQ) of the first result.
Get a video’s statistics data with a video ID
With the known videoId
, we can check its statistics data with videos
.
The list
method returns a list of videos that match the API request
parameters.
HTTP request:
You’ll find all parameters here.
Example
Retrieve information about the video with videoId
.
where
- the
part
parameter specifies a comma-separated list of one or more video resource properties that the API response will include. - the
id
parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) that are being retrieved. In a video resource, the id property specifies the video’s ID.
then we can get the result in “json” format:
There are 16295328 views since the release, 175632 likes, 7805 dislikes and 16676 comments.
Get a video’s comments
With the known videoId
, we can check its comments with commentThread
. A
commentThread
resource contains information about a YouTube comment thread,
which comprises a top-level comment and replies, if any exist, to that comment.
A commentThread
resource can represent comments about either a video or a
channel.
Both the top-level comment and the replies are actually comment
resources
nested inside the commentThread
resource. The commentThread
resource does
not necessarily contain all replies to a comment, and you need to use the
comments.list
method if you want to retrieve all replies for a particular
comment. Also note that some comments do not have replies.
The API supports the list
method for commentThreads resources.
HTTP request:
You’ll find all parameters here.
Example
Retrieve all comment threads associated with a particular video.
where
- the
part
parameter specifies a comma-separated list of one or morecommentThread
resource properties that the API response will include. - the
videoId
parameter instructs the API to return comment threads associated with the specified video ID.
then we can get the result in “json” format:
…
Get a channel’s information with channel ID
With the known channelId
, we can check its statistics data with channel
.
A channel
resource contains information about a YouTube channel. We could use
list
method to returns a collection of zero or more channel resources that
match the request criteria.
HTTP request:
You’ll find all parameters here.
Example
Retrieve channel data for the GoogleDevelopers YouTube channel. It uses the id request parameter to identify the channel by its YouTube channel ID.
where
- the
part
parameter specifies a comma-separated list of one or more channel resource properties that the API response will include. - the
id
parameter specifies a comma-separated list of the YouTube channel ID(s) for the resource(s) that are being retrieved. In a channel resource, the id property specifies the channel’s YouTube channel ID.
then we can get the result in “json” format:
According to the results, we get not only the title and the description of the channel, but also its statistics data like views count and subscriber count.
Get a channel’s playlist
A playlist
resource represents a YouTube playlist. A playlist is a collection
of videos that can be viewed sequentially and shared with other users. By
default, playlists are publicly visible to other users, but playlists can be
public or private.
The API supports the list
method for playlists resources, which returns a
collection of playlists that match the API request parameters. For example, you
can retrieve all playlists that the authenticated user owns, or you can retrieve
one or more playlists by their unique IDs.
HTTP request:
You’ll find all parameters here.
Example
Retrieve playlists owned by the YouTube channel that the request’s channelId
parameter identifies.
where
- the
part
parameter specifies a comma-separated list of one or moreplaylist
resource properties that the API response will include. - the
channelId
’s value indicates that the API should only return the specified channel’s playlists.
…
According to the results, we get information like author, title, description, tags, and timeCreated.
How to accomplish the request by Python?
Google creates a Python SDK for Python users, we will apply it in this section.
Prerequisites
- Python 2.7 or Python 3.5+
- The pip package management tool
- The Google APIs Client Library for Python
pip install --upgrade google-api-python-client
- The google-auth-oauthlib and google-auth-httplib2 libraries for user authorization
pip install --upgrade google-auth-oauthlib google-auth-httplib2
Get a video ID & channel ID with a query
google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file()
creates a :class:Flow
instance from a Google client secrets file.googleapiclient.discovery.build()
construct a Resource object for interacting with an API. TheserviceName
andversion
are the names of the Discovery service.youtube.search().list()
calls the search.list method to retrieve results matching the specified query term.
Get a video’s statistics data with a video ID
google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file()
creates a :class:Flow
instance from a Google client secrets file.googleapiclient.discovery.build()
construct a Resource object for interacting with an API. TheserviceName
andversion
are the names of the Discovery service.youtube.videos().list()
calls the API’s videos.list method to retrieve the video resource.
Get a video’s comments
googleapiclient.discovery.build()
construct a Resource object for interacting with an API. TheserviceName
andversion
are the names of the Discovery service.youtube.commentThreads().list()
calls the API’scommentThreads.list
method to list the existing comments.
Get a channel’s information with channel ID
google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file()
creates a :class:Flow
instance from a Google client secrets file.googleapiclient.discovery.build()
construct a Resource object for interacting with an API. TheserviceName
andversion
are the names of the Discovery service.youtube.channels().list()
calls the API’schannels.list
method to retrieve channel data for the GoogleDevelopers YouTube channel.
Get a channel’s playlist
google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file()
creates a :class:Flow
instance from a Google client secrets file.googleapiclient.discovery.build()
construct a Resource object for interacting with an API. TheserviceName
andversion
are the names of the Discovery service.youtube.playlists().list()
calls the API’splaylists.list
method to retrieve playlists owned by the YouTube channel that the request’schannelId
parameter identifies.
Reference
- “YouTube Data API”, developers.google.com. [Online]. Available: https://developers.google.com/youtube/v3/docs
- “Code Samples / Running code samples locally / Python”, developers.google.com. [Online]. Available: https://developers.google.com/explorer-help/guides/code_samples#python
- “youtube / api-samples”, github.com. [Online]. Available: https://github.com/youtube/api-samples/tree/master/python
- geralt, “Hand touch YouTube icon”, pixabay.com. [Online]. Available: https://pixabay.com/illustrations/hand-touch-you-tube-you-tube-icon-589488/
- slightly_different, “youtube logo graphic red”, pixabay.com. [Online]. Available: https://pixabay.com/vectors/youtube-logo-graphic-red-1837872/