| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
ば再度ログインする必要が多分無くなる |
| 8 |
はどうするべきか |
| 9 |
|
| 10 |
require "kconv" |
| 11 |
require "open-uri" |
| 12 |
require "net/https" |
| 13 |
|
| 14 |
class PIAPRO |
| 15 |
USER_AGENT = "Ruby-PIAPRO/0.3" |
| 16 |
REFERER = "http://piapro.jp/" |
| 17 |
MY_CHARCODE = Kconv::UTF8 |
| 18 |
PIAPRO_CHARCODE = Kconv::SJIS |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
module Auth |
| 23 |
LOGIN_PAGE = "https://piapro.jp/login/?mode=input" |
| 24 |
|
| 25 |
def self.auth(user, passwd, auto_login = nil) |
| 26 |
https = Net::HTTP.new("piapro.jp", 443) |
| 27 |
https.use_ssl = true |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
https.start {|session| |
| 32 |
post_login = Net::HTTP::Service.new("/login/") |
| 33 |
post_login["User-Agent"] = USER_AGENT |
| 34 |
post_login["Referer"] = REFERER |
| 35 |
post_login.set_form_data( |
| 36 |
{ |
| 37 |
:mode => "exe", |
| 38 |
:login_url => LOGIN_PAGE, |
| 39 |
:login_email => user, |
| 40 |
:login_password => passwd, |
| 41 |
:auto_login => auto_login |
| 42 |
} |
| 43 |
) |
| 44 |
response = session.request(post_login) |
| 45 |
@cookie = response["Set-Cookie"] |
| 46 |
} |
| 47 |
return @cookie |
| 48 |
end |
| 49 |
|
| 50 |
def self.logout(cookie) |
| 51 |
open("http://piapro.jp/logout/?mode=exe", |
| 52 |
"User-Agent" => USER_AGENT, |
| 53 |
"Cookie" => @cookie, |
| 54 |
"Referer" => REFERER |
| 55 |
){|f| |
| 56 |
return f.read |
| 57 |
} |
| 58 |
end |
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
class Download |
| 63 |
def initialize(arg={}) |
| 64 |
if arg[:cookie] |
| 65 |
@cookie = arg[:cookie] |
| 66 |
elsif arg[:user] and arg[:passwd] |
| 67 |
@cookie = PIAPRO::Auth.auth(arg[:user], arg[:passwd]) |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
ダウンロード |
| 72 |
rlはNG。直接ダウンロードリンク指定が必要 |
| 73 |
def download_url(url, filename=nil) |
| 74 |
open(url, |
| 75 |
"User-Agent" => USER_AGENT, |
| 76 |
"Cookie" => @cookie, |
| 77 |
"Referer" => REFERER |
| 78 |
){|f| |
| 79 |
return false unless f.meta["content-disposition"] |
| 80 |
ct_dp = Kconv.kconv(f.meta["content-disposition"], MY_CHARCODE, Kconv::AUTO) |
| 81 |
if ct_dp =~ /filename=\"(.+)\"/ |
| 82 |
file_orgname = $1 |
| 83 |
else |
| 84 |
return false |
| 85 |
end |
| 86 |
|
| 87 |
filename = file_orgname unless filename |
| 88 |
File.open(filename, "wb"){|new| |
| 89 |
new.write f.read |
| 90 |
} |
| 91 |
return filename |
| 92 |
} |
| 93 |
end |
| 94 |
|
| 95 |
|
| 96 |
ダウンロード |
| 97 |
(audio、imageなど)を指定する |
| 98 |
def download(cid, type=nil, filename=nil) |
| 99 |
|
| 100 |
url = "http://piapro.jp/download/" + "?" |
| 101 |
if type |
| 102 |
url << "view=content_#{type}&" |
| 103 |
else |
| 104 |
url << "view=content_audio&" |
| 105 |
end |
| 106 |
url << "id=#{cid}" |
| 107 |
|
| 108 |
open(url, |
| 109 |
"User-Agent" => USER_AGENT, |
| 110 |
"Cookie" => @cookie, |
| 111 |
"Referer" => REFERER |
| 112 |
){|f| |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
return false unless f.meta["content-disposition"] |
| 118 |
ct_dp = Kconv.kconv(f.meta["content-disposition"], MY_CHARCODE, Kconv::AUTO) |
| 119 |
if ct_dp =~ /filename=\"(.+)\"/ |
| 120 |
file_orgname = $1 |
| 121 |
else |
| 122 |
return false |
| 123 |
end |
| 124 |
|
| 125 |
filename = file_orgname unless filename |
| 126 |
File.open(filename, "wb"){|new| |
| 127 |
new.write f.read |
| 128 |
} |
| 129 |
return filename |
| 130 |
} |
| 131 |
end |
| 132 |
end |
| 133 |
|
| 134 |
module Search |
| 135 |
画像をURLのページから探して |
| 136 |
のを集めて配列で返し、 |
| 137 |
列を返す、ものすごく適当なメソッド。 |
| 138 |
イン不要。 |
| 139 |
def self.trim_image(url) |
| 140 |
list = Array.new |
| 141 |
open(url){|f| |
| 142 |
f.each_line{|line| |
| 143 |
if line =~ /background:url\((http:\/\/.+?\.piapro\.jp\/timg\/.+?_0500_0500\.(jpg|png|gif))\)/i |
| 144 |
list << $1 |
| 145 |
end |
| 146 |
} |
| 147 |
} |
| 148 |
return list |
| 149 |
end |
| 150 |
|
| 151 |
|
| 152 |
指定したページから探し配列で返し、 |
| 153 |
を返す、これまたてきとうなメソッド。 |
| 154 |
イン不要。 |
| 155 |
def self.contents(url) |
| 156 |
list = Array.new |
| 157 |
open(url){|f| |
| 158 |
f.each_line{|line| |
| 159 |
if line =~ /href="(http:\/\/piapro.jp\/download\/.+?)"/i |
| 160 |
list << $1 |
| 161 |
end |
| 162 |
} |
| 163 |
} |
| 164 |
return list |
| 165 |
end |
| 166 |
end |
| 167 |
end |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|