Youtube公式サイトをいじり、Rubyで動画情報の取得を試みています。
以下がコードです。
Ruby
1class Youtube < ApplicationRecord 2 3 require 'rubygems' 4 gem 'google-api-client', '>0.7' 5 require 'google/api_client' 6 require 'trollop' 7 8 DEVELOPER_KEY = ######################### 9 YOUTUBE_API_SERVICE_NAME = 'youtube' 10 YOUTUBE_API_VERSION = 'v3' 11 12 def self.get_service 13 client = Google::APIClient.new( 14 :key => DEVELOPER_KEY, 15 :authorization => nil, 16 :application_name => $PROGRAM_NAME, 17 :application_version => '1.0.0' 18 ) 19 youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION) 20 21 return client, youtube 22 end 23 24 def self.main 25 opts = Trollop::options do 26 opt :q, 'Search term', :type => String, :default => 'アウディ' 27 opt :max_results, 'Max results', :type => :int, :default => 5 28 end 29 30 client, youtube = get_service 31 32 begin 33 # Call the search.list method to retrieve results matching the specified 34 # query term. 35 search_response = client.execute!( 36 :api_method => youtube.search.list, 37 :parameters => { 38 :part => 'snippet', 39 :q => opts[:q], 40 :maxResults => opts[:max_results] 41 } 42 ) 43 44 urls = [] 45 titles = [] 46 descriptions = [] 47 channels = [] 48 thumbnails = [] 49 views = [] 50 51 # Add each result to the appropriate list, and then display the lists of 52 # matching videos, channels, and playlists. 53 search_response.data.items.each do |search_result| 54 case search_result.id.kind 55 when 'youtube#video' 56 urls << "http://www.youtube.com/watch?v=#{search_result.id.videoId}" 57 titles << "#{search_result.snippet.title}" 58 descriptions << "#{search_result.snippet.description}" 59 channels << "#{search_result.snippet.channelTitle}" 60 thumbnails << "#{search_result.snippet.thumbnails.default.url}" 61 views << "#{search_result.statistics.viewCount}" 62 end 63 end 64 65 puts "Urls:\n", urls, "\n" 66 puts "Titles:\n", titles, "\n" 67 puts "Descriotions:\n", descriptions, "\n" 68 puts "Channels:\n", channels, "\n" 69 puts "Thumbnials:\n", thumbnails, "\n" 70 puts "Views:\n", views, "\n" 71 72 rescue Google::APIClient::TransmissionError => e 73 puts e.result.body 74 end 75 76 end 77 78end
視聴回数を取得するために、下記の:partの部分を 「:part => 'snippet, statistics'」としているのですが、エラーが出ます。
search_response = client.execute!( :api_method => youtube.search.list, :parameters => { :part => 'snippet', :q => opts[:q], :maxResults => opts[:max_results] } )
####エラー画面
{ "error": { "errors": [ { "domain": "youtube.part", "reason": "unknownPart", "message": "statistics", "locationType": "parameter", "location": "part" } ], "code": 400, "message": "statistics" } }
statisticsは視聴回数を取得するためのプロパティだと認識しています。
どこが間違っているのでしょうか。ご教授お願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/05 07:52