I have a Discord Bot and I want to send a message whenever a certain user uploads a video. I have had a look at the Youtube API (https://developers.google.com/youtube/v3/docs/videos) but have not found how to do what I want.
-
4You can check in this [documentation](https://developers.google.com/youtube/v3/guides/push_notifications) on how to subscribe to Push notification. It is stated here that the YouTube Data API (v3) supports push notifications via [PubSubHubbub](https://github.com/pubsubhubbub/), a server-to-server publish/subscribe protocol for Web-accessible resources. Notifications are pushed out to subscribers via HTTP webhooks, which is much more efficient than polling-based solutions. – KENdi Apr 11 '17 at 16:41
1 Answers
Well ..I do it using checking for the video every 1 minute and check if the video link(or id) is not equal to the last video then post the video in that particular channel you want to post to.
I use google-api-python-client
Firstly pip install google-api-python-client
from from googleapiclient.discovery import build
In your on_ready function
@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
Making the tasks loop for checking the video
@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
So basically what Im doing is using a private channel to post the latest video of the bot as soon as it is ready because if by chance the bot goes offline in between and then comes online it posts the link to that channel then im making as tasks loop in which Im checking every minute that if the latest video link fof that youtube channel is not equal to the video link in my private channel that means the uploader has uploaded a video so post the video in the channel i wish to post.If its equal then do nothing i.e. pass
You can use a json file or a database if you are using instead of like I used channel to check for the video.It works fine.
- 21
- 2