0

I've been trying to create a YouTube Rank Checker in python. The idea is it searches on YouTube for a keyword and checks what position the video is.

The only problem is it isn't returning accurate results. For instance if I search for "dog video" and my video is position 3 when I search using the normal website and the API returns it as position 17. I was thinking this could be because it's sorting the results differently (It should be sorting by relevance) but I can't figure out how to chance the sort order.

If anyone can provide any help, even if it's just pointing out something obvious in my code I would appreciate it very much, thank you for your help in advance.

Here's my code:

from apiclient.discovery import build
from optparse import OptionParser
import time

# Set DEVELOPER_KEY to the "API key" value from the "Access" tab of the
# Google APIs Console http://code.google.com/apis/console#access
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = "devkey"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
videos = []
channels = []
playlists = []
videoids = []
videoidskeys = {"cat video":"ZBAGEeOms-8", "dog video":"nGeKSiCQkPw", "testing video":"GAyni_fKKLg"}
videokeys = videoidskeys.keys()

def checkrank(videoid, keyword):
  time.sleep(6)
  if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("--q", dest="q", help="Search term",
      default=keyword)
    parser.add_option("--max-results", dest="maxResults",
      help="Max results", default=25)
    (options, args) = parser.parse_args()
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)
  search_response = youtube.search().list(
    q=options.q,
    part="id,snippet",
    maxResults=options.maxResults,
  ).execute()

  for search_result in search_response.get("items", []):
    videoids.append(search_result["id"]["videoId"])
  count = 0

  for videoid1 in videoids:
    count = count+1
    if videoid1 == videoid:
      return "Keyword: %s - Rank: %s" % (keyword, count)
      count = 0


for video in videokeys:
  print checkrank(videoidskeys[video],video)

1 Answer 1

-1

Figured it out, here's the working code in case anyone wants it:

http://pastebin.com/Vei3FyUz

Also YouTube personalizes results if you're logged in, so be sure to log out when checking accuracy.

1
  • The pastebin link has expired. It should have been in the post itself. Aug 12, 2022 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.