def get_ytvideos(query, ilogger):
"""
Gets either a list of videos from a playlist or a single video, using the
first result of a YouTube search
Args:
query (str): The YouTube search query
ilogger (logging.logger): The logger to log API calls to
Returns:
queue (list): The items obtained from the YouTube search
"""
queue = []
# Search YouTube
search_result = ytdiscoveryapi.search().list(
q=query,
part="id,snippet",
maxResults=1,
type="video,playlist"
).execute()
if not search_result["items"]:
return []
# Get video/playlist title
title = search_result["items"][0]["snippet"]["title"]
ilogger.info("Queueing {}".format(title))
# Queue video if video
if search_result["items"][0]["id"]["kind"] == "youtube#video":
# Get ID of video
videoid = search_result["items"][0]["id"]["videoId"]
# Append video to queue
queue.append(["https://www.youtube.com/watch?v={}".format(videoid), title])
# Queue playlist if playlist
elif search_result["items"][0]["id"]["kind"] == "youtube#playlist":
queue = get_queue_from_playlist(search_result["items"][0]["id"]["playlistId"])
return queue
评论列表
文章目录