Search code examples
python-3.xyoutubeyoutube-apigoogle-api-python-clientyoutube-data-api

How to get all videos titles in a youtube channgel using Youtube Data API v3?


I'm working on extracting titles of all videos in a youtube channel using Youtube Data API v3.

I followed snippets from https://developers.google.com/youtube/v3/code_samples/python

I'm getting a number when I query for ['statistics']['videoCount']

But when I search for the actual channel in youtube, it is giving a different number for count of videos.

Let's say I'm trying for channel whose ID is - UCeLHszkByNZtPKcaVXOCOQQ

The ['statistics']['videoCount'] is giving 19

However if I search for the channel Post Malone on youtube, it has 36 videos in it. Where am I going wrong?

Does the ['statistics']['videoCount'] actually give exact number of videos in a youtube channel?

Here's my code:

from pprint import pprint
from googleapiclient.discovery import build
import os

YOUTUBE_API_KEY = os.environ.get('YOUTUBE_API_KEY')
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)

lis = ['UCeLHszkByNZtPKcaVXOCOQQ']
for i in lis:
    channels_response = youtube.channels().list(part='statistics', id=i).execute()
    print(i, channels_response['items'][0]['statistics']['videoCount'])
for i in lis:
    channels_response = youtube.channels().list(part='contentDetails', id=i).execute()
    for channel in channels_response['items']:
        uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
        playlistitems_list_request = youtube.playlistItems().list(
            playlistId=uploads_list_id,
            part="snippet",
            maxResults=50
          )
        while playlistitems_list_request:
            playlistitems_list_response = playlistitems_list_request.execute()
            for playlist_item in playlistitems_list_response["items"]:
                # pprint(playlist_item)
                title = playlist_item["snippet"]["title"]
                video_id = playlist_item["snippet"]["resourceId"]["videoId"]
                print(title, video_id)
            playlistitems_list_request = youtube.playlistItems().list_next(
                playlistitems_list_request, playlistitems_list_response
            )

Solution

  • First, you're printing the number of videos from a given YouTube Channel (by using its channel_id).

    Once you have the channel_id, use this request for retrieve the following data:

    • The number of uploaded videos (i.e. its videoCount).
    • The playlistid of the playlist that has the uploaded videos.

    This is the request:

    https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UCeLHszkByNZtPKcaVXOCOQQ&fields=items(contentDetails%2Cid%2Csnippet(country%2Cdescription%2Ctitle)%2Cstatistics%2Cstatus)%2CnextPageToken%2CpageInfo%2CprevPageToken%2CtokenPagination&key={YOUR_API_KEY}
    

    These are the results of the YouTube channel: Post Malone

    You can test these results in the Google API Explorer demo:

    {
     "pageInfo": {
      "totalResults": 1,
      "resultsPerPage": 1
     },
     "items": [
      {
       "id": "UCeLHszkByNZtPKcaVXOCOQQ",
       "snippet": {
        "title": "Post Malone",
        "description": "The official Post Malone YouTube Channel.\nwww.postmalone.com"
       },
       "contentDetails": {
        "relatedPlaylists": {
         "uploads": "UUeLHszkByNZtPKcaVXOCOQQ",
         "watchHistory": "HL",
         "watchLater": "WL"
        }
       },
       "statistics": {
        "viewCount": "967939106",
        "commentCount": "0",
        "subscriberCount": "11072809",
        "hiddenSubscriberCount": false,
        "videoCount": "19"
       }
      }
     ]
    }
    

    Check these two values: uploads and videoCount.

    If you enter to the Post Malone's uploaded videos, you'll get that he has indeed, 19 uploaded videos (the same quantity as shown in the videoCount value).


    In your question you said:

    However if I search for the channel Post Malone on youtube, it has 36 videos in it. Where am I going wrong?

    I don't think you're doing anything wrong, just you don't have the complete spectrum. You see, if you check some of its playlists, you'll see that the 35 videos corresponds to these playlists:

    All his 35 videos are shown in his "videos" tab in his channel.

    To sum it up, the 19 videos corresponds to his 19 uploaded videos (that are grouped in his "uploads" playlist). If you want retrieve all his videos, one option you have is retrieve all the playlists that the YouTube channel has.

    For this case, those videos aren't in is channel really, but in a separate autogenerated YouTube channel, hence, the confusion.