youtubeliveから情報を取得する
生配信中かどうかを判定するには、StackOverFlowのこの記事の内容を参考にする。
http://stackoverflow.com/questions/32454238/how-to-check-if-youtube-channel-is-streaming-live
GETで
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channelId}&type=video&eventType=live&key={APPKEY}
からjsonをダウンロードできる。liveBroadcastContentがliveであれば生配信中。videoIDを読み取り、配信URL等を取得できる。
<?php $search_api_base = "https://www.googleapis.com/youtube/v3/search?part=snippet"; $channelId = "channelId"; $key = "key"; $search_api = $search_api_base . "&channelId=" . $channelId . "&type=video&eventType=live" . "&key=" . $key ; $search_contents = file_get_contents($search_api); $search_json = json_decode($search_contents,true); $isLive = (string) $search_json["items"][0]["snippet"]["liveBroadcastContent"]; $videoId = (string) $search_json["items"][0]["id"]["videoId"]; if($isLive == "live"){ //視聴者数取得 $viewers_url = "https://www.youtube.com/live_stats?v=" . $videoId; $viewers = file_get_contents($viewers_url); $video_url_base = "https://www.googleapis.com/youtube/v3/videos?part=snippet"; $video_url = $video_url_base . "&id=" . $videoId . "&key=" . $key; $video_contents = file_get_contents($video_url); $video_json = json_decode($video_contents,true); //配信タイトル(トピック)取得 $title = $video_json["items"][0]["snippet"]["title"]; //配信開始時刻取得 $created_at = $video_json["items"][0]["snippet"]["publishedAt"]; //チャンネル名取得 $channel_name = $video_json["items"][0]["snippet"]["channelTitle"]; //スナップショット取得, サイズは default,medium,high,standard,maxresの5種類 //それぞれ 120x90, 320x180, 480x360, 640x480, 1280x720 $snapshot = $video_json["items"][0]["snippet"]["thumbnails"]["standard"]["url"]; $snapshot_width = $video_json["items"][0]["snippet"]["thumbnails"]["standard"]["width"]; $snapshot_height = $video_json["items"][0]["snippet"]["thumbnails"]["standard"]["height"]; //説明文取得 $description = $video_json["items"][0]["snippet"]["description"]; // var_dump($video_json); echo "isLive:" . $isLive . PHP_EOL ; echo "viewers:" . $viewers . PHP_EOL ; echo "title:" . $title . PHP_EOL ; echo "created_at:" . $created_at . PHP_EOL ; echo "id:" . $videoId . PHP_EOL ; echo "snapshot:" . $snapshot . PHP_EOL ; echo "channel_name:" . $channel_name . PHP_EOL ; echo "description:" . $description . PHP_EOL ; }
channelIdはUC~で始まる個々のチャンネルに与えられるIDで「UC**」。チャンネルのURLの後半部分。
https://www.youtube.com/channel/UC** ←ここ
マニュアルはここ↓
https://developers.google.com/youtube/v3/docs/search?hl=ja
本当はsearchではなく、channelでこんな感じにやってChannel IDを使って確認したいところだけど、どうもできないようだ。
https://www.googleapis.com/youtube/v3/channels?id={channelId}&key={APPKEY}&part=id,snippet,brandingSettings,contentDetails,invideoPromotion,statistics,topicDetails
追記
searchでは以下の問題あり・maxResultsが最大50。少なすぎる。
・channelIDを複数指定することが不可。
http://stackoverflow.com/questions/21667899/how-to-search-content-across-multiple-channel-in-youtube-api
つまりchannelIDを一つずつ指定して、一回一回APIを叩くか、channelIDを指定せずにeventType=liveのものを50ずつ取得するか(次のページに進むにはnextPageTokenを使う必要がある)。
http://qiita.com/yuji_saito/items/8f472dcd785c1fadf666
いずれにしてもAPIをたくさん叩くということ。仮にregionCode=JPなんてしても、かなり多くのライブが引っかかってしまう、さすがyoutube。
channelsはそもそもlive情報を取得することができない。
なのでいまのところsearchで複数回リクエストするしかなさそうだ。