1

I have been trying to get all videos of a given channel ID. But I am not getting all videos

code I tried to retrive all the videos of the channel:

api_key =  API_KEY
base_video_url  =  'https://www.youtube.com/watch?v='
base_search_url  =  'https://www.googleapis.com/youtube/v3/search?'
raw_url = 'https://www.googleapis.com/youtube/v3/' \
    'channels?key={}&forUsername={}&part=id'

def getChannelID(username):
    ''' returns the channel ID '''
    r=requests.get(raw_url.format(api_key,username))
    json=r.json()
    print(json['items'][0]['id'])
    return json['items'][0]['id']

def getchannelVideos():
   ''' returns list of all videos of a given channel '''
   chanId=getChannelID('tseries')
   first_url = base_search_url + \
          'order=date&part=snippet&channelId={}&maxResults=50&key={}'\
                        .format(chanId,api_key)

   video_links = []
   url = first_url
   while True:
      inp = requests.get(url)
      resp = inp.json()

      for i in resp['items']:
          if i['id']['kind'] == "youtube#video":
              video_links.append(base_video_url + i['id']['videoId'])

      try:
          next_page_token = resp['nextPageToken']
          url = first_url + '&pageToken={}'.format(next_page_token)
      except:
          break
      print('working') #used this to count repetitions of while loop
   return video_links

here the given channel is T-Series which has 11,537 videos so far [ click to see the image of the channel showing the count ] But I received 589 videos only

I used this line to count the no. of iterations while loop would do

  print('working')

for this I observed that the while loop ends after 19 iterations ( I have tried on many channels But same is repeating )

the is last (19th iteration) Json data I was provided

{'etag': "cbz3lIQ2N25AfwNr-BdxUVxJ_QY/7SEM6nSU4tBD7ZsR5Abt5L-uqAE",
 'items': [],
 'kind': 'youtube#searchListResponse',
 'pageInfo': {'resultsPerPage': 50, 'totalResults': 15008},
 'prevPageToken': 'CLYHEAE',
 'regionCode': 'IN'}

why API not providing nextpageID though totalResults are 15008 ??

Harry
  • 33
  • 1
  • 9
  • AFAIK the uploaded video count of a channel is retrieved via videoCount key value when listing statistics for a Channel object. Possible problem in your implementation: maxResults=50 in your GETs – BoboDarph Oct 18 '17 at 15:08
  • @BoboDarph `maxResults=50` is an implemented limit on the API side – MrAlexBailey Oct 18 '17 at 15:15

2 Answers2

3

Search call is not meant to be used to enumerate channels.

I was HTML scraping the channel > videos page until I found this recently.

https://stackoverflow.com/a/27872244/975887

Essentially the steps are

  • find the channel ID of the channel you want.
  • list playlists (youtube.channels.list set id to channelId and set part to contentDetails)
  • find the ID of the playlist with name uploads
  • list playlist items (youtube.playlistItems.list set playlistId and set part to snippet optionally set maxResults to 50)
  • Page through results using nextPageToken

If you only know a video ID, you can call youtube.videos.list with id set to video id and part set to snippet and extract the channel ID from the result.

This lists all videos uploaded by the channel, and unlike the search call, does not give up after a few 100 items, and results are always from the specified channel.

As an added bonus, this costs only 1-3 quota points per call (depending on what other parts you're requesting) compared to the search call which costs 100+ quota points for each call.

Madushan
  • 6,977
  • 31
  • 79
0

As have been mentioned already in the comments, the max videos that can be retrieved from the result is 50. So if you wanted to access the other 51-100 and so forth, you'd have to use nextPageToken:

pageToken

The pageToken parameter identifies a specific page in the result set that should be returned. In an API response, the nextPageToken and prevPageToken properties identify other pages that could be retrieved.

Check this Python on App Engine Code Samples for sample in making Youtube API calls using Python.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56