Created
August 28, 2018 21:46
-
-
Save javathunderman/b8e417ac3e4d2871c0879eb5f55a9b03 to your computer and use it in GitHub Desktop.
Gets the least popular YT video(s) of any channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment