api-samples icon indicating copy to clipboard operation
api-samples copied to clipboard

Please provide an up-to-date example of PubSubhubbub

Open sluongng opened this issue 7 years ago • 40 comments

According to https://developers.google.com/youtube/v3/guides/push_notifications you guys do support it. But most of the code I can find around github or tutorial online are widely outdated.

Plus the configuration is a mix-bag between setting up environment for callback support to working with the web interface on https://pubsubhubbub.appspot.com/subscribe

Many library also claim that their implementation are not compatitble with latest specification.

Please help provide a working tutorial/example code. Preferable in GO/Python or NodeJS. Also if there is a reliable way of testing this code, instead of having to wait around for a channel to upload a video, it would be very nice.

Thanks!

sluongng avatar Mar 18 '18 17:03 sluongng

Nothing changed after year. That's sad.

MrBoombastic avatar Aug 16 '19 15:08 MrBoombastic

@sluongng, @MrBoombastic Hi guys I had the same problem for the past several weeks and I guess I was able to fix it locally, so wanted to share my approach. To begin with I also share the same frustration towards the lack of documentation.

I tried two approaches - NodeJS (via koa2) didn't worked (wasn't able to read the body correctly) and pythonic very simple quite straightforwad approach, which worked (at least locally).

So the steps are:

1. Create a new flask app

pip install flask
touch app.py

app.py:

from flask import Flask
from flask import request

app = Flask(__name__)

# irrelevant to the question method, just for testing purposes
@app.route("/", methods=['GET', 'POST'])
def ping():
    return 'pong'


@app.route("/feed", methods=['GET', 'POST'])
def feed():
    challenge = request.args.get('hub.challenge')

    if (challenge):
        return challenge

    print(request.data)  # binary literal with xml payload

    return '', 204
flask run 

2. Create a ngrok tunnel (https://ngrok.com/docs)

For me it looked something like that image

3. Make a subscription in https://pubsubhubbub.appspot.com/subscribe

image

https://www.youtube.com/xml/feeds/videos.xml?channel_id={{CHANNEL_ID}} where {{CHANNEL_ID}} is YouTube channel id

4. Test subscription

image

5. Analyze report

image

  • if you're returning hub.challenge then server authorizes you for 5 days as it sends another checks from time to time
  • if your service return 204 for POST then youtube server will mark it as successful receive

6. Debugging

  • as mentioned above you can always subscribe to a big channel (e.g. CNN) and wait a day or two to generate 5-10 requests and debug it, but it is quite slow. In my case I just used a simple quite short (5-10 seconds) video and uploaded it multiple times to my personal channel. It is not simultaneous, yet way faster then waiting 6-12 hours for somebody. It was 5-10 minutes while video has been processed, etc.

7. Further improvements

  • subscription part are simple POST requests so this can be automized as well (for multiple channels)
  • there are some node-js implementation for PSHB subscriptions, but they are not documented as well

whydinkov avatar Dec 02 '19 20:12 whydinkov

Good job, but PSHB should be simple. Now scraping XML for channel is faster and easier than this. Anyway, thank you!

MrBoombastic avatar Dec 02 '19 20:12 MrBoombastic

@MrBoombastic what do you mean by

scraping xml for channel

whydinkov avatar Dec 02 '19 21:12 whydinkov

Each channel has xml file with 10 latest videos.

MrBoombastic avatar Dec 02 '19 21:12 MrBoombastic

Gotcha! You mean this RSS feed https://www.youtube.com/feeds/videos.xml?channel_id= which is still limiting, but yeah you're right - it's easier than figuring out how a 15 years old technology works without any decent documentation/example.

whydinkov avatar Dec 02 '19 22:12 whydinkov

@yoandinkov I tried to do this in Express.js but there is nothing return from the req or res object.

shan1iu avatar Mar 29 '20 10:03 shan1iu

@shan1iu can you share your project/give more information what have you done and what haven't worked exactly. Also do you get the right results from step 4 (testing the subscription)?

whydinkov avatar Mar 30 '20 07:03 whydinkov

What limits does PubSubhubbub have? How many channels can I listen to using it?

mmkhitaryan avatar Jun 05 '20 20:06 mmkhitaryan

i creating api in python to deploy in heroku and use the pubsubhubub

Ulusamay avatar Jun 28 '20 23:06 Ulusamay

@mmkhitaryan I'd like to know that also - I asked a question at StackOverflow but no answer yet: https://stackoverflow.com/questions/63726197/is-there-usage-limit-for-youtube-push-notifications

malaire avatar Sep 03 '20 18:09 malaire

Hey there! I am currently having problems determining whether a pubsubhubbub notification that i recieved is a new upload or just a title/description change. Can anybody help me with that? I tried comparing the published and updated value but both are never same 😖

AXVin avatar Sep 19 '20 06:09 AXVin

@malaire - what's your count that you're trying it with?

whydinkov avatar Sep 20 '20 21:09 whydinkov

@AXVin - actually it's hard to say, I guess, I do use following approach:

if a video's release date was less than an hour in the time the event appears I treat it as "new video" and store it in my database. in my database, I have a unique constraint on video_id to avoid duplicates

whydinkov avatar Sep 20 '20 21:09 whydinkov

@yoandinkov I have been testing with just 2 subscriptions for now, but YouTube push notifications are not working at all - I have received ZERO notifications while both channels have uploaded videos. I'm now waiting for YouTube to fix this bug before I start testing with more subscriptions.

(Subscriber Diagnostics confirms that PubSubHubbub has received nothing from YouTube, so it's YouTube bug, not PubSubHubbub bug.)

malaire avatar Sep 21 '20 10:09 malaire

@malaire Do you have the GET method handled for the callback? You must return the hub.challenge query param inside that otherwise it will not be verified correctly and you will never recieve any updates ever. (Because, i do get the notifications)

AXVin avatar Sep 21 '20 15:09 AXVin

@AXVin My code is working correctly. Subscriber Diagnostics says "State: verified", and no errors of any kind, but "Content received: n/a" and "Content delivered: n/a".

And manually checking the Topic URL confirms that uploads are happening so there should be push notifications.

malaire avatar Sep 21 '20 15:09 malaire

@malaire I can confirm that this service works correctly for me. My suggestion is either upload your own video in your own channel (so you know exactly what and when is being uploaded) or use some big channel (as CNN for example).

Another suggestion is to refer to my previous post (from 02.12.2019) especially point 5. What about other properties (except State) do you have timestamps for all others as well (like subscription, etc.)?

whydinkov avatar Sep 24 '20 13:09 whydinkov

@yoandinkov I did test with my own channel where I uploaded video - no notification. And timestamps were correct also.

I'm not interested in big channels but in small channels.

I made issue here (without timestamps as I didn't remember them anymore, but I can add those once I finish currently ongoing 10-day test without any notifications): https://issuetracker.google.com/issues/169067950

malaire avatar Sep 24 '20 13:09 malaire

I also tried doing this for live broadcasts. I can confirm the live broadcast shows up in the XML videos feed but the pubsubhubbub just doesn't seem to work. I did a similar flow for Twitch webhooks and it worked fine so I'm not sure where the disconnect is. There's seemingly no way to debug things programmatically so it just "doesn't work" from my end.

JMTK avatar Oct 15 '20 18:10 JMTK

@AXVin - actually it's hard to say, I guess, I do use the following approach:

if a video's release date was less than an hour in the time the event appears I treat it as "new video" and store it in my database. in my database, I have a unique constraint on video_id to avoid duplicates

I created flask API using your snippet it displays get request on terminal and black page on pubsubhubbub

127.0.0.1 - - [21/Nov/2020 10:46:19] "[37mGET /feed?hub.topic=https://www.youtube.com/xml/feeds/videos.xml%3Fchannel_id%3DUCqD5M4_-cpDOSGiKq3iyGHg&hub.challenge=10170485706286032479&hub.mode=subscribe&hub.lease_seconds=432000 HTTP/1.1[0m" 204 -

and when i try to diagnose it displays these details

image

Any idea why this is soo?

hanzlahabib avatar Nov 21 '20 05:11 hanzlahabib

Here's how I got it to work with NodeJS:

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
require('body-parser-xml')(bodyParser);

app.get('/test', ({ query: { 'hub.challenge': challenge } }, res) => {
  console.log(challenge);
  res.status(200).end(challenge)
});

app.post(`/test`, bodyParser.xml(), ({ body: {feed} }, res) => {
  console.log(feed);
  res.status(204).end();
});

app.listen(1234);

PSHB sends a GET method with a few queries, the one you're interested in is hub.challenge. Response back with that code (sub/unsub).

POST method is used to send the XML. I use body-parser-xml but you can use anything for this.

pham avatar Jan 11 '21 21:01 pham

Hi guys, thanks for the solution! Sorry, this question might be stupid, so please bear with me — I'm really new to YouTube APIs.
As far as I can see, you get the activities per channel. But what I'm really missing is the email notifications about new videos I'm subscribed to. I mean, before August 13th 2020 you could adjust it so that when a new video is uploaded to any channel you're subscribed to, you get an email notification.
Could PSHB help me to accomplish that task? Thanks.

Menelion avatar Mar 14 '21 21:03 Menelion

@Menelion In theory - yes, you can, if you have your own mailing service etc. In practice - PSHB is really piece of garbage to test and handle. As I said about year ago in this thread, it's easier to use XML endpoint than trying to set up PubSub. Maybe you will have more luck?

MrBoombastic avatar Mar 14 '21 21:03 MrBoombastic

@MrBoombastic Thanks for your reply! However, I don't see any way of getting the single feed for all of my subscriptions in XML, either. See this, for example. Any ideas?

Menelion avatar Mar 14 '21 23:03 Menelion

@Menelion As somebody said in the answer in your linked question, you can manually export all your subscribed channels here: https://www.youtube.com/subscription_manager, convert downloaded XML to something easy to work with like JSON and ask Youtube's XML endpoint about each of channels listed in your JSON file. Yeah, if you have, for example, 200 channels subscribed, you have to make 200 requests to YouTube. But don't worry - it's not very limited, so you shouldn't expect Captchas or other blocks.

MrBoombastic avatar Mar 14 '21 23:03 MrBoombastic

Hey there! I am currently having problems determining whether a pubsubhubbub notification that i recieved is a new upload or just a title/description change. Can anybody help me with that? I tried comparing the published and updated value but both are never same 😖

@AXVin It seems this feature of poorly documented, so I created an issue on their official issue tracker.

Edit: Google has already given this issue to an assignee.

andrei-polukhin avatar Apr 10 '21 06:04 andrei-polukhin

I made it work with Firebase Cloud Functions. Here is the github repo you can refer to: https://github.com/zainulhassan815/youtube-pubsub-firebase

The cloud function verifies and also handles incoming feed. When it recieves data, it gets the updated video id, make a request to youtube api to get updated data and then stores it in firestore.

zainulhassan815 avatar Jan 23 '22 16:01 zainulhassan815

@AXVin - actually it's hard to say, I guess, I do use the following approach: if a video's release date was less than an hour in the time the event appears I treat it as "new video" and store it in my database. in my database, I have a unique constraint on video_id to avoid duplicates

I created flask API using your snippet it displays get request on terminal and black page on pubsubhubbub

127.0.0.1 - - [21/Nov/2020 10:46:19] "�[37mGET /feed?hub.topic=https://www.youtube.com/xml/feeds/videos.xml%3Fchannel_id%3DUCqD5M4_-cpDOSGiKq3iyGHg&hub.challenge=10170485706286032479&hub.mode=subscribe&hub.lease_seconds=432000 HTTP/1.1�[0m" 204 -

and when i try to diagnose it displays these details

image

Any idea why this is soo?

@hanzlahabib can you fix your problem? I have the same problem, don't receive the notification (post) from pubsub

CarlosMunozT avatar May 05 '22 17:05 CarlosMunozT

I have save problem. Is Topic url https://www.youtube.com/feeds/videos.xml or https://www.youtube.com/xml/feeds/videos.xml? image

tree1891 avatar Jun 02 '23 05:06 tree1891