コンテンツ・ビジネスが熱いですね。中でも Youtube はダントツで人気 (中毒) !これを活用する手はないでしょう。Ruby の勉強がてら Youtube API v3 を触ってみました。まずは Google が提供しているサンプルで動かしましたが、これが行数多くてmndks、よって yt というライブラリを使うことにします。
今回は動画の検索のみです。
yt
https://github.com/Fullscreen/yt
API_KEY の設定
1 2 3 4 5 | @category = Category.find(params[:category_id]) Yt.configure do |config| config.api_key = 'API_KEY' config.log_level = :debug # debug で遊んでた end |
検索結果 100件の ID を JSON で出力する
1 2 3 4 5 6 7 | a = Array.new videos = Yt::Collections::Videos.new videos.where(q: 'KEYWORDS').first(100).each do |item| a << item.id end @json = {'ids' => a } render :json => @json |
JSON 出力結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | "ids": [ "Rn_AAM_cq44", "bF22ElI2PFs", "a6KFjvZNfYY", "EAueRiAWYp0", "4egU0Ejgess", "ZJ1Qpd5mtGc", "0spN7WaGbbI", "Fm4D6045St8", "IwXUWqxUM7A", "CNIKQKvUGaA", "nRZWGB89Y3g", // 以下略 ] |