import requests import json uname = input("Username or channel ID\n") APIkey = "" if("U" == uname[0]): channelID = uname else: IDlookup = requests.get("https://www.googleapis.com/youtube/v3/channels?key=" + APIkey + "&forUsername=" + uname + "&part=id&type=video") IDClean = IDlookup.json() channelID = IDClean['items'][0]['id'] print(channelID) firstRaw = requests.get("https://www.googleapis.com/youtube/v3/search?key=" + APIkey + "&channelId=" + channelID + "&part=snippet,id&order=viewCount&maxResults=50&type=video") firstParse = firstRaw.json() if(firstParse['pageInfo']['totalResults'] <= 50): try: print("https://youtube.com/watch?v=" + firstParse['items'][-1]['id']['videoId']) except KeyError: print("https://youtube.com/watch?v=" + firstParse['items'][-2]['id']['videoId']) else: nextPage = firstParse['nextPageToken'] jsonContent = "asdf" while(jsonContent != "[]"): jsonContent = "" nextPageContent = requests.get("https://www.googleapis.com/youtube/v3/search?pageToken=" + nextPage + "&key=" + APIkey + "&channelId=" + channelID +"&part=snippet,id&order=viewCount&maxResults=50&type=video") nextPageClean = nextPageContent.json() jsonContent = str(nextPageClean['items']) try: nextPage = (nextPageClean['nextPageToken']) except KeyError: break previousPage = nextPageClean['prevPageToken'] previousPageContent = requests.get("https://www.googleapis.com/youtube/v3/search?pageToken=" + previousPage + "&key=" + APIkey + "&channelId=" + channelID +"&part=snippet,id&order=viewCount&maxResults=50&type=video") previousPageClean = previousPageContent.json() print("https://youtube.com/watch?v=" + previousPageClean['items'][-1]['id']['videoId']) print("https://youtube.com/watch?v=" + previousPageClean['items'][-2]['id']['videoId'])