sagive

Youtube Playlist data extraction example

Sep 24th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.51 KB | None | 0 0
  1. <?php
  2. if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['getUtubePlaylist'] )) {
  3.        
  4.         $link = $_POST['youtubePlaylist'];
  5.         $link = str_replace('http://www.youtube.com/playlist?list=PL', '', $link);
  6.  
  7.         // set feed URL
  8.         $feedURL = 'http://gdata.youtube.com/feeds/api/playlists/'.$link;
  9.        
  10.         // read feed into SimpleXML object
  11.         $sxml = simplexml_load_file($feedURL);
  12.         echo '<h1>'.$sxml->title.'</h1>';
  13.        
  14.         // iterate over entries in feed
  15.         foreach ($sxml->entry as $entry) {
  16.               // get nodes in media: namespace for media information
  17.               $media = $entry->children('http://search.yahoo.com/mrss/');
  18.              
  19.               // get video player URL
  20.               $attrs = $media->group->player->attributes();
  21.               $watch = $attrs['url'];
  22.              
  23.               // embed url usign $watch value
  24.               $embedUrl         = str_replace('&feature=youtube_gdata_player', '', $watch);
  25.               $embedUrl         = str_replace('http://www.youtube.com/watch?v=', '', $embedUrl);
  26.               $onlyUrl          = $embedUrl;
  27.               $cleanEmbedUrl    = 'httpv://youtu.be/'.$embedUrl;
  28.               $embedUrl         = '<iframe width="320" height="280" src="http://www.youtube.com/embed/'.$embedUrl.'" frameborder="0" allowfullscreen></iframe>';
  29.              
  30.               // get video thumbnail
  31.               $attrs = $media->group->thumbnail[0]->attributes();
  32.               $thumbnail = $attrs['url'];
  33.                    
  34.               // get <yt:duration> node for video length
  35.               $yt = $media->children('http://gdata.youtube.com/schemas/2007');
  36.               $attrs = $yt->duration->attributes();
  37.               $length = $attrs['seconds'];
  38.              
  39.               // get <yt:stats> node for viewer statistics
  40.               $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
  41.               $attrs = $yt->statistics->attributes();
  42.               $viewCount = $attrs['viewCount'];
  43.              
  44.               // get <gd:rating> node for video ratings
  45.               $gd = $entry->children('http://schemas.google.com/g/2005');
  46.               if ($gd->rating) {
  47.                 $attrs = $gd->rating->attributes();
  48.                 $rating = $attrs['average'];
  49.               } else {
  50.                 $rating = 0;
  51.               }
  52.         ?>
  53.         <div class="videoItem fullb p3 mb2">
  54.             <div class="videoTitle mb1">
  55.                 <?php _e('Video Name: ', 'sagive'); ?><a href="<?php echo $watch; ?>"><?php echo $media->group->title; ?></a>
  56.             </div>
  57.            
  58.             <div class="videoDesc mb1"><?php echo __('Video Description: ', 'sagive').$media->group->description; ?></div>
  59.            
  60.             <div class="innerAttr mb1 p1" style="border: 1px solid #ccc;">
  61.                 <form name="form<?php echo $onlyUrl; ?>" action="">
  62.                     <?php echo __('Video Embed Code: ', 'sagive'); ?>
  63.                     <input type="text" id="<?php echo $onlyUrl; ?>" name="<?php echo $onlyUrl; ?>" value="<?php echo $cleanEmbedUrl; ?>" />
  64.                     <input type="button" value="<?php _e('Copy', 'sagive'); ?>" class="button orangeNum" />
  65.                 </form>
  66.             </div>
  67.  
  68.             <div class="videoContainer">
  69.                 <div class="thumbnail fright w50 ml5">
  70.                 <a href="<?php echo $watch; ?>"><img src="<?php echo $thumbnail;?>" /></a>
  71.                 </div>
  72.                
  73.                 <div class="videoPlayer fright w45">
  74.                 <?php echo $embedUrl; ?>
  75.                 </div>
  76.                
  77.                 <div class="fix"></div>
  78.             </div>
  79.            
  80.             <div class="attr mt1">
  81.                 <div class="innerAttr fright"><?php _e('By:', 'sagive'); ?> <?php echo $entry->author->name; ?> </div>
  82.                 <div class="innerAttr fright"><?php _e('Duration:', 'sagive'); ?> <?php printf('%0.2f', $length/60); ?> </div>
  83.                 <div class="innerAttr fright"><?php _e('Views:', 'sagive'); ?> <?php echo $viewCount; ?> </div>
  84.                 <div class="innerAttr fright"><?php _e('Rating:', 'sagive'); ?> <?php echo $rating; ?> </div>
  85.                 <div class="fix"></div>
  86.             </div>
  87.         </div>      
  88. <?php
  89.     }
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment