Messenger chatbot send youtube video 失敗心得

Messenger chatbot send youtube video 失敗心得




因為專題想要開發一個bot可以推播特定的youtube影片給使用者,
然而youtube影片格式無法直接使用messenger bot 的 video api,api只吃類似.mp4格式的網址,
所以若使用者想在messenger看youtube影片,必須得不停的切換messenger and brower視窗,
對使用者體驗來說相當不便
所以運用了神通廣大的google大神,仍然宣告失敗,然而還是學到許多事情,所以記錄下來

youtube api 教學

youtube api還蠻簡單的,輕鬆使用get方法可以拿到頻道資訊 播放清單資訊 影片資訊等等

這是channel的 http request,可以抓取頻道資訊 要將{}改成自己的參數
https://www.googleapis.com/youtube/v3/channelSections?part=contentDetails&channelId={}&key={}

這是playlist的 http request,可以抓取播放清單資訊 要將{}改成自己的參數
https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails,snippet&maxResults=50&playlistId={}&key={}

以下code為對某頻道做search並抓去該頻道播放清單的video_id

import requests import json import sys def main(argv): key = "your api key" cat_channel_id = "UC9egiwuJsQZ0Cy2to5fvSIQ" # cat channel id jsondata = requests.get( "https://www.googleapis.com/youtube/v3/channelSections?part=contentDetails&channelId={}&key={}".format(cat_channel_id, key)) # request channel information data = json.loads(jsondata.text) list_playlist = data['items'] # get playlists count = 0 for playlist in list_playlist: count = count + 1 if 'contentDetails' in playlist and 'playlists' in playlist['contentDetails']: playlist_id = playlist['contentDetails']['playlists'][0] print playlist_id videos_json = requests.get( 'https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails,snippet&maxResults=50&playlistId={}&key={}'.format(playlist_id, key)) # request playlist information videos_data = json.loads(videos_json.text) print videos_data videos = videos_data['items'] for video in videos: if 'contentDetails' in video: video_id = video['contentDetails']['videoId'] print "video_id={}".format(video_id) #print video id print count if __name__ == '__main__': main(sys.argv[1:])

youtube dl in python

再來採用youtube dl 套件,想辦法獲取video的真實url,讓api可以傳送video

import youtube_dl ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'}) with ydl: result = ydl.extract_info( 'http://www.youtube.com/watch?v=ET0rV79uFDQ', download=False # We just want to extract the info ) if 'entries' in result: # Can be a playlist or a list of videos video = result['entries'][0] else: # Just a video video = result print(video) video_url = video['url'] print(video_url)

很好,我拿到網址了,

https://r6—sn-u5oxu-uooe.googlevideo.com/videoplayback?ratebypass=yes&itag=22&requiressl=yes&expire=1495442327&mime=video%2Fmp4&key=yt6&sparams=dur%2Cei%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&ip=140.116.1.136&lmt=1492925516230696&dur=527.046&mt=1495420642&mv=m&ms=au&source=youtube&signature=DAD0CEE5063DE3B61E892B6A23EC6BAD16E124F0.C78C5E8FA97EA53668EBB9604976BD939829FAF9&initcwndbps=4775000&mm=31&mn=sn-u5oxu-uooe&id=o-AKRm_kSrfIkSvTcaOtsXmUPBRhb-JW2xHJPhQFHF3YDA&upn=zv89kP3EApo&ipbits=0&ei=Nk8iWfzoPJGb4AKty5ioBA&pl=19

但是這個網址fb仍然不吃… :cry:

結論:失敗,待解決

目前仍然想不到在messenger內直接看youtube影片的方法

目前最好的方式是採用general template,圖片用youtube api 抓到的縮圖

下方建button來讓使用者點擊進入youtu
be

  •  
張書維 張書維 Author

總網頁瀏覽量

Popular Posts