开发者

How can I get all videos ID's from a Youtube channel

开发者 https://www.devze.com 2023-03-13 02:58 出处:网络
How can I get all video id\'s from the youtube data feed? I receive the youtube feed via this (API) URL:

How can I get all video id's from the youtube data feed?

I receive the youtube feed via this (API) URL: http://gdata.youtube.com/feeds/base/users/#userid#/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile

I already know how to extract the links, descriptions and thumbnails from a Channe开发者_如何学JAVAl, but I want to extract all the video Id's from a Channel (e.g. http://www.youtube.com/watch?v=WWooNnPnHTs)


This is my way. Slow, but it works. :)

function getVideos($channel){
    if($channel == ""){
        return false;   
    }
    /* Get number of videos */
    $books = simplexml_load_file('http://gdata.youtube.com/feeds/base/users/'.$channel.'/uploads?max-results=1&start-index=1');
    $numb_videos = $books->children( 'openSearch', true )->totalResults; 
    settype($numb_videos, "integer");

    $ids = array();
    $i = 1;
    for($i = 1; $i <= $numb_videos; $i++){
        $books = simplexml_load_file('http://gdata.youtube.com/feeds/base/users/'.$channel.'/uploads?max-results=1&start-index='.$i);
        $ApiLink  = $books->entry->id;
        settype($ApiLink, "string");
        $ApiLink = str_replace("http://gdata.youtube.com/feeds/base/videos/", "", $ApiLink);
        array_push($ids, $ApiLink);
    }
    return $ids;    
}


Use this:

GET https://www.googleapis.com/youtube/v3/search?part=id&channelId=UC9MAhZQQd9egwWCxrwSIsJQ&maxResults=10&order=date&key={YOUR_API_KEY}

And you will get a result for the above url as:

200 OK

- Show headers -

{
"kind": "youtube#searchListResponse",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/WiiEAt3fgPkFw_831Iveo6mV-IU\"",
"nextPageToken": "CAQQAA",
"pageInfo": {
"totalResults": 1046,
"resultsPerPage": 4
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/OtU1Ja-W-gNf83iiXWzodKk3Ce0\"",
"id": {
"kind": "youtube#video",
"videoId": "jKLMD-LXIgk"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/EUhMCxemh2UGmf2ufGS0IYdcMUs\"",
"id": {
"kind": "youtube#video",
"videoId": "glCQQeH_ddw"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/2IMOnedhjKXxnFZy-PNg5o80kkY\"",
"id": {
"kind": "youtube#video",
"videoId": "yB78MIcmDxs"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/oEb7q9_GwGdXcHsvuRDuNmh_rGQ\"",
"id": {
"kind": "youtube#video",
"videoId": "NnkDja1cXlo"
}
}
]
}

For more reference you may check here


$channelsResponse = $youtube->channels->listChannels('id,contentDetails', array(
    'mine' => 'true'));

$playlistId = $channelsResponse['items']['contentDetails']['relatedPlaylists']['uploads'];
$searchResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $playlistId,
        'maxResults' => 50,
        'fields' => 'items(snippet(publishedAt,channelId,title,description,thumbnails(default),resourceId)),pageInfo,nextPageToken'));

echo json_encode($searchResponse['items']['contentDetails']['videoId']);

use youtube data api v3 to do that. here's the link


Hi I think you should have to retrieve the value from the v parameter in the link of the video, you can use regex or substrings...


I know it's an old question, but I wrote up something that does what is asked for here:

https://gist.github.com/gport/5693404

Just edit line 23:

return $videoids[array_rand($videoids)];

To:

return $videoids;

This will return an array containing the users video ID's :)

edit: sorry guys ( @testing, @Code_Ed_Student ), no idea why the gist is not there anymore but I found it in my archives, code below. The implementation is a bit different from in the old example, but this achieves the same (you could replace the array by any other logic).

    $feedresult = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/wondervol/uploads');

    if($feedresult) {

    $videoids = array();

    foreach ($feedresult->entry as $video) {

            $media = $video->children('media', true);
            $url = (string)$media->group->player->attributes()->url;
            $index = strrpos($url, "&");
            $url = substr($url, 0, $index);
            $index = strrpos($url, "watch");
            $url = substr($url, $index + 8, strlen($url) - ($index + 8)); 

            $videoids[] = $url;

    }


It might look like the search API is a good idea to retrieve all the videos by a single loop, but apparently not. If a channel has thousands (more than 1600) of videos, search will not return all of them: few last result pages will be empty, number of results will be wrong, and the amount of given pages will be around only 20 pages.

So, if you need to get all IDs via API, then it's better to use at first channels API to get an "uploads" playlist ID, and then use it in playlistItems API to retrieve all videos.

First request URL will look like this (using https://www.googleapis.com/youtube/v3/ in the beginning, of course):

channels?part=contentDetails&id=CHANNEL_ID&key=YOUR_TOKEN

Result:

{
    "kind": "youtube#channelListResponse",
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Nza6t_W5qltBCnHIHKgNiSRu0bE\"",
    "pageInfo": {
    "totalResults": 1,
        "resultsPerPage": 1
    },
    "items": [
        {
            "kind": "youtube#channel",
            "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/95y8HKrtWV3t4apHu0KjgoaNbc0\"",
            "id": "UC9-y-6csu5WGm29I7JiwpnA",
            "contentDetails": {
                "relatedPlaylists": {
                    "likes": "LL9-y-6csu5WGm29I7JiwpnA",
                    "favorites": "FL9-y-6csu5WGm29I7JiwpnA",
                    "uploads": "UU9-y-6csu5WGm29I7JiwpnA",
                    "watchHistory": "HL",
                    "watchLater": "WL"
                }
            }
        }
    ]
}

Or if you don't need tons of bytes, use filter fields like this:

channels?fields=items(contentDetails(relatedPlaylists(uploads)))&part=contentDetails&id=CHANNEL_ID&key=YOUR_TOKEN

Filtered result:

{
    "items": [
        {
            "contentDetails": {
                "relatedPlaylists": {
                    "uploads": "UU9-y-6csu5WGm29I7JiwpnA"
                }
            }
        }
    ]
}

Next step is to get list of all pages with videos:

playlistItems?maxResults=50&playlistId=PLAYLIST_ID&part=snippet&key=YOUR_TOKEN

Result (shorten by 1st result of array in items key):

{
    "kind": "youtube#playlistItemListResponse",
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/GmoHN7IVzBjFEtB0547SRegTL9c\"",
    "nextPageToken": "CDIQAA",
    "pageInfo": {
        "totalResults": 399,
        "resultsPerPage": 50
    },
    "items": [
        {
            "kind": "youtube#playlistItem",
            "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/5gxLp2iP0FwcZQWb98LCOGu0TfA\"",
            "id": "VVU5LXktNmNzdTVXR20yOUk3Sml3cG5BLjlua3RyMU1nUy1B",
            "snippet": {
                "publishedAt": "2017-08-03T11:27:38.000Z",
                "channelId": "UC9-y-6csu5WGm29I7JiwpnA",
                "title": "Stop Button Solution? - Computerphile",
                "description": "After seemingly insurmountable issues with Artificial General Intelligence, Rob Miles takes a look at a promising solution.\n\nConcrete Problems in AI Safety: blah blah blah blah blah blah blah blah blah",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/default.jpg",
                        "width": 120,
                        "height": 90
                    },
                    "medium": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/mqdefault.jpg",
                        "width": 320,
                        "height": 180
                    },
                    "high": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/hqdefault.jpg",
                        "width": 480,
                        "height": 360
                    },
                    "standard": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/sddefault.jpg",
                        "width": 640,
                        "height": 480
                    },
                    "maxres": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/maxresdefault.jpg",
                        "width": 1280,
                        "height": 720
                    }
                },
                "channelTitle": "Computerphile",
                "playlistId": "UU9-y-6csu5WGm29I7JiwpnA",
                "position": 0,
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "9nktr1MgS-A"
                }
            }
        },
        {
            ...
        }
    ]
}

Or the filtered one to get only IDs of the videos (and next page tokens):

playlistItems?fields=nextPageToken,items(snippet(resourceId(videoId)))&maxResults=50&playlistId=PLAYLIST_ID&part=snippet&key=YOUR_TOKEN

Result:

{
    "nextPageToken": "CDIQAA",
    "items": [
        {
            "snippet": {
                "resourceId": {
                    "videoId": "9nktr1MgS-A"
                }
            }
        }
    ]
}

You can retrieve next pages by using nextPageToken key value, adding it to GET parameters with pageToken key:

(I took a token from the result above just for example, it could be different for your request)

playlistItems?fields=nextPageToken,items(snippet(resourceId(videoId)))&maxResults=50&part=snippet&playlistId=PLAYLIST_ID&key=YOUR_TOKEN&pageToken=CDIQAA

On the last page there will be no nextPageToken key.


My solution uses Python, Google API, and paginates to reach all the video Ids.

  • Create a .env file to store your Developer Key and put it in the .gitignore file
  • The parameter "forUserName" should be set with the name of the Youtube Channel (username). Alternatively, you can use the channel id, setting the parameter "id", instead of "forUserName".
import os, sys
import googleapiclient.discovery
from decouple import config
def main():
    os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"

    api_service_name = "youtube"
    api_version = "v3"
    DEVELOPER_KEY = config('API_KEY')

    youtube = googleapiclient.discovery.build(
        api_service_name, api_version, developerKey = DEVELOPER_KEY)

    request = youtube.channels().list(
        part="contentDetails",
        forUsername="username_here",
        # id="uiyiouyiouy",
    )

    response = request.execute()
    for item in response['items']:
        playlistId = item['contentDetails']['relatedPlaylists']['uploads']
        nextPageToken = ''
        while (nextPageToken != None):
            playlistResponse = youtube.playlistItems().list(
                part='snippet',
                playlistId=playlistId,
                maxResults=25,
                pageToken=nextPageToken
            )
            playlistResponse = playlistResponse.execute()
            for idx, playlistItem in enumerate(playlistResponse['items']):
                print(idx, playlistItem['snippet']['resourceId']['videoId'])
            if 'nextPageToken' in playlistResponse.keys():
                nextPageToken = playlistResponse['nextPageToken']
            else:
                nextPageToken = None

if __name__ == "__main__":
    main()

Example for the .env file

API_KEY=<Key_Here>


Notice: Array to string conversion in

C:\wamp\www\youtube-vimeo-api-playlist-to-database-importer-master\youtube-vimeo-api-playlist-to-database-importer.php

on line 28

0

精彩评论

暂无评论...
验证码 换一张
取 消