Nice to meet you. My name is Shun and I am working hard as an engineer. I made a calendar that automatically updates the distribution schedule of Vtuber, so I would like to write the process. This time, we will use the YouTube Data API to get the video information of the YouTube channel.
--YouTube Channel ID
--Channel information --Video information
First of all, register your application with Google so that you can use the YouTube Data API. For details, see here. (By the way, you can also try the API from this page.) We will use the API key obtained here to acquire video information.
The acquisition flow looks like this.
You can get the channel ID from the URL when you opened the home of the channel you want to know. For example, in the case of my favorite Akua Minato, https://www.youtube.com/channel/UC1opHUrw8rvnsadT-iGp7Cg Since this is the URL of the YouTube channel, the channel ID will be ** UC1opHUrw8rvnsadT-iGp7Cg **.
Use this channel ID to get a list of videos.
import urllib.request
import json
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
def get_video_info(channel_id, page_token=None, published_after=None):
url = 'https://www.googleapis.com/youtube/v3/search'
params = {
'key': 'YOUTUBE_API_KEY',
'part': 'id',
'channelId': channel_id,
'maxResults': 50,
'order': 'date'
}
if page_token is not None:
params['pageToken'] = page_token
if published_after is not None:
params['publishedAfter'] = published_after
req = urllib.request.Request('{}?{}'.format(url, urllib.parse.urlencode(params)))
with urllib.request.urlopen(req, context=context) as res:
body = json.load(res)
return body
In addition to the channel ID, specify the following values as parameters.
Use the acquired video ID to acquire detailed information for each video.
def get_video_details(video_ids):
url = 'https://www.googleapis.com/youtube/v3/videos'
params = {
'key': 'YOUTUBE_API_KEY',
'part': 'snippet, liveStreamingDetails',
'id': video_ids
}
req = urllib.request.Request('{}?{}'.format(url, urllib.parse.urlencode(params)))
with urllib.request.urlopen(req, context=context) as res:
body = json.load(res)
return body
In addition to newly specifying the video ID as id in the parameter, snippet and liveStreamingDetails are specified in part. With this, in addition to the basic information of the video, you can also get the information at the time of live distribution.
def get_videos(items):
video_ids = ''
for item in items:
if 'videoId' in item['id']:
video_ids += item['id']['videoId']
video_ids += ', '
video_details = get_video_details(video_ids[:-2])
for video_detail in video_details['items']:
print(video_detail)
Since the playlist ID is also included in the list of video IDs acquired first, only those with videoId are collected before acquiring video information.
The code that executes these together is ↓.
video_info = get_video_info(channel_id='CHANNEL_ID', published_after='DATETIME')
get_videos(video_info['items'])
while 'nextPageToken' in video_info:
page_token = video_info['nextPageToken']
video_info = get_video_info(channel_id='CHANNEL_ID', page_token=page_token)
get_videos(video_info['items'])
First, specify the date and time to get it, and then continue to get it as long as there is nextPageToken. In the case of Minato Akua Channel, the channel was established on July 31, 2018, so if you start acquiring it at an older date and time, you will get information on all videos. I can do it.
Actually, I will continue to update the calendar after this, but that will be another opportunity ....
Recommended Posts