Youtube 视频上传时通知 API

5 youtube youtube-api youtube-javascript-api youtube-data-api

我有一个 Discord 机器人,我想在某个用户上传视频时发送一条消息。我查看了 Youtube API ( https://developers.google.com/youtube/v3/docs/videos ),但没有找到如何做我想做的事情。

小智 2

好吧..我每 1 分钟检查一次视频,并检查视频链接(或 id)是否不等于最后一个视频,然后将视频发布到您想要发布的特定频道中。google-api-python-client 我首先使用pip install google-api-python-client

from from googleapiclient.discovery import build
Run Code Online (Sandbox Code Playgroud)

在你的 on_ready 函数中

@client.event
async def on_ready():

    youtube=build('youtube','v3',developerKey='Enter your key here')
    req=youtube.playlistItems().list(
       playlistId='The Playlist id of the channel you want to post video i.e. the id of video playlist of the channel',
       part='snippet',
       maxResults=1
    )
    res=req.execute()
    vedioid=res['items'][0]['snippet']['resourceId']['videoId']
    link="https://www.youtube.com/watch?v="+vedioid
    ch=await client.fetch_channel(the  channel id from where im checking for the new video)
    await ch.send(link)


    yt.start()#Starting tasks loop which is made below for checking every minute if the new video is equal or unequal to old video link
Run Code Online (Sandbox Code Playgroud)

使任务循环以检查视频

@tasks.loop(seconds=60)
async def yt():      
     youtube=build('youtube','v3',developerKey='Enter your key here')
     req=youtube.playlistItems().list(
         playlistId='The Playlist id of the channel you want to post video i.e. the id of video playlist of the channel',
         part='snippet',
         maxResults=1
     )
     res=req.execute()
     vedioid=res['items'][0]['snippet']['resourceId']['videoId']
     link="https://www.youtube.com/watch?v="+vedioid
     ch=await client.fetch_channel(Fetching same channel from which you are checking for the video link)

     async for message in ch.history(limit=1):#looping through the channel to get  the latest message i can do this using last message also but I prefer using channel.history        
     if str(link) != str(message.content):
          ch2=await client.fetch_channel(the channel you want to post video to)
        
          await ch2.send(f'@everyone,**User** just posted a vedio!Go and check it out!\n{link}')
      
          await ch.send(link2)#this is important as after posting the video the link must also be posted to the check channel so that the bot do not send other link
     else:
          pass
Run Code Online (Sandbox Code Playgroud)

所以基本上我所做的就是使用私人频道在准备好后立即发布机器人的最新视频,因为如果机器人在中间离线然后上线,它会发布到该频道的链接,然后我将其作为任务循环中,我每分钟检查一次,如果该 YouTube 频道的最新视频链接不等于我的私人频道中的视频链接,则意味着上传者已上传视频,因此将视频发布到我希望发布的频道中。如果是等于则不执行任何操作,即pass 如果您正在使用而不是像我使用通道来检查视频,则可以使用 json 文件或数据库。它工作正常。