通過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就是我們想要的數據

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章