class PIAPRO::Download

Public Class Methods

new(arg={}) click to toggle source
# File core/lib/piapro.rb, line 63
def initialize(arg={})
  if arg[:cookie]
    @cookie = arg[:cookie]
  elsif arg[:user] and arg[:passwd]
    @cookie = PIAPRO::Auth.auth(arg[:user], arg[:passwd])
  end
end

Public Instance Methods

download(cid, type=nil, filename=nil) click to toggle source

cidからダウンロード typeにコンテンツのタイプ(audio、imageなど)を指定する

# File core/lib/piapro.rb, line 98
def download(cid, type=nil, filename=nil)
  # http://piapro.jp/download/?view=content_image&id=cidcidcidcidcid
  url = "http://piapro.jp/download/" + "?"
  if type
    url << "view=content_#{type}&"
  else
    url << "view=content_audio&"
  end
  url << "id=#{cid}"

  open(url,
       "User-Agent" => USER_AGENT,
       "Cookie"     => @cookie,
       "Referer"    => REFERER
       ){|f|
    # f.meta.keys.each{|key|
    #   printf("%10s : %10s\n", key, Kconv.kconv(f.meta[key], MY_CHARCODE, Kconv::AUTO))
    # }
    # p f.meta
    return false unless f.meta["content-disposition"]
    ct_dp = Kconv.kconv(f.meta["content-disposition"], MY_CHARCODE, Kconv::AUTO)
    if ct_dp =~ /filename=\"(.+)\"/
      file_orgname = $1
    else
      return false
    end

    filename = file_orgname unless filename
    File.open(filename, "wb"){|new|
      new.write f.read
    }
    return filename
  }
end
download_url(url, filename=nil) click to toggle source

urlからダウンロード コンテンツページurl、短縮urlはNG。直接ダウンロードリンク指定が必要

# File core/lib/piapro.rb, line 73
def download_url(url, filename=nil)
  open(url,
       "User-Agent" => USER_AGENT,
       "Cookie"     => @cookie,
       "Referer"    => REFERER
       ){|f|
    return false unless f.meta["content-disposition"]
    ct_dp = Kconv.kconv(f.meta["content-disposition"], MY_CHARCODE, Kconv::AUTO)
    if ct_dp =~ /filename=\"(.+)\"/
      file_orgname = $1
    else
      return false
    end

    filename = file_orgname unless filename
    File.open(filename, "wb"){|new|
      new.write f.read
    }
    return filename
  }
end