最近比较想看loli的时间,但是在线看太慢了,每一页都要等半天,就想找个地方下载下来看 xunlei的漫画下载是有规律可循的,下载起来相对容易一些。写了几个正则来处理,但是后来每篇就10几页,太麻烦了,就写了个程序来处理,就当ruby的练手程序了 代码共享给大家,欢迎指正,呵呵
require 'net/http'
require 'fileutils'
require 'uri'
require 'stringio'
$url_base = "/origin/"
$hostname = "images.mh.xunlei.com"
$save_dir_regex = /萝莉的时间 - 第(\d+)话 - 漫画在线/
$save_dir = ""
$images_arr_regex = /images_arr\[(\d{1,2})\] = \'(\S+\.jpg)\';/
def processFile(body)
ip = StringIO.new(body)
ip.each_line do |line|
if line =~ $save_dir_regex
$save_dir = $1
FileUtils.mkdir($save_dir) unless File.exist?($save_dir)
end
#puts line if line =~ images_arr_regex
if line =~ $images_arr_regex and $save_dir != ""
puts "downloading http://" + $hostname + $url_base + $2
puts "saving as " + $save_dir + "/" + expendFilename($1) + ".jpg"
downloadAndSavePics($hostname, $url_base + $2, $save_dir + "/" + expendFilename($1) + ".jpg")
end
end
end
def expendFilename(filename)
'0' * (4 - filename.length) + filename
end
def downloadAndSavePics(hostname, relativePath, destFilename)
Net::HTTP.start(hostname){ |http|
response = http.get(relativePath)
if (response.message == "OK")
open(destFilename, "wb") { |file|
file.write(response.body)
}
end
}
end
#processFile('6309.html')
def batchProcessAllUrls(filename)
File.open(filename, 'r') { |file|
while line = file.gets
puts line
url = URI.parse(line)
puts "" + url.host + url.path
Net::HTTP.start(url.host) {|http|
response = http.get(url.path)
if response.message == "OK"
processFile(response.body)
end
}
end
}
end
batchProcessAllUrls("index_urls")