Skip to content

Instantly share code, notes, and snippets.

@javathunderman
Created August 28, 2018 21:46
Show Gist options
  • Save javathunderman/b8e417ac3e4d2871c0879eb5f55a9b03 to your computer and use it in GitHub Desktop.
Save javathunderman/b8e417ac3e4d2871c0879eb5f55a9b03 to your computer and use it in GitHub Desktop.

Revisions

  1. javathunderman created this gist Aug 28, 2018.
    35 changes: 35 additions & 0 deletions least_popular.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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'])