通过 API 获取 YouTube 用户的完整播放列表列表

发布于 2024-11-29 22:49:38 字数 1429 浏览 0 评论 0原文

所以,这是我获取 YouTube 用户的公共播放列表的代码:

function getyoutubeplaylists($userName) {
$yt = connectyoutube();
$yt->setMajorProtocolVersion(2);
$playlistListFeed = $yt->getPlaylistListFeed($userName);
foreach ($playlistListFeed as $playlistListEntry) {
    $playlist['title'] = $playlistListEntry->title->text;
    $playlist['id'] = $playlistListEntry->getPlaylistID();
    $playlists[] = $playlist;
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
    foreach ($playlistVideoFeed as $videoEntry) {
        $playlist_assignment['youtube_id'] = substr($videoEntry->getVideoWatchPageUrl(),31,11);
        $playlist_assignment['id'] = $playlist['id'];
        $playlist_assignments[] = $playlist_assignment;
    }
}
$everything['playlists'] = $playlists;
$everything['playlist_assignments'] = $playlist_assignments;
return $everything;
}

问题是,这只获取第一页或结果。关于如何使用 Zend Gdata 检索下一页结果有什么想法吗?

原始 gdata XML 显示所需的 URL:

<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=1&amp;max-results=25"/>
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=26&amp;max-results=25"/>

但是,getPlaylistListFeed 似乎没有任何参数来指定“start-index”或“max-results”。

So, here's my code for getting a youtube user's public playlists:

function getyoutubeplaylists($userName) {
$yt = connectyoutube();
$yt->setMajorProtocolVersion(2);
$playlistListFeed = $yt->getPlaylistListFeed($userName);
foreach ($playlistListFeed as $playlistListEntry) {
    $playlist['title'] = $playlistListEntry->title->text;
    $playlist['id'] = $playlistListEntry->getPlaylistID();
    $playlists[] = $playlist;
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
    foreach ($playlistVideoFeed as $videoEntry) {
        $playlist_assignment['youtube_id'] = substr($videoEntry->getVideoWatchPageUrl(),31,11);
        $playlist_assignment['id'] = $playlist['id'];
        $playlist_assignments[] = $playlist_assignment;
    }
}
$everything['playlists'] = $playlists;
$everything['playlist_assignments'] = $playlist_assignments;
return $everything;
}

Problem is, this only gets the first page or results. Any ideas on how to use the Zend Gdata to retrieve the next page of results?

The raw gdata XML shows the URLs needed:

<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=1&max-results=25"/>
<link rel="next" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/pennstate/playlists?start-index=26&max-results=25"/>

However, getPlaylistListFeed doesn't seem to have any parameters to specify "start-index" or "max-results".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪 2024-12-06 22:49:39

由于我浪费了一整天“只是想在我的网站上显示我的视频列表”,我想我应该粘贴一些示例代码,这些代码教会了我很多东西。正如预期的那样,Zend_Gdata_YouTube:: 已经在 Magento 安装中。下面的代码将呈现一个统一的“视频”列表,其中播放列表在列表中与其他视频并排显示相同。 更新:我根据我的答案制作了一个 Magento 扩展

只是一个通用的 php,我放入 var/export 中。在顶部,您必须包含 app/Mage.php

<?
$userName='cacycleworksdotcom';
$yt = new Zend_Gdata_YouTube();
//////////////////////////////////////////////////////////////////////
// Get playlists.
$playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName));
$playlist=Array();
$videoEntry=NULL;
$playlistvideos=Array();

在这里,您在 $playlistListFeed 中有一个 Zend 对象数组。

foreach ($playlistListFeed as $idx=>$playlistListEntry) {
    // process each playlist
    $playlists[$idx]['title'] = $playlistListEntry->title->text;
    $url=$playlistListEntry->getSelfLink()->href;
    $id=explode("/PL",$url);
    $playlists[$idx]['id'] = $id[1];
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
    $playlists[$idx]['time']=0;
    $playlists[$idx]['views']=0;
    $playlists[$idx]['rating']=0;

现在,在播放列表中,我查看该播放列表的视频并收集每个视频的统计数据。我将他们的时间相加以获得总播放时间,然后捕获播放列表中视频的最高观看次数和评分。

    foreach ($playlistVideoFeed as $videoEntry) {
        // info of each video inside this playlist
        $_id=substr($videoEntry->getVideoWatchPageUrl(),31,11);
        $playlistvideos[]=$_id;
        $_url=$videoEntry->getVideoWatchPageUrl();
        $playlists[$idx]['videos'][$_id]=$_url;
        $playlists[$idx]['time']+=$videoEntry->getVideoDuration();
        $_views=$videoEntry->getVideoViewCount();
        if( $_views > $playlists[$idx]['views'] )
            $playlists[$idx]['views']=$_views;
        $_rating=$videoEntry->getRating()->average;
        if( $_rating > $playlists[$idx]['rating'] )
            $playlists[$idx]['rating']=$_rating;
    }

我最终使用 XML 来获取缩略图数据。

    $xml=$playlistListEntry->getXML();
    // $playlists[$idx]['xml']=$xml;    // store original XML for now
    $xml = simplexml_load_string($xml); // transfer into object
    $attrs=$xml->group->thumbnail[1];
    $playlists[$idx]['thumb']=(string)$attrs['url'];
    //                                1st vid id         playlist id
    // http://www.youtube.com/watch?v=mcnIAErKc-g&list=PLEDADE9CA0E65BA78
    $videoid=array_keys( $playlists[$idx]['videos']);
    $videoid=$videoid[0];
    $playlists[$idx]['url'] = "http://www.youtube.com/watch?v=$videoid&list=PL".$playlists[$idx]['id'];     
}

播放列表已处理完毕,现在让我们获取不在播放列表中的剩余视频:

//////////////////////////////////////////////////////////////////////
// Videos themselves
$idx=count($playlists);
$userFeed = $yt->getUserUploads($userName);
foreach ($userFeed as $videoEntry) {
    $idx++;
    $_id=substr($videoEntry->getVideoWatchPageUrl(),31,11);
    if( ! in_array($_id, $playlistvideos) ) {
        $_url=$videoEntry->getVideoWatchPageUrl();
        $playlists[$idx]['id']=$_id;
        $playlists[$idx]['url']=$_url;
        $playlists[$idx]['title']=$videoEntry->title->text;
        $playlists[$idx]['views']=$videoEntry->getVideoViewCount();
        $playlists[$idx]['rating']=$videoEntry->getRating()->average;
        $thumbs=$videoEntry->getVideoThumbnails();
        // these need resizing to width="320" height="180"
        $playlists[$idx]['thumb']=$thumbs[0]['url'];
        $playlists[$idx]['time']=$videoEntry->getVideoDuration();
    } // else { echo "$_id already in playlist\n";  }
}

这里我们有一个 YouTube 视频数组,顶部是按最旧的顺序排列的播放列表,后面是未出现在相同播放列表中的用户视频最旧的第一订单。所以我找到了这个简单的排序代码来改变顺序。这是一篇关于排序的好文章,如果您在这里尝试对多维数组进行排序,那么值得一读。

//////////////////////////////////////////////////////////////////////
// http://www.the-art-of-web.com/php/sortarray/
function orderBy($data, $field) {
    $code = "return strnatcmp(\$a['$field'], \$b['$field']);";
    //  swap $a and $b to make descending instead of ascending
    usort($data, create_function('$b,$a', $code)); //('$a,$b', $code));
    return $data;
}
$playlists = orderBy($playlists, 'views');
//////////////////////////////////////////////////////////////////////
echo "\n\n";
print_r($playlists);

下面是帮助我开始使用这些愚蠢的 GData YouTube Zend 对象的代码:

echo "\n\n";
show_methods($videoEntry);
echo "\n\n";
show_methods($playlistListFeed[0]);
echo "\n\n";
show_methods($playlistListFeed);

function show_methods( $_a ) {
    echo "<h3>Methods for ".get_class($_a)."</h3>";
    $_a= get_class_methods($_a);
    $_a=array_unique($_a);
    array_multisort(&$_a);
    $i=0;
    foreach( $_a as $method ) {
        $i++;
        printf("%-30.30s",$method);
        if($i%5==0)
            echo "\n";
    }
}

下面是两个用于显示结构的数组条目。请注意,播放列表有一个 videos 键,其中包含视频数组。测试 videos 键可以告诉您这是一个播放列表。 url 是您的用户可以点击打开 YouTube 网站上的视频或播放列表的地址。

[1] => Array
    (
        [title] => Ducatitech.com "HowTo" Adjust your Valves
        [id] => 970EC735D36A95E8
        [time] => 855
        [views] => 144847
        [rating] => 4.9322033
        [videos] => Array
            (
                [dIj3nSJGPZw] => http://www.youtube.com/watch?v=dIj3nSJGPZw&feature=youtube_gdata_player
                [3WQY1MRlmH4] => http://www.youtube.com/watch?v=3WQY1MRlmH4&feature=youtube_gdata_player
            )

        [thumb] => http://i.ytimg.com/vi/dIj3nSJGPZw/mqdefault.jpg
        [url] => http://www.youtube.com/watch?v=dIj3nSJGPZw&list=PL970EC735D36A95E8
            )

        [thumb] => http://i.ytimg.com/vi/mcnIAErKc-g/mqdefault.jpg
        [url] => http://www.youtube.com/watch?v=mcnIAErKc-g&list=PLEDADE9CA0E65BA78
    )
[7] => Array
    (
        [id] => 80yCiFkOB9g
        [url] => http://www.youtube.com/watch?v=80yCiFkOB9g&feature=youtube_gdata_player
        [title] => Ducatitech.com: ExactFit Timing Belt Tensile Test
        [views] => 7589
        [rating] => 4.25
        [thumb] => http://i.ytimg.com/vi/80yCiFkOB9g/0.jpg
        [time] => 625
    )

最后,你可以从 show_methods() 中得到这样的东西:

Methods for Zend_Gdata_YouTube_VideoEntry

__construct                   __get                         __isset                       __set                         __toString                    
__unset                       addVideoDeveloperTag          delete                        encode                        ensureMediaGroupIsNotNull     
flushNamespaceLookupCache     getAlternateLink              getAuthor                     getCategory                   getComments                   
getContent                    getContributor                getControl                    getDOM                        getEditLink                   
getEtag                       getExtensionAttributes        getExtensionElements          getFeedLink                   getFlashPlayerUrl             
getHttpClient                 getId                         getLicenseLink                getLink                       getLocation                   
getMajorProtocolVersion       getMediaGroup                 getMediaSource                getMinorProtocolVersion       getNextLink                   
getNoEmbed                    getPreviousLink               getPublished                  getRacy                       getRating                     
getRecorded                   getRights                     getSelfLink                   getService                    getSource                     
getStatistics                 getSummary                    getText                       getTitle                      getTitleValue                 
getUpdated                    getVideoCategory              getVideoCommentFeedUrl        getVideoComplaintsLink        getVideoDescription           
getVideoDeveloperTags         getVideoDuration              getVideoGeoLocation           getVideoId                    getVideoRatingInfo            
getVideoRatingsLink           getVideoRecorded              getVideoResponsesLink         getVideoState                 getVideoTags                  
getVideoThumbnails            getVideoTitle                 getVideoViewCount             getVideoWatchPageUrl          getWhere                      
getXML                        isVideoEmbeddable             isVideoPrivate                lookupNamespace               registerAllNamespaces         

Since I wasted all day "just trying to get a list of my videos to show on my website", I thought I'd paste up some sample code that just taught me a lot. And as expected, Zend_Gdata_YouTube:: is already in Magento installations. The code below will present a unified list of "videos", where playlists appear the same in the list as the other videos alongside each other. Update: I made a Magento extension out of my answer.

This is just a generic php I dropped into var/export. At the top, you have to include app/Mage.php.

<?
$userName='cacycleworksdotcom';
$yt = new Zend_Gdata_YouTube();
//////////////////////////////////////////////////////////////////////
// Get playlists.
$playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName));
$playlist=Array();
$videoEntry=NULL;
$playlistvideos=Array();

Here, you've got an array of Zend objects in $playlistListFeed.

foreach ($playlistListFeed as $idx=>$playlistListEntry) {
    // process each playlist
    $playlists[$idx]['title'] = $playlistListEntry->title->text;
    $url=$playlistListEntry->getSelfLink()->href;
    $id=explode("/PL",$url);
    $playlists[$idx]['id'] = $id[1];
    $playlistVideoFeed = $yt->getPlaylistVideoFeed($playlistListEntry->getPlaylistVideoFeedUrl());
    $playlists[$idx]['time']=0;
    $playlists[$idx]['views']=0;
    $playlists[$idx]['rating']=0;

Now, within the playlist, I look at the videos of this playlist and gather stats on each one. I sum their times to get a total playtime and then capture the highest view count and rating from the videos in the playlist.

    foreach ($playlistVideoFeed as $videoEntry) {
        // info of each video inside this playlist
        $_id=substr($videoEntry->getVideoWatchPageUrl(),31,11);
        $playlistvideos[]=$_id;
        $_url=$videoEntry->getVideoWatchPageUrl();
        $playlists[$idx]['videos'][$_id]=$_url;
        $playlists[$idx]['time']+=$videoEntry->getVideoDuration();
        $_views=$videoEntry->getVideoViewCount();
        if( $_views > $playlists[$idx]['views'] )
            $playlists[$idx]['views']=$_views;
        $_rating=$videoEntry->getRating()->average;
        if( $_rating > $playlists[$idx]['rating'] )
            $playlists[$idx]['rating']=$_rating;
    }

I ended up using the XML to get the thumbnail data.

    $xml=$playlistListEntry->getXML();
    // $playlists[$idx]['xml']=$xml;    // store original XML for now
    $xml = simplexml_load_string($xml); // transfer into object
    $attrs=$xml->group->thumbnail[1];
    $playlists[$idx]['thumb']=(string)$attrs['url'];
    //                                1st vid id         playlist id
    // http://www.youtube.com/watch?v=mcnIAErKc-g&list=PLEDADE9CA0E65BA78
    $videoid=array_keys( $playlists[$idx]['videos']);
    $videoid=$videoid[0];
    $playlists[$idx]['url'] = "http://www.youtube.com/watch?v=$videoid&list=PL".$playlists[$idx]['id'];     
}

The playlists are handled, now let's get the remaining videos that aren't in playlists:

//////////////////////////////////////////////////////////////////////
// Videos themselves
$idx=count($playlists);
$userFeed = $yt->getUserUploads($userName);
foreach ($userFeed as $videoEntry) {
    $idx++;
    $_id=substr($videoEntry->getVideoWatchPageUrl(),31,11);
    if( ! in_array($_id, $playlistvideos) ) {
        $_url=$videoEntry->getVideoWatchPageUrl();
        $playlists[$idx]['id']=$_id;
        $playlists[$idx]['url']=$_url;
        $playlists[$idx]['title']=$videoEntry->title->text;
        $playlists[$idx]['views']=$videoEntry->getVideoViewCount();
        $playlists[$idx]['rating']=$videoEntry->getRating()->average;
        $thumbs=$videoEntry->getVideoThumbnails();
        // these need resizing to width="320" height="180"
        $playlists[$idx]['thumb']=$thumbs[0]['url'];
        $playlists[$idx]['time']=$videoEntry->getVideoDuration();
    } // else { echo "$_id already in playlist\n";  }
}

Here we have an array of our youtube videos, at the top are the playlsits ordered by oldest first, followed by the user's videos not appearing in playlists in same oldest first order. So I found this simple sort code to change the order. Great article there about sorting and worth a read if you're here trying to sort multidimensional arrays.

//////////////////////////////////////////////////////////////////////
// http://www.the-art-of-web.com/php/sortarray/
function orderBy($data, $field) {
    $code = "return strnatcmp(\$a['$field'], \$b['$field']);";
    //  swap $a and $b to make descending instead of ascending
    usort($data, create_function('$b,$a', $code)); //('$a,$b', $code));
    return $data;
}
$playlists = orderBy($playlists, 'views');
//////////////////////////////////////////////////////////////////////
echo "\n\n";
print_r($playlists);

Here is the code that helped me get started working with these goofy GData YouTube Zend objects:

echo "\n\n";
show_methods($videoEntry);
echo "\n\n";
show_methods($playlistListFeed[0]);
echo "\n\n";
show_methods($playlistListFeed);

function show_methods( $_a ) {
    echo "<h3>Methods for ".get_class($_a)."</h3>";
    $_a= get_class_methods($_a);
    $_a=array_unique($_a);
    array_multisort(&$_a);
    $i=0;
    foreach( $_a as $method ) {
        $i++;
        printf("%-30.30s",$method);
        if($i%5==0)
            echo "\n";
    }
}

Here are two of the array entries to show the structure. Note that playlists have a videos key with an array of the videos inside it. Testing for the videos key can tell you it's a playlist. The url is what your user can click on to open the video or playlist on the youtube site.

[1] => Array
    (
        [title] => Ducatitech.com "HowTo" Adjust your Valves
        [id] => 970EC735D36A95E8
        [time] => 855
        [views] => 144847
        [rating] => 4.9322033
        [videos] => Array
            (
                [dIj3nSJGPZw] => http://www.youtube.com/watch?v=dIj3nSJGPZw&feature=youtube_gdata_player
                [3WQY1MRlmH4] => http://www.youtube.com/watch?v=3WQY1MRlmH4&feature=youtube_gdata_player
            )

        [thumb] => http://i.ytimg.com/vi/dIj3nSJGPZw/mqdefault.jpg
        [url] => http://www.youtube.com/watch?v=dIj3nSJGPZw&list=PL970EC735D36A95E8
            )

        [thumb] => http://i.ytimg.com/vi/mcnIAErKc-g/mqdefault.jpg
        [url] => http://www.youtube.com/watch?v=mcnIAErKc-g&list=PLEDADE9CA0E65BA78
    )
[7] => Array
    (
        [id] => 80yCiFkOB9g
        [url] => http://www.youtube.com/watch?v=80yCiFkOB9g&feature=youtube_gdata_player
        [title] => Ducatitech.com: ExactFit Timing Belt Tensile Test
        [views] => 7589
        [rating] => 4.25
        [thumb] => http://i.ytimg.com/vi/80yCiFkOB9g/0.jpg
        [time] => 625
    )

And finally, the kind of stuff you get out of show_methods():

Methods for Zend_Gdata_YouTube_VideoEntry

__construct                   __get                         __isset                       __set                         __toString                    
__unset                       addVideoDeveloperTag          delete                        encode                        ensureMediaGroupIsNotNull     
flushNamespaceLookupCache     getAlternateLink              getAuthor                     getCategory                   getComments                   
getContent                    getContributor                getControl                    getDOM                        getEditLink                   
getEtag                       getExtensionAttributes        getExtensionElements          getFeedLink                   getFlashPlayerUrl             
getHttpClient                 getId                         getLicenseLink                getLink                       getLocation                   
getMajorProtocolVersion       getMediaGroup                 getMediaSource                getMinorProtocolVersion       getNextLink                   
getNoEmbed                    getPreviousLink               getPublished                  getRacy                       getRating                     
getRecorded                   getRights                     getSelfLink                   getService                    getSource                     
getStatistics                 getSummary                    getText                       getTitle                      getTitleValue                 
getUpdated                    getVideoCategory              getVideoCommentFeedUrl        getVideoComplaintsLink        getVideoDescription           
getVideoDeveloperTags         getVideoDuration              getVideoGeoLocation           getVideoId                    getVideoRatingInfo            
getVideoRatingsLink           getVideoRecorded              getVideoResponsesLink         getVideoState                 getVideoTags                  
getVideoThumbnails            getVideoTitle                 getVideoViewCount             getVideoWatchPageUrl          getWhere                      
getXML                        isVideoEmbeddable             isVideoPrivate                lookupNamespace               registerAllNamespaces         
装迷糊 2024-12-06 22:49:38

这是一个有用的 Zend 函数,不仅可以检索下一页,还可以检索提要的所有可用条目。

 $playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName));

This is a useful Zend function for retrieving not only the next page but all available entries for a feed.

 $playlistListFeed = $yt->retrieveAllEntriesForFeed($yt->getPlaylistListFeed($userName));
他是夢罘是命 2024-12-06 22:49:38

听起来您想要遵循有关分页的开发指南示例

It sounds like you want to follow the Dev Guide example on pagination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文