From 75182110372223499e695c5251c87ffe0500e8b7 Mon Sep 17 00:00:00 2001 From: teraoka-h Date: Thu, 3 Apr 2025 14:33:07 +0900 Subject: [PATCH] Add artist command --- .env.sample | 4 +++ Gemfile | 1 + lib/swimmy/command/artist.rb | 47 ++++++++++++++++++++++++++++++ lib/swimmy/resource.rb | 1 + lib/swimmy/resource/artist_info.rb | 36 +++++++++++++++++++++++ lib/swimmy/service.rb | 1 + lib/swimmy/service/spotify.rb | 30 +++++++++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 lib/swimmy/command/artist.rb create mode 100644 lib/swimmy/resource/artist_info.rb create mode 100644 lib/swimmy/service/spotify.rb diff --git a/.env.sample b/.env.sample index 33f9593..62b6a37 100644 --- a/.env.sample +++ b/.env.sample @@ -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 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..92926c1 --- /dev/null +++ b/lib/swimmy/command/artist.rb @@ -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 - アーティストの楽曲ジャンル,人気楽曲,関連アーティストを表示します" + end #help + end #class Coop + end #module Command +end #module Swimmy diff --git a/lib/swimmy/resource.rb b/lib/swimmy/resource.rb index ff2b8db..e1ccdb3 100644 --- a/lib/swimmy/resource.rb +++ b/lib/swimmy/resource.rb @@ -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 diff --git a/lib/swimmy/resource/artist_info.rb b/lib/swimmy/resource/artist_info.rb new file mode 100644 index 0000000..08ffee7 --- /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 + 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 diff --git a/lib/swimmy/service.rb b/lib/swimmy/service.rb index 20ba7ac..eff5239 100644 --- a/lib/swimmy/service.rb +++ b/lib/swimmy/service.rb @@ -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 diff --git a/lib/swimmy/service/spotify.rb b/lib/swimmy/service/spotify.rb new file mode 100644 index 0000000..05d72b8 --- /dev/null +++ b/lib/swimmy/service/spotify.rb @@ -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