Skip to content
Closed
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
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ NEWS_API_KEY=ThisIsSampleAPIKeyj985uYheRgw53IHuhs9OOindbrv6atResi

# Use in cook.rb
RAKUTEN_COOK_ID=ThisIsSampleSwimmyCookIDkfjgikdjrmkhfgc

# Use in spotify.rb
SPOTIFY_CLIENT_ID=ThisIsSampleSpotifyClientID
SPOTIFY_CLIENT_SECRET=ThisIsSampleSpotifyClientSecret
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'
47 changes: 47 additions & 0 deletions lib/swimmy/command/artist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Swimmy
module Command
class Artist < Swimmy::Command::Base
command "artist" do |client, data, match|
case match[:expression]
when nil
client.say(channel: data.channel, text: "引数の数が正しくありません.検索するアーティスト名を入力してください.")
when "help"
client.say(channel: data.channel, text: help_message("artist"))
else
begin
# SpotifyのAPIを使用するための認証
spotify = Swimmy::Service::Spotify.new(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_CLIENT_SECRET'])
artist = spotify.search(match[:expression])
rescue => e
client.say(channel: data.channel, text: "アーティスト情報を取得できませんでした.API認証に失敗した可能性があります.")
return
end

genres = artist.genres
tracks = artist.popular_tracks
related_artists = artist.related_artists

message = "アーティスト情報を取得中...\n"
message << "*#{match[:expression]}* の検索結果 ⇒ *#{artist.name}*\n"

message << "*[楽曲ジャンル]*\n"
genres.each { |genre| message << "・#{genre}\n"}

message << "*[人気曲]*\n"
tracks.each { |track| message << "・#{track}\n"}

message << "*[関連アーティスト]*\n"
related_artists.each { |related_artist| message << "・#{related_artist}\n"}

client.say(channel: data.channel, text: message)
end
end #command

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 @@ -20,5 +20,6 @@ module Resource
autoload :CoopShop, "#{dir}/coop_info.rb"
autoload :NomnichiArticle, "#{dir}/nomnichi_article.rb"
autoload :CookResource, "#{dir}/cook_resource.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
genrus = []
@genres.each { |genre| genrus << "#{genre}" }
return genrus
end

def popular_tracks
# popularityの値が大きい順にソート
sorted_tracks = @tracks.sort_by{|track| track.popularity}.reverse
tracks = []
sorted_tracks.each { |track| tracks << "#{track.name}" }
return tracks
end

def related_artists
related_artists = []
@related_artists.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 @@ -18,5 +18,6 @@ module Service
autoload :Coop, "#{dir}/coop.rb"
autoload :Nomnichi, "#{dir}/nomnichi.rb"
autoload :RecipeInfomation, "#{dir}/recipe_information.rb"
autoload :Spotify, "#{dir}/spotify.rb"
end
end
30 changes: 30 additions & 0 deletions lib/swimmy/service/spotify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Swimmy
module Service
class Spotify
require 'rspotify'

def initialize(client_id, client_secret)
# 結果を日本語で取得するための設定
ENV['ACCEPT_LANGUAGE'] = "ja"
# 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)
related_artists = []
for i in 0..2
# 検索結果中,2番目以降のアーティストを3件取り出す
related_artists << artists[i + 1]
end

return Swimmy::Resource::ArtistInfo.new(name, genres, tracks, related_artists)
end
end # class Spotify
end # module Service
end # module Swimmy