api_music.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:modis 作者: Infraxion 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号