0
0

More than 3 years have passed since last update.

大好きなSCANDALのLINEbotを作りたいの巻1

Posted at

作りたいbot

毎日決まった時間にランダムでSCANDALのMVを送るbot
※SCANDALは女性バンドです。

そのための僕の頭の中の構想(railsで作成)

  1. まずはWEB上で動く物を作る
  2. それをLINEbotにする
  3. そこから機能を追加していく(例えば、ACIDMANとLINEで送るとACIDMANの曲をランダムに返すなど) ※ACIDMANもバンドです。

ひとまずできたこと

  1. まずはWEB上で動く物を作る

参考にした記事

Qiitaの記事を元にYouTube Data APIのリファレンスを参考にしました。

Qiita
https://qiita.com/sakakinn/items/46c0d4945e4646f346f6

YouTube Data API
https://developers.google.com/youtube/v3/docs?hl=ja

コード

index.html.erb
<% number = 0 %>
<% ran = rand(1..11) %>
<% @youtube_data.items.each do |item| %>
  <% number = number + 1 %>
  <% if number == ran %>
   <% snippet = item.snippet %>
   <p><%= snippet.title %></p>
   <p><%= snippet.published_at %><%= snippet.channel_title %></p>
   <div><iframe width="560" height="315" src="https://www.youtube.com/embed/<%= item.id.video_id %>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
  <% end %>
<% end %>
youtube_controller.rb
class YoutubeController < ApplicationController
  def find_videos(keyword)
    service = Google::Apis::YoutubeV3::YouTubeService.new
    service.key = ENV["YOUTUBEKEY"]

    next_page_token = nil
    opt = {
      q: keyword,
      type: 'video',
      channel_id: 'UCSNX8VGaawLFG_bAZuMyQ3Q',
      max_results: 11,
      order: :date,
      page_token: next_page_token
    }
    service.list_searches(:snippet, opt)
  end

  def index
    @youtube_data = find_videos('SCANDAL')
  end
end

出力結果↓
https://twitter.com/pompom06yutoz/status/1273869183491559425?s=21

今わかっている課題

1. Viewで11個のデータにアクセスしているので、それを無くしてランダムにチャンネルからデータを一つだけ取り出したい。(助けてくれ〜ww)
2. 今のままだとチャンネルが固定(いったんスルーで)

次にすること

とりあえずこれをLINEbotにしてみて、課題が浮上したらその時に考える。

最後に

プログラミング学習始めて約4ヶ月が経過し、今程よく楽しんでいる人間です。
もし、何か良い代替の案があったら気軽にコメントお願いします〜。
後、SCANDALが好きな人もぜひ。
つづく

0
0
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
0
0