0

I have a python/html file that uses the YouTube api to run a few simple searches. It runs completely fine when I have it on a local server but whenever I upload it to Replit, I get the following error whenever I press the "reccomend" button.

This is the code python code:

from flask import Flask, jsonify, request, render_template
from googleapiclient.discovery import build

app = Flask(__name__)


@app.route('/')
def index():
  return render_template('index.html')


@app.route('/recommend', methods=['GET'])
def recommend():
  # Get the YouTube link from the query parameter
  youtube_link = request.args.get('link')

  # Parse the video ID from the link
  video_id = youtube_link.split('v=')[-1]

  # Initialize the YouTube API client
  youtube = build('youtube',
                  'v3',
                  developerKey='AIzaSy...')

  # Get the details of the video
  video_response = youtube.videos().list(part='snippet', id=video_id).execute()

  # Extract the video title and channel ID
  video_title = video_response['items'][0]['snippet']['title']
  channel_id = video_response['items'][0]['snippet']['channelId']

  # Get the details of the channel
  channel_response = youtube.channels().list(part='snippet,statistics',
                                             id=channel_id).execute()

  # Extract the channel title and subscriber count
  channel_title = channel_response['items'][0]['snippet']['title']
  subscriber_count = channel_response['items'][0]['statistics'][
    'subscriberCount']

  # Get the related videos for the video
  related_response = youtube.search().list(
    part='id,snippet', type='video', relatedToVideoId=video_id).execute()

  # Filter the related videos to only include videos from channels with less than 1000 subscribers
  recommendations = []
  for item in related_response['items']:
    channel_id = item['snippet']['channelId']
    video_id = item['id']['videoId']

    channel_response = youtube.channels().list(part='statistics',
                                               id=channel_id).execute()

    subscriber_count = int(
      channel_response['items'][0]['statistics']['subscriberCount'])
    if subscriber_count < 1000:
      recommendations.append({
        'id': video_id,
        'title': item['snippet']['title']
      })

  return jsonify(recommendations)


if __name__ == '__main__':
  app.run(host='0.0.0.0', port=5000, debug=True)

and this is the error i'm getting:

 File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/flask/app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/runner/SmallChannelReccomender/main.py", line 21, in recommend
    youtube = build('youtube',
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/googleapiclient/discovery.py", line 287, in build
    content = _retrieve_discovery_doc(
  File "/home/runner/SmallChannelReccomender/venv/lib/python3.10/site-packages/googleapiclient/discovery.py", line 404, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion(
googleapiclient.errors.UnknownApiNameOrVersion: name: youtube  version: v3

Have changing API key, tried changing the API version but none of this seems to be making a difference. I even ran it through ChatGPT but this just told me the API version was wrong which I know is not the case. I have the package installed for the APi as well.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Stanners
  • 1
  • 2

1 Answers1

0

I have no idea why this works but adding static_discover=False to the build works for me, I had the exact same issue.

googleapiclient.discovery.build('youtube', 'v3', developerKey=API_KEY, static_discovery=False)

Richard Askew
  • 1,217
  • 1
  • 10
  • 16