Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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, <code>Zend_Gdata_YouTube::</code> 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. <em>Update: I made a <a href="http://ca-cycleworks.com/blog/ytfeed/" rel="nofollow">Magento extension out of my answer</a>.</em></p> <p>This is just a generic php I dropped into var/export. At the top, you have to include <code>app/Mage.php</code>.</p> <pre><code>&lt;? $userName='cacycleworksdotcom'; $yt = new Zend_Gdata_YouTube(); ////////////////////////////////////////////////////////////////////// // Get playlists. $playlistListFeed = $yt-&gt;retrieveAllEntriesForFeed($yt-&gt;getPlaylistListFeed($userName)); $playlist=Array(); $videoEntry=NULL; $playlistvideos=Array(); </code></pre> <p>Here, you've got an array of Zend objects in <code>$playlistListFeed</code>. </p> <pre><code>foreach ($playlistListFeed as $idx=&gt;$playlistListEntry) { // process each playlist $playlists[$idx]['title'] = $playlistListEntry-&gt;title-&gt;text; $url=$playlistListEntry-&gt;getSelfLink()-&gt;href; $id=explode("/PL",$url); $playlists[$idx]['id'] = $id[1]; $playlistVideoFeed = $yt-&gt;getPlaylistVideoFeed($playlistListEntry-&gt;getPlaylistVideoFeedUrl()); $playlists[$idx]['time']=0; $playlists[$idx]['views']=0; $playlists[$idx]['rating']=0; </code></pre> <p>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.</p> <pre><code> foreach ($playlistVideoFeed as $videoEntry) { // info of each video inside this playlist $_id=substr($videoEntry-&gt;getVideoWatchPageUrl(),31,11); $playlistvideos[]=$_id; $_url=$videoEntry-&gt;getVideoWatchPageUrl(); $playlists[$idx]['videos'][$_id]=$_url; $playlists[$idx]['time']+=$videoEntry-&gt;getVideoDuration(); $_views=$videoEntry-&gt;getVideoViewCount(); if( $_views &gt; $playlists[$idx]['views'] ) $playlists[$idx]['views']=$_views; $_rating=$videoEntry-&gt;getRating()-&gt;average; if( $_rating &gt; $playlists[$idx]['rating'] ) $playlists[$idx]['rating']=$_rating; } </code></pre> <p>I ended up using the XML to get the thumbnail data.</p> <pre><code> $xml=$playlistListEntry-&gt;getXML(); // $playlists[$idx]['xml']=$xml; // store original XML for now $xml = simplexml_load_string($xml); // transfer into object $attrs=$xml-&gt;group-&gt;thumbnail[1]; $playlists[$idx]['thumb']=(string)$attrs['url']; // 1st vid id playlist id // http://www.youtube.com/watch?v=mcnIAErKc-g&amp;list=PLEDADE9CA0E65BA78 $videoid=array_keys( $playlists[$idx]['videos']); $videoid=$videoid[0]; $playlists[$idx]['url'] = "http://www.youtube.com/watch?v=$videoid&amp;list=PL".$playlists[$idx]['id']; } </code></pre> <p>The playlists are handled, now let's get the remaining videos that aren't in playlists:</p> <pre><code>////////////////////////////////////////////////////////////////////// // Videos themselves $idx=count($playlists); $userFeed = $yt-&gt;getUserUploads($userName); foreach ($userFeed as $videoEntry) { $idx++; $_id=substr($videoEntry-&gt;getVideoWatchPageUrl(),31,11); if( ! in_array($_id, $playlistvideos) ) { $_url=$videoEntry-&gt;getVideoWatchPageUrl(); $playlists[$idx]['id']=$_id; $playlists[$idx]['url']=$_url; $playlists[$idx]['title']=$videoEntry-&gt;title-&gt;text; $playlists[$idx]['views']=$videoEntry-&gt;getVideoViewCount(); $playlists[$idx]['rating']=$videoEntry-&gt;getRating()-&gt;average; $thumbs=$videoEntry-&gt;getVideoThumbnails(); // these need resizing to width="320" height="180" $playlists[$idx]['thumb']=$thumbs[0]['url']; $playlists[$idx]['time']=$videoEntry-&gt;getVideoDuration(); } // else { echo "$_id already in playlist\n"; } } </code></pre> <p>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.</p> <pre><code>////////////////////////////////////////////////////////////////////// // 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); </code></pre> <p>Here is the code that helped me get started working with these goofy GData YouTube Zend objects:</p> <pre><code>echo "\n\n"; show_methods($videoEntry); echo "\n\n"; show_methods($playlistListFeed[0]); echo "\n\n"; show_methods($playlistListFeed); function show_methods( $_a ) { echo "&lt;h3&gt;Methods for ".get_class($_a)."&lt;/h3&gt;"; $_a= get_class_methods($_a); $_a=array_unique($_a); array_multisort(&amp;$_a); $i=0; foreach( $_a as $method ) { $i++; printf("%-30.30s",$method); if($i%5==0) echo "\n"; } } </code></pre> <p>Here are two of the array entries to show the structure. Note that playlists have a <code>videos</code> key with an array of the videos inside it. Testing for the <code>videos</code> key can tell you it's a playlist. The <code>url</code> is what your user can click on to open the video or playlist on the youtube site.</p> <pre><code>[1] =&gt; Array ( [title] =&gt; Ducatitech.com "HowTo" Adjust your Valves [id] =&gt; 970EC735D36A95E8 [time] =&gt; 855 [views] =&gt; 144847 [rating] =&gt; 4.9322033 [videos] =&gt; Array ( [dIj3nSJGPZw] =&gt; http://www.youtube.com/watch?v=dIj3nSJGPZw&amp;feature=youtube_gdata_player [3WQY1MRlmH4] =&gt; http://www.youtube.com/watch?v=3WQY1MRlmH4&amp;feature=youtube_gdata_player ) [thumb] =&gt; http://i.ytimg.com/vi/dIj3nSJGPZw/mqdefault.jpg [url] =&gt; http://www.youtube.com/watch?v=dIj3nSJGPZw&amp;list=PL970EC735D36A95E8 ) [thumb] =&gt; http://i.ytimg.com/vi/mcnIAErKc-g/mqdefault.jpg [url] =&gt; http://www.youtube.com/watch?v=mcnIAErKc-g&amp;list=PLEDADE9CA0E65BA78 ) [7] =&gt; Array ( [id] =&gt; 80yCiFkOB9g [url] =&gt; http://www.youtube.com/watch?v=80yCiFkOB9g&amp;feature=youtube_gdata_player [title] =&gt; Ducatitech.com: ExactFit Timing Belt Tensile Test [views] =&gt; 7589 [rating] =&gt; 4.25 [thumb] =&gt; http://i.ytimg.com/vi/80yCiFkOB9g/0.jpg [time] =&gt; 625 ) </code></pre> <p>And finally, the kind of stuff you get out of <code>show_methods()</code>:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload