Search script on YouTube from Google API V3. I have the error (urllib.error.HTTPError: HTTP Error 404: Not Found)

0

I am trying to test this script in Google Python to do searches on YouTube. The code I have removed from here:

link

I had to make a couple of changes to make it work with Python 3

The only thing I've done is add parentheses to all print () .

Include this module import urllib.request .

and change these two lines

  freebase_url = FREEBASE_SEARCH_URL % urllib.parse.urlencode(freebase_params)
  freebase_response = json.loads(urllib.request.urlopen(freebase_url).read())

That they were like this before

 freebase_url = FREEBASE_SEARCH_URL % urllib.urlencode(freebase_params)
 freebase_response = json.loads(urllib.urlopen(freebase_url).read())

But he is giving me this error

Traceback (most recent call last):
  File "C:\Users\ivan\Desktop\Codigos_YouTube-buscar\buscar_freebase.py", line 86, in <module>
    mid = get_topic_id(args)
  File "C:\Users\ivan\Desktop\Codigos_YouTube-buscar\buscar_freebase.py", line 24, in get_topic_id
    freebase_response = json.loads(urllib.request.urlopen(freebase_url).read())
  File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Users\ivan\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

The error comes to me in any way that executes the scritp (with arguments and without them). I'm trying to execute it like this:

py -3 C:\Users\ivan\Desktop\Codigos_YouTube-buscar\buscar_freebase.py --query="Barsa Madrid" --type="playlist"

The following is the modified full code:

#!/usr/bin/python

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

import json
import urllib
import urllib.request

# Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps
# tab of
#   https://cloud.google.com/console
# Please ensure that you have enabled the YouTube Data API for your project.
DEVELOPER_KEY = "MI API KEY de google developers"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s"

def get_topic_id(options):
  # Retrieve a list of Freebase topics associated with the provided query term.
  freebase_params = dict(query=options.query, key=DEVELOPER_KEY)
  freebase_url = FREEBASE_SEARCH_URL % urllib.parse.urlencode(freebase_params)
  freebase_response = json.loads(urllib.request.urlopen(freebase_url).read())

  if len(freebase_response["result"]) == 0:
    exit("No matching terms were found in Freebase.")

  # Display the list of matching Freebase topics.
  mids = []
  index = 1
  print("The following topics were found:")
  for result in freebase_response["result"]:
    mids.append(result["mid"])
    print("  %2d. %s (%s)" % (index, result.get("name", "Unknown"),
      result.get("notable", {}).get("name", "Unknown")))
    index += 1

  # Display a prompt for the user to select a topic and return the topic ID
  # of the selected topic.
  mid = None
  while mid is None:
    index = raw_input("Enter a topic number to find related YouTube %ss: " %
      options.type)
    try:
      mid = mids[int(index) - 1]
    except ValueError:
      pass
  return mid


def youtube_search(mid, options):
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
  developerKey=DEVELOPER_KEY)

  # Call the search.list method to retrieve results associated with the
  # specified Freebase topic.
  search_response = youtube.search().list(
    topicId=mid,
    type=options.type,
    part="id,snippet",
    maxResults=options.max_results
  ).execute()

  # Print the title and ID of each matching resource.
  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      print("%s (%s)" % (search_result["snippet"]["title"],
        search_result["id"]["videoId"]))
    elif search_result["id"]["kind"] == "youtube#channel":
      print ("%s (%s)" % (search_result["snippet"]["title"],
        search_result["id"]["channelId"]))
    elif search_result["id"]["kind"] == "youtube#playlist":
      print ("%s (%s)" % (search_result["snippet"]["title"],
        search_result["id"]["playlistId"]))


if __name__ == "__main__":
  argparser.add_argument("--query", help="Freebase search term", default="Google")
  argparser.add_argument("--max-results", help="Max YouTube results",
    default=25)
  argparser.add_argument("--type",
    help="YouTube result type: video, playlist, or channel", default="channel")
  args = argparser.parse_args()

  mid = get_topic_id(args)
  try:
    youtube_search(mid, args)
  except HttpError as e:
    print ("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))

Apparently the error is related to this line

freebase_response = json.loads(urllib.request.urlopen(freebase_url).read()

But I'm not sure what the reason may be. Any questions?

    
asked by Iván Rodríguez 16.06.2018 в 18:34
source

1 answer

0

Ok, I've found the answer to the problem. It all comes down to one word: DEPRECATED .

SOURCE: link

Thanks Google for wasting my time (again)

    
answered by 16.06.2018 / 20:55
source