RubyでiTunes Search APIを使ってみました。
Rubygemsのitunes-search-apiを使いました。
インストール
gem install itunes-search-api
でインストールします。
このままだと、バージョンが古くて、使えない機能がありました。あまり良くないのですが、 https://github.com/rlivsey/itunes-search-api/blob/master/lib/itunes-search-api.rb の内容を/var/lib/gems/2.0.0/gems/itunes-search-api-0.1.0/lib/itunes-search-api.rb に上書きしました。 実際のソースはこのファイルだけなので、Gemをインストールしなくても良い気もしますが…
iTunes Search API
iTunes Search API には searchとlookupという二つのAPIが用意されています。
- search :検索文字列を与えると、その文字列で検索を行い結果を返します。
- lookup :IDの文字列を与えると、IDに対応するデータを返します。
詳細は、iTunes Search APIを見てください。
search の例
検索文字列に対応するiTunesの楽曲情報を取得する。
require 'itunes-search-api'
ITunesSearchAPI.search(
:term => "RYUTist",
:country => 'jp',
:media => 'music',
:lang => 'ja_jp',
:limit => '2'
).each do |item|
p item
print("#{item['artistName']},#{item['trackName']}\n")
end
実行結果
$ ruby search.rb
{"wrapperType"=>"track", "kind"=>"song", "artistId"=>534038778, "collectionId"=>534038740, "trackId"=>534038851, "artistName"=>"RYUTist", "collectionName"=>"RYUTist! ~新しいHOME~ - EP", "trackName"=>"ラリリレル", "collectionCensoredName"=>"RYUTist! ~新しいHOME~ - EP", "trackCensoredName"=>"ラリリレル", "artistViewUrl"=>"https://itunes.apple.com/jp/artist/ryutist/id534038778?uo=4", "collectionViewUrl"=>"https://itunes.apple.com/jp/album/raririreru/id534038740?i=534038851&uo=4", "trackViewUrl"=>"https://itunes.apple.com/jp/album/raririreru/id534038740?i=534038851&uo=4", "previewUrl"=>"http://a1018.phobos.apple.com/us/r20/Music/10/d3/1e/mzi.pdertafk.aac.p.m4a", "artworkUrl30"=>"http://is4.mzstatic.com/image/thumb/Music/v4/99/55/2a/99552af9-4661-12a5-acb2-d669c573c83f/source/30x30bb.jpg", "artworkUrl60"=>"http://is4.mzstatic.com/image/thumb/Music/v4/99/55/2a/99552af9-4661-12a5-acb2-d669c573c83f/source/60x60bb.jpg", "artworkUrl100"=>"http://is4.mzstatic.com/image/thumb/Music/v4/99/55/2a/99552af9-4661-12a5-acb2-d669c573c83f/source/100x100bb.jpg", "collectionPrice"=>900.0, "trackPrice"=>150.0, "releaseDate"=>"2012-05-29T07:00:00Z", "collectionExplicitness"=>"notExplicit", "trackExplicitness"=>"notExplicit", "discCount"=>1, "discNumber"=>1, "trackCount"=>6, "trackNumber"=>3, "trackTimeMillis"=>211998, "country"=>"JPN", "currency"=>"JPY", "primaryGenreName"=>"J-Pop", "isStreamable"=>false}
RYUTist,ラリリレル
{"wrapperType"=>"track", "kind"=>"song", "artistId"=>534038778, "collectionId"=>534038740, "trackId"=>534038849, "artistName"=>"RYUTist", "collectionName"=>"RYUTist! ~新しいHOME~ - EP", "trackName"=>"RYUTist! ~新しいHOME~", "collectionCensoredName"=>"RYUTist! ~新しいHOME~ - EP", "trackCensoredName"=>"RYUTist! ~新しいHOME~", "artistViewUrl"=>"https://itunes.apple.com/jp/artist/ryutist/id534038778?uo=4", "collectionViewUrl"=>"https://itunes.apple.com/jp/album/ryutist!-xinshiihome/id534038740?i=534038849&uo=4", "trackViewUrl"=>"https://itunes.apple.com/jp/album/ryutist!-xinshiihome/id534038740?i=534038849&uo=4", "previewUrl"=>"http://a936.phobos.apple.com/us/r20/Music/63/c4/f4/mzi.rskitlaq.aac.p.m4a", "artworkUrl30"=>"http://is4.mzstatic.com/image/thumb/Music/v4/99/55/2a/99552af9-4661-12a5-acb2-d669c573c83f/source/30x30bb.jpg", "artworkUrl60"=>"http://is4.mzstatic.com/image/thumb/Music/v4/99/55/2a/99552af9-4661-12a5-acb2-d669c573c83f/source/60x60bb.jpg", "artworkUrl100"=>"http://is4.mzstatic.com/image/thumb/Music/v4/99/55/2a/99552af9-4661-12a5-acb2-d669c573c83f/source/100x100bb.jpg", "collectionPrice"=>900.0, "trackPrice"=>150.0, "releaseDate"=>"2012-05-29T07:00:00Z", "collectionExplicitness"=>"notExplicit", "trackExplicitness"=>"notExplicit", "discCount"=>1, "discNumber"=>1, "trackCount"=>6, "trackNumber"=>1, "trackTimeMillis"=>234301, "country"=>"JPN", "currency"=>"JPY", "primaryGenreName"=>"J-Pop", "isStreamable"=>false}
RYUTist,RYUTist! ~新しいHOME~
lookup の例
artistIdに関連するiTunesの楽曲情報を取得する
require 'itunes-search-api'
ITunesSearchAPI.lookup(
:id => '534038778',
:entity => 'song',
:country => 'jp',
:limit => '2',
:sort => 'recent',
).each do |item|
if (item['trackId'])
p item
print("#{item['artistName']},#{item['trackName']}\n")
end
end
実行結果
$ ruby lookup.rb
{"wrapperType"=>"track", "kind"=>"song", "artistId"=>534038778, "collectionId"=>1163185685, "trackId"=>1163185795, "artistName"=>"RYUTist", "collectionName"=>"Winter merry go round - EP", "trackName"=>"Winter Wish (Minus RYUTist)", "collectionCensoredName"=>"Winter merry go round - EP", "trackCensoredName"=>"Winter Wish (Minus RYUTist)", "artistViewUrl"=>"https://itunes.apple.com/jp/artist/ryutist/id534038778?uo=4", "collectionViewUrl"=>"https://itunes.apple.com/jp/album/winter-wish-minus-ryutist/id1163185685?i=1163185795&uo=4", "trackViewUrl"=>"https://itunes.apple.com/jp/album/winter-wish-minus-ryutist/id1163185685?i=1163185795&uo=4", "previewUrl"=>"http://audio.itunes.apple.com/apple-assets-us-std-000001/AudioPreview71/v4/3c/e3/7b/3ce37b43-1ec8-c5fb-b5ea-2ef38ef8c086/mzaf_7522996155650872529.plus.aac.p.m4a", "artworkUrl30"=>"http://is2.mzstatic.com/image/thumb/Music71/v4/56/e3/1a/56e31ac0-3e1c-e399-0e87-df68f79f4554/source/30x30bb.jpg", "artworkUrl60"=>"http://is2.mzstatic.com/image/thumb/Music71/v4/56/e3/1a/56e31ac0-3e1c-e399-0e87-df68f79f4554/source/60x60bb.jpg", "artworkUrl100"=>"http://is2.mzstatic.com/image/thumb/Music71/v4/56/e3/1a/56e31ac0-3e1c-e399-0e87-df68f79f4554/source/100x100bb.jpg", "collectionPrice"=>1200.0, "trackPrice"=>200.0, "releaseDate"=>"2015-11-17T08:00:00Z", "collectionExplicitness"=>"notExplicit", "trackExplicitness"=>"notExplicit", "discCount"=>1, "discNumber"=>1, "trackCount"=>6, "trackNumber"=>6, "trackTimeMillis"=>245653, "country"=>"JPN", "currency"=>"JPY", "primaryGenreName"=>"J-Pop", "isStreamable"=>true}
RYUTist,Winter Wish (Minus RYUTist)
{"wrapperType"=>"track", "kind"=>"song", "artistId"=>534038778, "collectionId"=>1163185685, "trackId"=>1163185794, "artistName"=>"RYUTist", "collectionName"=>"Winter merry go round - EP", "trackName"=>"piece of life (Minus RYUTist)", "collectionCensoredName"=>"Winter merry go round - EP", "trackCensoredName"=>"piece of life (Minus RYUTist)", "artistViewUrl"=>"https://itunes.apple.com/jp/artist/ryutist/id534038778?uo=4", "collectionViewUrl"=>"https://itunes.apple.com/jp/album/piece-of-life-minus-ryutist/id1163185685?i=1163185794&uo=4", "trackViewUrl"=>"https://itunes.apple.com/jp/album/piece-of-life-minus-ryutist/id1163185685?i=1163185794&uo=4", "previewUrl"=>"http://audio.itunes.apple.com/apple-assets-us-std-000001/AudioPreview62/v4/a5/50/05/a55005e0-7027-1d21-05a3-a69c8830a733/mzaf_1470101674958450610.plus.aac.p.m4a", "artworkUrl30"=>"http://is2.mzstatic.com/image/thumb/Music71/v4/56/e3/1a/56e31ac0-3e1c-e399-0e87-df68f79f4554/source/30x30bb.jpg", "artworkUrl60"=>"http://is2.mzstatic.com/image/thumb/Music71/v4/56/e3/1a/56e31ac0-3e1c-e399-0e87-df68f79f4554/source/60x60bb.jpg", "artworkUrl100"=>"http://is2.mzstatic.com/image/thumb/Music71/v4/56/e3/1a/56e31ac0-3e1c-e399-0e87-df68f79f4554/source/100x100bb.jpg", "collectionPrice"=>1200.0, "trackPrice"=>200.0, "releaseDate"=>"2015-11-17T08:00:00Z", "collectionExplicitness"=>"notExplicit", "trackExplicitness"=>"notExplicit", "discCount"=>1, "discNumber"=>1, "trackCount"=>6, "trackNumber"=>5, "trackTimeMillis"=>271173, "country"=>"JPN", "currency"=>"JPY", "primaryGenreName"=>"J-Pop", "isStreamable"=>true}
RYUTist,piece of life (Minus RYUTist)