YouTubeで、特定名称の再生リストにある動画の一覧を表示したく、リファレンス ガイド: Data API プロトコル – YouTube の API とツール – Google Codeを読んで実装を試みた。
再生リストに関するやりたいこと
- Read:動画一覧の作成
- Update:再生リストにある動画に対するコメント入力
- Create:特定名称の再生リストがなければ作成
- Delete:特定名称の再生リストを削除
“Read”だけは実装したいなと。用途は、ピアノの練習動画を定期的にアップロードした再生リストを一覧表示し、そのまま再生(確認)をし、感想・気づいたことをコメントするというもの。(モチベーション維持のため)
メモ
- ユーザーの再生リストフィードの取得
- http://gdata.youtube.com/feeds/api/users/”username”/playlists
- ユーザーが作った再生リスト群の情報をゲット
- 再生リストのtitleチェック
- 再生リストフィードはの形
- 動画には、HTTP POSTでコメント追加できる
- YouTubeのPHPライブラリがある
- HTTP POSTを送るのは、fsockopen関数や、PEAR::Http_Requestとかもある
確認ページ:keep_motivation_by_moviepost.php
<form action="" type="get">
<input type="text" name="youtube_username" size="35" maxlength="255" class="help" value="" title="YouTube username" />
<input type="submit" value="search" />
</form>
<?php
function get_user_playlists($youtube_username) {
$url = 'http://gdata.youtube.com/feeds/api/users/'.
$youtube_username.
'/playlists';
$str = file_get_contents($url);
$ret = simplexml_load_string($str);
return $ret;
}
function get_playlist_url($xml, $playlist_name) {
$ret = '';
foreach ($xml->entry as $entry) {
// var_dump($entry);
if (strcmp($entry->title, $playlist_name) == 0) {
$gd = $entry->children('http://schemas.google.com/g/2005');
$attrs = $gd->feedLink->attributes();
$ret = $attrs['href'];
}
}
return $ret;
}
function make_movie_list($playlist_url) {
$contents = file_get_contents($playlist_url);
$xml = simplexml_load_string($contents);
$html = '<table>';
foreach ($xml->entry as $entry) {
$media = $entry->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->content->attributes();
$link = $attrs['url'];
$html_row = '<tr><td rowspan="3" width="120" height="90" align="center">'
. '<object style="height: 90px; width: 120px"><param name="movie" value="' . $link . '">'
. '<embed src="' . $link . '" type="application/x-shockwave-flash" width="120" height="90"></object></td>';
$title = $media->group->title;
$html_row .= '<td>' . $title . '</td></tr>';
$published = $entry->published;
$html_row .= '<tr><td>' . $published . '</td></tr>';
$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
$counter = $attrs['viewCount'];
$html_row .= '<tr><td>' . $counter . '</td></tr>';
$html .= $html_row;
}
$html .= '</table>';
return $html;
}
if ($_REQUEST["youtube_username"] !== 'YouTube username') {
$youtube_username = htmlspecialchars($_REQUEST["youtube_username"], ENT_QUOTES, 'UTF-8');
$xml = get_user_playlists($youtube_username);
$playlist_url = get_playlist_url($xml, 'keep_motivation_by_moviepost');
$html = make_movie_list($playlist_url);
echo $html;
}
?>
関連エントリー