Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ gem 'enumdate'
gem 'mqtt'
gem 'news-api'
gem 'nokogiri'
gem 'rspotify'
50 changes: 50 additions & 0 deletions lib/swimmy/command/artist.rb
Original file line number Diff line number Diff line change
@@ -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 <artist name> - アーティストの楽曲ジャンル,人気楽曲,関連アーティストを表示します"
end #help
end #class Coop
end #module Command
end #module Swimmy
1 change: 1 addition & 0 deletions lib/swimmy/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions lib/swimmy/resource/artist_info.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/swimmy/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions lib/swimmy/service/spotify.rb
Original file line number Diff line number Diff line change
@@ -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