1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Ruby】Discogs::Wrapper でアーティスト情報とかもろもろを取得する

Last updated at Posted at 2019-12-11

discogsでアーティスト名、ジャンル、リリースされたトラックの情報を取得したい。
discogs APIのwrapperを利用して適宜ロジック等を記載していく。

ドキュメント等

discogsについて
https://ja.wikipedia.org/wiki/Discogs

公式
https://www.discogs.com/ja/

gem
https://github.com/buntine/discogs

アクセストークンの取得

wrapperを使う前にアクセストークンを取得する。

googleアカウントでdiscogsにサインアップ後、User Tokenを取得する。個人的に利用する分には当面はUserTokenで良さそう。

手順

  1. https://www.discogs.com/developers/index.html#page:authentication にアクセス

image.png

  1. Developer Settingsをクリックしてサインアップ

  2. トークンを生成
    image.png

使ってみる

アーティストIDを取得する

newの第一引数に自分のアプリ名(多分なんでもいい)、第二引数にユーザートークンを設定する
discogsからyaejiに関する情報を取得する。

次のメソッドを実行すると

discogs_manager.rb
class Discogs_manager
  require 'discogs-wrapper'

auth_wrapper = Discogs::Wrapper.new(
        "アプリ名", user_token: "ユーザートークン"
        )
    search = auth_wrapper.search("yaeji", per_page: 10, type: :artist)
    puts search.results
end
end

次のように結果が返ってくる。

# <Hashie::Mash cover_image="https://img.discogs.com/HNsphNdpp5Xm_lboR0nCsXb3n1k=/600x819/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/A-5294873-1509655522-2592.jpeg.jpg" id=5294873 master_id=nil master_url=nil resource_url="https://api.discogs.com/artists/5294873" thumb="https://img.discogs.com/T5LLenQqOoje7_0qKFE6BqtyCqI=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-5294873-1509655522-2592.jpeg.jpg" title="Yaeji" type="artist" uri="/artist/5294873-Yaeji" user_data=#<Hashie::Mash in_collection=false in_wantlist=false>>#<Hashie::Mash cover_image="https://img.discogs.com/6c7c7083538b032c3e9ef32acbd53fb23a88d9d0/images/spacer.gif" id=7473529 master_id=nil master_url=nil resource_url="https://api.discogs.com/artists/7473529" thumb="" title="Katherine Yaeji Lee" type="artist" uri="/artist/7473529-Katherine-Yaeji-Lee" user_data=#<Hashie::Mash in_collection=false in_wantlist=false>>
# <Hashie::Mash cover_image="https://img.discogs.com/jRu60yfLwNLfCc63iGO3mKDzkdg=/600x466/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/A-6557568-1556058811-9015.jpeg.jpg" id=6557568 master_id=nil master_url=nil resource_url="https://api.discogs.com/artists/6557568" thumb="https://img.discogs.com/jiW4R_Iq7HC2aUpyG75DoulPLgs=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-6557568-1556058811-9015.jpeg.jpg" title="Korea Town Acid" type="artist" uri="/artist/6557568-Korea-Town-Acid" user_data=#<Hashie::Mash in_collection=false in_wantlist=false>>

これはdiscogsにyaejiに紐づくアーティスト情報が複数登録されているため、検索結果が二件取得されている。
検索結果の内、一件目を取得したい場合は次のようにする

# 省略
search = auth_wrapper.search("yaeji", per_page: 10, type: :artist) 
puts search.results.first
# 省略

すると検索結果は次のように一件のみ返る

# <Hashie::Mash cover_image="https://img.discogs.com/HNsphNdpp5Xm_lboR0nCsXb3n1k=/600x819/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/A-5294873-1509655522-2592.jpeg.jpg" id=5294873 master_id=nil master_url=nil resource_url="https://api.discogs.com/artists/5294873" thumb="https://img.discogs.com/T5LLenQqOoje7_0qKFE6BqtyCqI=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-5294873-1509655522-2592.jpeg.jpg" title="Yaeji" type="artist" uri="/artist/5294873-Yaeji" user_data=#<Hashie::Mash in_collection=false in_wantlist=false>>

アーティストIDのみを取得したい場合は次のようにする

# 省略
search = auth_wrapper.search("yaeji", per_page: 10, type: :artist) 
puts search.results.first.id
# 省略

アーティストIDが返ってくる

5294873
1
2
1

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?