drawnboy meets subtech☆

 | 

2006-09-19

fetching artwork うんぬん 22:26

http://printf.dk/itunes_7_fetching_artwork_for.html

息抜きがてら。とりあえずベタ写しで。ストア番号とかはキャプチャすればわかるんだろうけどしてない。

require 'uri'
require 'open-uri'
require 'md5'
require 'base64'
require 'tempfile'
require 'zlib'
require 'rexml/document'

class I7AWF
  I7_UA    = "iTunes/7.0 (Macintosh; U; PPC Mac OS X 10.4.7)"
  I7_URL   = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZSearch.woa/wa/coverArtMatch?an=%s&pn=%s"
  I7_STORE = "143457"

  attr_accessor :artist, :album

  def initialize(options = {})
    @artist = options[:artist]
    @album  = options[:album]
  end 

  def payload
    comped_url = URI::escape( sprintf(I7_URL, @artist, @album) )

    res = open(
      comped_url,
      'User-Agent' => I7_UA,
      'X-Apple-Tz' => '7200',
      'X-Apple-Store-Front' => I7_STORE,
      'X-Apple-Validation'  => comp_seed(comped_url, I7_UA),
      #'Accept-Encoding' => 'gzip, x-aes-cbc',
      'Connection' => 'close',
      'Host' => 'ax.phobos.apple.com.edgesuite.net'
    ).read

    tempfile = Tempfile.new('i7awf', '/tmp')
    tempfile.puts(res)
    tempfile.close

    gz = Zlib::GzipReader.new(tempfile.open)
    content = gz.read
    gz.close

    return get_URL(content)
  end

  def get_URL(result)
    doc = REXML::Document.new result
    node = doc.elements["/Document/Protocol/plist/dict/string"]
    return node.nil? ? nil : node.text
  end

  def comp_seed(url, user_agent)
    random = sprintf("%04X%04X", rand(0x10000), rand(0x10000))
    static = Base64::decode64("ROkjAaKid4EUF5kGtTNn3Q==")
    url_end = (url =~ %r!.*/.*/.*(/.+)$!) ? $1 : '?'
    digest = Digest::MD5::hexdigest(url_end + user_agent + static + random)
    return random + '-' + digest.upcase
  end
end

if __FILE__ == $0
  i7 = I7AWF.new()
  i7.artist = "The Strokes"
  i7.album = "First Impressions Of Earth"
  p i7.payload
end
 |