gpt4 book ai didi

python - Youtube API每个请求或错误返回1个视频

转载 作者:行者123 更新时间:2023-12-03 05:53:15 27 4
gpt4 key购买 nike

我创建了从YouTube API搜索查询中提取数据的代码:

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
from oauth2client import client, GOOGLE_TOKEN_URI
import json

DEVELOPER_KEY = "my key"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def youtube_search(q, max_results=50,order="relevance", token=None, location=None, location_radius=None):

youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY)

search_response = youtube.search().list(q=q, type="video", pageToken=token, order = order, part="id,snippet", maxResults=max_results, location=location, locationRadius=location_radius).execute()

videos = []

for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append(search_result)
try:
nexttok = search_response["nextPageToken"]
return(nexttok, videos)
except Exception as e:
nexttok = "last_page"
return(nexttok, videos)

当我使用搜索查询测试该功能时,
youtube_search('sport')

有时返回超时 错误:
timeout: The read operation timed out

有时会返回ConnectionResetError 错误:
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

有时每个 token 仅返回 一个视频:
('CDIQAA',
[{'kind': 'youtube#searchResult',
'etag': '"Fznwjl6JEQdo1MGvHOGaz_YanRU/ig3kkPiIvrPIiptur1Y1wpzbSlU"',
'id': {'kind': 'youtube#video', 'videoId': '3TJP5a3pBME'},
'snippet': {'publishedAt': '2018-07-29T16:00:52.000Z',
'channelId': 'UCBINFWq52ShSgUFEoynfSwg',
'title': '20-Minute Victoria Sport Workout For Toned Abs and Legs',
'description': "Stay strong all Summer with Victoria Sport Ambassador Lindsey Harrod's 20-minute high-impact cardio workout. Tone your whole body through circuits including ...",
'thumbnails': {'default': {'url': 'https://i.ytimg.com/vi/3TJP5a3pBME/default.jpg',
'width': 120,
'height': 90},
'medium': {'url': 'https://i.ytimg.com/vi/3TJP5a3pBME/mqdefault.jpg',
'width': 320,
'height': 180},
'high': {'url': 'https://i.ytimg.com/vi/3TJP5a3pBME/hqdefault.jpg',
'width': 480,
'height': 360}},
'channelTitle': 'POPSUGAR Fitness',
'liveBroadcastContent': 'none'}}])

我需要该函数为每个请求返回50个视频,但是该函数不稳定(错误)或拒绝返回50个视频。

最佳答案

我有同样的错误,我认为这是由于http请求未更新而引起的,我每次调用请求的函数并每次都为我工作时,通过构建发现资源来解决它。

from googleapiclient.discovery import build
import time


def url_sinffer(search_word):
api_key = "Your Api key"
youtube = build('youtube', 'v3', developerKey=api_key)
print(type(youtube))
request = youtube.search().list(
part="id",
q=search_word,
maxResults=1,
type="music"
)
response = request.execute()
time.sleep(0.5)

video_id = response['items'][0]['id']['videoId']
url = f'https://www.youtube.com/watch?v={video_id}'
return url



关于python - Youtube API每个请求或错误返回1个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59770032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com