diff --git a/.env.sample b/.env.sample index 52b8dc5..d528a31 100644 --- a/.env.sample +++ b/.env.sample @@ -24,5 +24,11 @@ NEWS_API_KEY=ThisIsSampleAPIKeyj985uYheRgw53IHuhs9OOindbrv6atResi # Use in cook.rb RAKUTEN_COOK_ID=ThisIsSampleSwimmyCookIDkfjgikdjrmkhfgc +<<<<<<< HEAD # Use in translate.rb TRANSLATE_API_URL=ThisIsSampleSwimmyHizukiTranslateGAS +======= +# Use in spotify.rb +SPOTIFY_CLIENT_ID=ThisIsSampleSpotifyClientID +SPOTIFY_CLIENT_SECRET=ThisIsSampleSpotifyClientSecret +>>>>>>> be4210c (Add artist command) diff --git a/Gemfile b/Gemfile index e859418..724c9de 100644 --- a/Gemfile +++ b/Gemfile @@ -17,3 +17,4 @@ gem 'enumdate' gem 'mqtt' gem 'news-api' gem 'nokogiri' +gem 'rspotify' diff --git a/lib/swimmy/command/artist.rb b/lib/swimmy/command/artist.rb new file mode 100644 index 0000000..546ca38 --- /dev/null +++ b/lib/swimmy/command/artist.rb @@ -0,0 +1,50 @@ +module Swimmy + module Command + class Artist < Swimmy::Command::Base + command "artist" do |client, data, match| + case match[:expression] + when nil + msg = "引数の数が正しくありません.検索するアーティスト名を入力してください." + when "help" + msg = help_message("artist") + else + begin + spotify = Swimmy::Service::Spotify.new(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET']) + artist_info = spotify.search(match[:expression]) + rescue => e + msg = "アーティスト情報を取得できませんでした.API認証に失敗した可能性があります." + end + + # 人気曲は最大10件,関連アーティストは最大5件 + num_tracks = 10 + num_related_artists = 5 + msg = make_artist_message(artist_info, num_tracks, num_related_artists) + end + + client.say(channel: data.channel, text: msg) + end #command + + def self.make_artist_message(artist_info, num_tracks, num_related_artists) + msg = "" + msg << "検索結果: *#{artist_info.name}*\n" + + msg << "*[楽曲ジャンル]*\n" + artist_info.genres.each { |genre| msg << "・#{genre}\n" } + + msg << "*[人気曲]*\n" + artist_info.popular_tracks(num_tracks).each { |track| msg << "・#{track}\n" } + + msg << "*[関連アーティスト]*\n" + artist_info.related_artists(num_related_artists).each { |related_artist| msg << "・#{related_artist}\n" } + + return msg + end + + help do + title "artist" + desc "アーティストに関連する情報を表示します" + long_desc "artist - アーティストの楽曲ジャンル,人気楽曲,関連アーティストを表示します" + end #help + end #class Coop + end #module Command +end #module Swimmy diff --git a/lib/swimmy/resource.rb b/lib/swimmy/resource.rb index ea970cc..994c4d5 100644 --- a/lib/swimmy/resource.rb +++ b/lib/swimmy/resource.rb @@ -22,5 +22,6 @@ module Resource autoload :CookResource, "#{dir}/cook_resource.rb" autoload :BookmarkEntry, "#{dir}/bookmark.rb" autoload :CalendarEvent, "#{dir}/calendar_event.rb" + autoload :ArtistInfo , "#{dir}/artist_info.rb" end end diff --git a/lib/swimmy/resource/artist_info.rb b/lib/swimmy/resource/artist_info.rb new file mode 100644 index 0000000..10448e8 --- /dev/null +++ b/lib/swimmy/resource/artist_info.rb @@ -0,0 +1,36 @@ +module Swimmy + module Resource + class ArtistInfo + def initialize(name, genres, tracks, related_artists) + @name = name + @genres = genres + @tracks = tracks + @related_artists = related_artists + end + + def name + return @name + end + + def genres + return @genres + end + + def popular_tracks(num) + # popularityの値が大きい順にソート + sorted_tracks = @tracks.sort_by{|track| track.popularity}.reverse + tracks = [] + sorted_tracks[0, num].each { |track| tracks << "#{track.name}" } + return tracks + end + + def related_artists(num) + related_artists = [] + # 先頭は検索結果のアーティストのため除外 + @related_artists[1, num].each { |related_artist| related_artists << "#{related_artist.name}" } + return related_artists + end + + end + end +end # module Swimmy diff --git a/lib/swimmy/service.rb b/lib/swimmy/service.rb index aac5a63..41e6e79 100644 --- a/lib/swimmy/service.rb +++ b/lib/swimmy/service.rb @@ -23,5 +23,6 @@ module Service autoload :Translate, "#{dir}/translate.rb" autoload :Numbersapi, "#{dir}/numbersapi.rb" autoload :GoogleCalendar, "#{dir}/schedule_service.rb" + autoload :Spotify, "#{dir}/spotify.rb" end end diff --git a/lib/swimmy/service/spotify.rb b/lib/swimmy/service/spotify.rb new file mode 100644 index 0000000..8367963 --- /dev/null +++ b/lib/swimmy/service/spotify.rb @@ -0,0 +1,23 @@ +module Swimmy + module Service + class Spotify + require 'rspotify' + + def initialize(client_id, client_secret) + # Spotify-APIの認証 + RSpotify.authenticate(client_id, client_secret) + end + + def search(artist_name) + artists = RSpotify::Artist.search(artist_name) + # 先頭要素のアーティストを検索結果とする + artist = artists.first + name = artist.name + genres = artist.genres + tracks = artist.top_tracks(:JP) + + return Swimmy::Resource::ArtistInfo.new(name, genres, tracks, artists) + end + end # class Spotify + end # module Service +end # module Swimmy