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 ??