通过PubSubHubbub实现YouTube订阅功能

详细资料参考官方的这篇文档

https://developers.google.com/youtube/v3/guides/push_notifications

 

第一步:

在下面这个网址中添加订阅频道和回调地址

https://pubsubhubbub.appspot.com/subscribe

在Subscribe/Unsubscribe项中填写Callback URL和Topic URL,其中Verify type和Mode使用默认的Asynchronous和Subscribe就行

也可以脚本实现:

#coding=utf-8
import requests

mode = 'subscribe'
callback = 'Your callback url'
hub = 'https://pubsubhubbub.appspot.com/subscribe'

req_data = {
            "hub.mode": mode,
            "hub.callback": callback,
            "hub.lease_seconds": 60*60*24*365,
            "hub.verify": "async",
            "hub.topic": "https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCeuOYybaSI9LCwvvjFvraqg",
        }

requests.packages.urllib3.disable_warnings()
try:
	response = requests.post(hub, data=req_data, verify=False, timeout=10)
	print(response)
except requests.exceptions.RequestException as e:
	print(e)

我使用的是脚本形式,执行后就会在服务器上接到消息

[pid: 12716|app: 0|req: 1/1] 100.120.97.93 () {46 vars in 1056 bytes} [Fri Apr 10 14:39:19 2020] GET /spider/youtube/subscribe?hub.topic=https://www.youtube.com/xml/feeds/videos.xml%3Fchannel_id%3DUCeuOYybaSI9LCwvvjFvraq&hub.challenge=3708291169963076134&hub.mode=subscribe&hub.lease_seconds=864000 => generated 19 bytes in 1 msecs (HTTP/1.1 200) 2 headers in 79 bytes (1 switches on core 0)
request.content_length: None
request.content_type:
request.data: b''
request.form: ImmutableMultiDict([])
request.headers: Remoteip: 66.102.8.110
Host: Your host
X-Forwarded-For: 66.102.8.110
Connection: close
Cache-Control: no-cache,max-age=0
Pragma: no-cache
Accept: */*
From: googlebot(at)googlebot.com
User-Agent: FeedFetcher-Google; (+http://www.google.com/feedfetcher.html)
Accept-Encoding: gzip,deflate,br

从请求上看这是一个get请求,我的接口响应是这样的:

    def get(self):
        print("request.content_length:", request.content_length)
        print("request.content_type:", request.content_type)
        print("request.data:", request.data)
        print("request.form:", request.form)
        print("request.headers:", request.headers)

        data = self.parser.parse_args()
        # 要访问的目标页面
        challenge = data.get('hub.challenge')


        #return challenge

        res = make_response(challenge)
        return res

当我在上面指定的频道账号上发布视频时收到了如下请求:

[pid: 13190|app: 0|req: 1/1] 100.120.97.103 () {52 vars in 956 bytes} [Fri Apr 10 15:54:52 2020] POST /spider/youtube/subscribe => generated 44 bytes in 1 msecs (HTTP/1.1 200) 4 headers in 196 bytes (1 switches on core 0)
request.content_length: 870
request.content_type: application/atom+xml
request.data: b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns="http://www.w3.org/2005/Atom"><link rel="hub" href="https://pubsubhubbub.appspot.com"/><link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCeuOYybaSI9LCwvvjFvraqg"/><title>YouTube video feed</title><updated>2020-04-10T07:54:51.196462987+00:00</updated><entry>\n  <id>yt:video:F9qsJlQfTqM</id>\n  <yt:videoId>F9qsJlQfTqM</yt:videoId>\n  <yt:channelId>UCeuOYybaSI9LCwvvjFvraqg</yt:channelId>\n  <title>\xe5\x92\x9a\xe5\xb7\xb4\xe6\x8b\x89</title>\n  <link rel="alternate" href="https://www.youtube.com/watch?v=F9qsJlQfTqM"/>\n  <author>\n   <name>qiuchen zhang</name>\n   <uri>https://www.youtube.com/channel/UCeuOYybaSI9LCwvvjFvraqg</uri>\n  </author>\n  <published>2020-04-10T07:54:43+00:00</published>\n  <updated>2020-04-10T07:54:51.196462987+00:00</updated>\n </entry></feed>\n'
request.form: ImmutableMultiDict([])
request.headers: Content-Type: application/atom+xml
Content-Length: 870
Remoteip: 66.102.8.110
Host: Your host
X-Forwarded-For: 66.102.8.110
Connection: close
Link: <https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCeuOYybaSI9LCwvvjFvraqg>; rel=self, <http://pubsubhubbub.appspot.com/>; rel=hub
Cache-Control: no-cache,max-age=0
Pragma: no-cache
Accept: */*
From: googlebot(at)googlebot.com
User-Agent: FeedFetcher-Google; (+http://www.google.com/feedfetcher.html)
Accept-Encoding: gzip,deflate,br

从请求上可以看出这是个post请求,request.data就是我们想要的数据

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值