API

2011-08-15 3 views
5

그래서, 여기에 유튜브 사용자의 공개 재생 목록을 얻기를위한 내 코드의를 통해 YouTube 사용자를위한 완벽한 재생 목록을 가져 오기 :이 첫 번째 페이지 또는 결과를 얻을,API

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는 "시작 인덱스"또는 "최대-결과"를 지정하는 매개 변수를 갖고있는 것 같다하지 않습니다.

+0

나는 내가 따라 URI 말했다 매개 변수를 사용하고 수정하는 getPlaylistListFeed을 수정할 수 있습니다 알고, 그것은 말했다해야 가정 , 그러나 나는 어떻게 든 Zend 제품에서 그런 단점을 발견했다고 생각하지 않는다. – poolnoodl

답변

1

Dev Guide example on pagination을 따르고 싶은 것 같습니다.

+0

나를 위해이 게시물을 도울 수 있습니다. http://stackoverflow.com/questions/24210689/how-to-get-time-value-from-youtube-playlist – user3501407

2

피드의 다음 페이지뿐만 아니라 사용 가능한 모든 항목을 검색하는 데 유용한 Zend 기능입니다. 나는 모든 일이 "내 웹 사이트에 표시 할 내 동영상의 목록을 얻으려고 노력"낭비 때문에

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

, 나는 그냥 나에게 많은 것을 가르쳐 일부 샘플 코드를 붙여 줄 알았는데. 예상대로 Zend_Gdata_YouTube::은 이미 Magento에 설치되어 있습니다. 아래 코드는 재생 목록이 다른 동영상과 나란히 목록에 표시되는 '동영상'의 통합 목록을 제공합니다. 업데이트 : 나는 Magento extension out of my answer을 만들었습니다.

이것은 var/export에 포함 된 일반적인 PHP입니다. 상단에서 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에 젠드 객체의 배열을 가지고있다.

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을 사용했다.

다음
////////////////////////////////////////////////////////////////////// 
// 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 동영상의 배열을 가지고이 playlsits에 의해 정렬 :

재생 목록의 처리
$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'];  
} 

, 지금의 재생 목록에없는 나머지 비디오를하자 가장 오래된 것부터 시작하여 사용자의 동영상이 동일한 가장 오래된 첫 번째 순서로 재생 목록에 나타나지 않습니다. 그래서이 간단한 정렬 코드를 사용하여 순서를 변경했습니다. 다차원 배열을 정렬하려고 여기에 있다면 읽기에 가치있는 정렬과 가치에 관한 멋진 기사입니다. 여기

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"; 
    } 
} 

이 구조를 보여 배열 항목이 있습니다 : 여기

////////////////////////////////////////////////////////////////////// 
// 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 유튜브 젠드 객체와 작업을 시작 도움 코드입니다. 재생 목록에는 동영상 배열이 포함 된 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