Created
January 26, 2011 14:15
-
-
Save masarakki/796740 to your computer and use it in GitHub Desktop.
2次画像ダウンローダ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'uri' | |
require 'kconv' | |
Net::HTTP.version_1_2 | |
puts 'require url' and exit if ARGV[0].nil? | |
def fetch_url(src_url, dist = 0) | |
uri = URI.parse(src_url) | |
puts "fetching #{src_url}" | |
html_body = '' | |
Net::HTTP.start(uri.host, uri.port) do |http| | |
response = http.get(uri.path) | |
html_body = response.body | |
end | |
title_full = html_body.match(/<title>(.+)<\/title>/)[1] | |
puts " #{title_full.tosjis}" | |
title = title_full.split(/:/).last.match(/(.+?)(その.+)?$/)[1].strip | |
image_urls = html_body.scan(/<a[^h]*href="(http:\/\/(nijigazo\.0t0\.jp|img\.nijigazo\.com)([^>]*?\.(jpg|png|gif)?))".*?>/).map do |match| | |
match | |
end | |
connections = html_body.scan(/<a rel="(prev|next)" href="(http:\/\/nijigazo\.2chblog\.jp\/archives\/[0-9]+\.html)">(.+?)<\/a>/).uniq | |
path = image_urls[0][2].split('/') | |
d = path[0..3].join('') | |
chara = path[4] | |
dir_base = "E:\\masaki\\Pictures\\2jisoku\\#{chara}" | |
Dir.mkdir(dir_base) unless File.exists?(dir_base) | |
dir = "#{dir_base}\\#{d}" | |
Dir.mkdir(dir) unless File.exists?(dir) | |
image_urls.each_with_index do |image_url, idx| | |
host = image_url[1] | |
fname = image_url[2] | |
begin | |
Net::HTTP.start(host, 80) do |http| | |
req = Net::HTTP::Get.new(fname) | |
req['referer'] = uri | |
res = http.request(req) | |
if res.code.to_i == 200 | |
filename = "#{dir}\\#{File.basename(fname)}" | |
File.open(filename, 'wb') do |file| | |
file.write res.body | |
end | |
end | |
end | |
rescue => e | |
puts image_url | |
end | |
end | |
connections.each do |connection| | |
case connection[0] | |
when "prev" | |
fetch_url(connection[1], -1) if dist <= 0 && connection[2].match(title) | |
when "next" | |
fetch_url(connection[1], 1) if dist >= 0 && connection[2].match(title) | |
end | |
end | |
end | |
fetch_url(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment