DataAPIを使えるように登録する。
- 色々な人が解説しているけど、使い方は公式が一番わかりやすい。

YouTube Data API の概要 | Google Developers

DataAPIの仕様を理解する
- 例えば、YoutubeのChannel情報だと、下記のものが取得できる。
{ "kind": "youtube#channel", "etag": etag, "id": string, "snippet": { "title": string, "description": string, "publishedAt": datetime, "thumbnails": { (key): { "url": string, "width": unsigned integer, "height": unsigned integer } } }, "contentDetails": { "relatedPlaylists": { "likes": string, "favorites": string, "uploads": string, "watchHistory": string, "watchLater": string }, "googlePlusUserId": string }, "statistics": { "viewCount": unsigned long, "commentCount": unsigned long, "subscriberCount": unsigned long, "hiddenSubscriberCount": boolean, "videoCount": unsigned long }, "topicDetails": { "topicIds": [ string ] }, "status": { "privacyStatus": string, "isLinked": boolean }, "brandingSettings": { "channel": { "title": string, "description": string, "keywords": string, "defaultTab": string, "trackingAnalyticsAccountId": string, "moderateComments": boolean, "showRelatedChannels": boolean, "showBrowseView": boolean, "featuredChannelsTitle": string, "featuredChannelsUrls": [ string ], "unsubscribedTrailer": string, "profileColor": string }, "watch": { "textColor": string, "backgroundColor": string, "featuredPlaylistId": string }, "image": { "bannerImageUrl": string, "bannerMobileImageUrl": string, "backgroundImageUrl": { "default": string, "localized": [ { "value": string, "language": string } ] }, "largeBrandedBannerImageImapScript": { "default": string, "localized": [ { "value": string, "language": string } ] }, "largeBrandedBannerImageUrl": { "default": string, "localized": [ { "value": string, "language": string } ] }, "smallBrandedBannerImageImapScript": { "default": string, "localized": [ { "value": string, "language": string } ] }, "smallBrandedBannerImageUrl": { "default": string, "localized": [ { "value": string, "language": string } ] }, "watchIconImageUrl": string, "trackingImageUrl": string, "bannerTabletLowImageUrl": string, "bannerTabletImageUrl": string, "bannerTabletHdImageUrl": string, "bannerTabletExtraHdImageUrl": string, "bannerMobileLowImageUrl": string, "bannerMobileMediumHdImageUrl": string, "bannerMobileHdImageUrl": string, "bannerMobileExtraHdImageUrl": string, "bannerTvImageUrl": string, "bannerExternalUrl": string }, "hints": [ { "property": string, "value": string } ] }, "invideoPromotion": { "defaultTiming": { "type": string, "offsetMs": unsigned long, "durationMs": unsigned long }, "position": { "type": string, "cornerPosition": string }, "items": [ { "id": { "type": string, "videoId": string, "websiteUrl": string }, "timing": { "type": string, "offsetMs": unsigned long, "durationMs": unsigned long }, "customMessage": string } ] } }
Check
- 多すぎて混乱してしまいそうですが、チャンネル名と登録者数を取得するだけなら下記を取得
"snippet"
のtitle
がチャンネル名、"statistics"
のsubscriberCount
が登録者数です
{ "kind": "youtube#channel", "etag": etag, "id": string, "snippet": { "title": string, "description": string, "publishedAt": datetime, "thumbnails": { (key): { "url": string, "width": unsigned integer, "height": unsigned integer } } }, "statistics": { "viewCount": unsigned long, "commentCount": unsigned long, "subscriberCount": unsigned long, "hiddenSubscriberCount": boolean, "videoCount": unsigned long }
ソースコード
Youtube DataAPIからチャンネル名を取得する場合
#必要なモジュール import requests import json #チャンネル情報を取得したい場合は下記のURLを指定 URL='https://www.googleapis.com/youtube/v3/channels' # 取得したAPIKEYを入力してください。 API_KEY = '***' # 調べたいチャンネルのID(私のチャンネルIDなので適宜変更してください) CHANNEL_ID = 'UC5mn8FC8G7QW5eMqOkHN-Lg' # 入力するパラメータの設定 params = { 'key': API_KEY, 'part': 'snippet', 'id': CHANNEL_ID, } #データ取得 res = requests.get(URL,params=params) res.json()['items'][0]['snippet']['title']
- 下記のjsonデータが出力されます。
'snuow プログラムCh'
Youtube DataAPIから登録者数を取得する場合
#必要なモジュール import requests import json #チャンネル情報を取得したい場合は下記のURLを指定 URL='https://www.googleapis.com/youtube/v3/channels' # 取得したAPIKEYを入力してください。 API_KEY = '***' # 調べたいチャンネルのID(私のチャンネルIDなので適宜変更してください) CHANNEL_ID = 'UC5mn8FC8G7QW5eMqOkHN-Lg' # 入力するパラメータの設定 params = { 'key': API_KEY, 'part': 'statistics', 'id': CHANNEL_ID, } #データ取得 res = requests.get(URL,params=params) res.json()
- 下記のjsonデータが出力されます。
- subscriberCountが登録者数です。(18人という登録者数は正常です。)
{'kind': 'youtube#channelListResponse', 'etag': 'bdlGSLcoaiIg4rdryIJ-6vyspqI', 'pageInfo': {'totalResults': 1, 'resultsPerPage': 5}, 'items': [{'kind': 'youtube#channel', 'etag': 'vbdBkh925lOqvoQGmGHtIMfB7XM', 'id': 'UC5mn8FC8G7QW5eMqOkHN-Lg', 'statistics': {'viewCount': '576', 'subscriberCount': '18', 'hiddenSubscriberCount': False, 'videoCount': '20'}}]}
コメント