【问题标题】:Notified when Youtube video is uploaded APIYoutube 视频上传时通知 API
【发布时间】:2017-03-30 12:37:34
【问题描述】:
【问题讨论】:
-
您可以在documentation 中查看如何订阅推送通知。这里声明 YouTube 数据 API (v3) 通过PubSubHubbub 支持推送通知,这是一种用于 Web 可访问资源的服务器到服务器发布/订阅协议。通知通过 HTTP webhook 推送给订阅者,这比基于轮询的解决方案更有效。
标签:
youtube
youtube-api
youtube-data-api
youtube-javascript-api
【解决方案1】:
嗯 ..我每 1 分钟检查一次视频并检查视频链接(或 ID)是否不等于最后一个视频,然后将视频发布到您要发布到的特定频道中。
我用google-api-python-client
首先pip install google-api-python-client
from from googleapiclient.discovery import build
在你的 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
使任务循环以检查视频
@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
所以基本上我所做的是使用私人频道在机器人准备好后立即发布最新视频,因为如果机器人偶然在两者之间脱机然后上线,它会发布指向该频道的链接,然后我作为任务循环,我每分钟检查一次,如果该 youtube 频道的最新视频链接不等于我的私人频道中的视频链接,这意味着上传者已经上传了视频,所以将视频发布到我希望发布的频道中.如果相等则什么也不做,即pass
如果您使用的是 json 文件或数据库,而不是像我使用频道来检查视频,则可以使用它。它工作正常。