Skip to content

Instantly share code, notes, and snippets.

@siwasu17
Created January 12, 2013 14:24
Show Gist options
  • Save siwasu17/4518267 to your computer and use it in GitHub Desktop.
Save siwasu17/4518267 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# coding: utf-8
#
# http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
#
require 'rubygems'
require 'json'
require 'open-uri'
require 'uri'
if ARGV.size < 1 then
puts "Usage: #{$0} <Search Word>"
exit 255
end
search_word = ARGV[0]
# smaple request
## https://itunes.apple.com/search?term=game&country=JP
country='JP'
media='music'
attribute='songTerm'
limit=5
uri = URI.parse(
"http://itunes.apple.com/search?"\
"country=#{country}"\
"&term=#{search_word}"\
"&media=#{media}"\
"&attribute=#{attribute}"\
"&limit=#{limit}"
)
puts "Request: #{uri}"
doc = uri.read
resObj = JSON.parse(doc)
count = resObj['resultCount']
puts "取得件数: #{count}"
resObj['results'].each { |con|
puts "============"
puts "TrackName: #{con['trackName']}"
puts "Artist: #{con['artistName']}"
puts "Preview: #{con['previewUrl']}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment