LoginSignup
2
1

More than 3 years have passed since last update.

YouTube Data API (v3) による YouTube 動画のコメント取得(Ruby版)

Last updated at Posted at 2020-04-12

はじめに

YouTube Data API v3 を使用してYouTube動画のコメントを取得します。

使用言語はRubyで、Ruby 用 Google API ライブラリ(google-api-ruby-client)のバージョンは0.37.3です。

事前準備

  • API キー
  • Ruby 用 Google API ライブラリ

APIキー取得

下記参照下さい。
Youtube Data APIを使用して、Youtube Liveのコメントを取得する - Apiキーの有効化

Ruby用Google APIライブラリインストール

$ gem install google-api-client

YouTube動画のコメント取得

基礎編

YouTube Video ID を指定して、対象動画のコメントを10件取得する例です。

ちなみにYouTube Video ID とは、http://www.youtube.com/watch?v=aN46pEO_jX8aN46pEO_jX8の部分です。

require 'google/apis/youtube_v3'

class YoutubeApi
    attr_reader :response

    def initialize api_key
        @youtube = Google::Apis::YoutubeV3::YouTubeService.new

        @youtube.key = api_key # APIキー設定
    end

    def comment_threads video_id
        @response = @youtube.list_comment_threads(
            'snippet', # part
            max_results: 10, # コメント取得件数(1 ~ 100で指定可)
            text_format: 'plainText', # 出力フォーマット(html / plainText 形式のいずれかを指定可)
            video_id: video_id)
    end
end

if $0 == __FILE__

    api_key = 'xxx' # APIキー
    youtube_api = YoutubeApi.new(api_key)

    video_id = 'xxx' # Video ID
    youtube_api.comment_threads(video_id)

    comment = youtube_api.response.items.first.snippet.top_level_comment # 最初のコメント

    p comment.snippet.author_display_name # ユーザ名
    p comment.snippet.text_display # コメント

end

応用編

YouTube Data API v3の制約上、一回のアクセスで取得可能な最大コメント数は100なので
コメントの多い動画(例えば、2000件のコメント投稿のある動画)だと、後ろのコメントが取得できません。

よって、次のステップとして
対象動画のコメントを最新日時順で100件以上取得する例を紹介します。

require 'google/apis/youtube_v3'

class YoutubeApi
    attr_reader :response

    def initialize api_key
        @youtube = Google::Apis::YoutubeV3::YouTubeService.new

        @youtube.key = api_key # APIキー設定
    end

    def comment_threads video_id, page_token = ''
        @response = @youtube.list_comment_threads(
            'snippet', # part
            max_results: 100, # コメント取得件数(1 ~ 100で指定可)
            order: 'time', # 取得コメントの並び順(time / relevance 形式のいずれかを指定可)
            page_token: page_token, 
            text_format: 'plainText', # 出力フォーマット(html / plainText 形式のいずれかを指定可)
            video_id: video_id)
    end
end

if $0 == __FILE__

    api_key = 'xxx' # APIキー
    youtube_api = YoutubeApi.new(api_key)

    next_page_token = ''

    video_id = 'xxx' # Video ID

    10.times do |i|
        youtube_api.comment_threads(video_id, next_page_token)

        p youtube_api.response

        next_page_token = youtube_api.response.next_page_token
    end

end

基礎編との差分

  • order使用:最新日時順で取得
  • page_token使用:コメント取得ページの指定

最後に

本プログラムを使って、コメント分析(言語・感情解析)をしていこうと思います。
次回記事も是非読んで下さい。

最後の最後に(更新追記分)

本記事では、APIキーを使った認証方法にてYouTube 動画のコメント取得を行いました。
只、ユーザーのプライベートデータにアクセスするには、GoogleはOAuth 2.0の使用が必須とのことなので、ご注意下さい。

動画のコメントはプライベートデータになるのでしょうか?
わかる方いればご教授願います。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1