dongpa5277 2014-09-04 20:25
浏览 173
已采纳

从频道中获取所有YouTube视频(缺少某些视频)

I'm using the v3 Google API for Youtube:

$url = 'https://www.googleapis.com/youtube/v3/search?part=id&channelId=' . $channelID . '&maxResults=50&order=date&key=' . $API_key;

I've set up a script which should give me all videos from a given channel ID. For some channels I get all videos, for some a few are missing (compared with the number of videos shown directly in Youtube), and for bigger channel I get a max. result of 488 videos despite there are more.

The pageToken is a strange thing. For example a channel has 955 videos. I get 18 pages with 50 items per page (that would be 900 videos). Some of them are playlists but if I subtract the 23 playlists I still have 877 videos. If I remove duplicates I only have 488 results! The totalResults in the JSON output shows me 975 results!?

This is my recursive function:

function fetchAllVideos($parsed_json){
    $foundIds = array();
    if($parsed_json != ''){
        $foundIds = getVideoIds($parsed_json);
        $nextPageToken = getNextPageToken($parsed_json);
        $prevPageToken = getPrevPageToken($parsed_json);

        if($nextPageToken != ''){
            $new_parsed_json = getNextPage($nextPageToken);
            $foundIds = array_merge($foundIds, fetchAllVideos($new_parsed_json));
        }
        if($prevPageToken != ''){
            $new_parsed_json = getNextPage($prevPageToken);
            $foundIds = array_merge($foundIds, fetchAllVideos($new_parsed_json));
        }
    }

    return $foundIds;
}

I call it with $videoIds = fetchAllVideos($parsed_json); and $parsed_json is the result from the first URL which I retrieve. Can you see an error here?

Does anybody know how the number of videos are counted, which are directly shown in Youtube? Has anybody managed to get a full list which correspond to the number in Youtube?

  • 写回答

2条回答 默认 最新

  • dougan7657 2014-11-04 21:08
    关注

    https://gdata.youtube.com/feeds/api/users/USERNAME_HERE/uploads?max-results=50&alt=json&start-index=1 did the trick. It's a JSON feed where you have to loop until you get less than 50 results.

    Edit:

    This should be the script I used:

    ini_set('max_execution_time', 900);
    
    function getVideos($channel){
        $ids = array();
        $start_index = 1;
        $still_have_results = true;
    
        if($channel == ""){
            return false;   
        }
    
        $url = 'https://gdata.youtube.com/feeds/api/users/' . $channel . '/uploads?max-results=50&alt=json&start-index=' . $start_index;
        $json = file_get_contents($url);
        $obj = json_decode($json);
    
        while($still_have_results){
            foreach($obj->feed->entry as $video){
                $video_url = $video->id->{'$t'};
                $last_pos = strrpos($video_url, '/');
                $video_id = substr($video_url, $last_pos+1, strlen($video_url) - $last_pos);
                array_push($ids, $video_id);
            }
            $number_of_items = count($obj->feed->entry);
            $start_index += count($obj->feed->entry);
            if($number_of_items < 50) {
                $still_have_results = false;
            }
    
            $url = 'https://gdata.youtube.com/feeds/api/users/' . $channel . '/uploads?max-results=50&alt=json&start-index=' . $start_index;
            $json = file_get_contents($url);
            $obj = json_decode($json);
        }
    
        return $ids;    
    }
    
    $videoIds = getVideos('youtube');
    echo '<pre>';
    print_r($videoIds);
    echo '</pre>';
    

    Now I made a test, but I didn't gathered 100% of the videos. Nevertheless, the best option I came up with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。