Skip to content

Instantly share code, notes, and snippets.

@eggplants
Last active November 30, 2019 19:27
Show Gist options
  • Save eggplants/f7073734c80ec1fa345d840a1eb50d7a to your computer and use it in GitHub Desktop.
Save eggplants/f7073734c80ec1fa345d840a1eb50d7a to your computer and use it in GitHub Desktop.
#############################################################
#############################################################
## ##
## _ _ _ _ ___ ____ ____ _ ____ _ ___ _ _ ##
##| || | | \ | | |_ _/ ___| | __ ) / \ / ___| |/ / | | |##
##| || |_| \| | | |\___ \ | _ \ / _ \| | | ' /| | | |##
##|__ _| |\ | | | ___) | | |_) / ___ \ |___| . \|_|_|_|##
## |_| |_| \_| |___|____/ |____/_/ \_\____|_|\_(_|_|_)##
## ##
## ##
#############################################################
#############################################################
# This script is to download the drawings written
# by: 4n(https://danbooru.donmai.us/artists/65485)
# from: 4n様保管庫(http://hatoyt.web.fc2.com/4n)
require "open-uri"
require "fileutils"
require "nokogiri"
def checkMkdir(test)
unless FileTest.exist?(test)
FileUtils.mkdir_p(test)
p "SaveDIR(#{test}) CREATED!"
end
end
image_PATH=[]
42.times do | i |
before,after=i*10+1,i*10+10
before="01" if i==0
p uri_EACHPAGE="http://hatoyt.web.fc2.com/4n/#{before}-#{after}.htm"
begin; source=Nokogiri::HTML.parse(URI.open(uri_EACHPAGE).read)
rescue; retry
end
image_PATH<< source.xpath("//*/p/img[@src]").map{|_|
_='http://hatoyt.web.fc2.com/4n/'+_[:src]
}
end
# remove recursive arraies
image_PATH.each_slice(2).map(&:last)
# create save directory
save_PATH="#{__dir__}/4n_archive_images"
checkMkdir(save_PATH)
flag=true
image_PATH.each do | _ |;_.each{ | image_URI |
# give a filename & its saving path
file_NAME=File.basename(image_URI)
file_PATH="#{save_PATH}/#{'%02d' % (image_PATH.index(_)+1)}"
checkMkdir(file_PATH)
if FileTest.exist?(file_PATH+'/'+file_NAME)
# already retrieved for past more than one time
p "EXIST!"
else
# save a image
File.open(file_PATH+'/'+file_NAME, 'wb') do | f |
begin; image_DATA=URI.open(image_URI).read
rescue;retry
end
f.write(image_DATA)
p "SAVED!(#{file_NAME})"
end
end
};end
#############################################################
#############################################################
## ##
## _ _ _ _ ___ ____ ____ _ ____ _ ___ _ _ ##
##| || | | \ | | |_ _/ ___| | __ ) / \ / ___| |/ / | | |##
##| || |_| \| | | |\___ \ | _ \ / _ \| | | ' /| | | |##
##|__ _| |\ | | | ___) | | |_) / ___ \ |___| . \|_|_|_|##
## |_| |_| \_| |___|____/ |____/_/ \_\____|_|\_(_|_|_)##
## ##
## ##
#############################################################
#############################################################
# This script is to download the drawings written
# by: 4n(https://danbooru.donmai.us/artists/65485)
# from: 4n様保管庫(http://hatoyt.web.fc2.com/4n)
require "open-uri"
require "fileutils"
require "nokogiri"
def checkMkdir(test)
unless FileTest.exist?(test)
FileUtils.mkdir_p(test)
p "SaveDIR(#{test}) CREATED!"
end
end
image_PATH=[]
42.times do | i |
before,after=i*10+1,i*10+10
before="01" if i==0
p uri_EACHPAGE="http://hatoyt.web.fc2.com/4n/#{before}-#{after}.htm"
begin; source=Nokogiri::HTML.parse(URI.open(uri_EACHPAGE).read)
rescue; retry
end
image_PATH<< source.xpath("//*/p/img[@src]").map{|_|
_='http://hatoyt.web.fc2.com/4n/'+_[:src]
}
end
# remove recursive arraies
image_PATH.each_slice(2).map(&:last)
# create save directory
save_PATH="#{__dir__}/4n_archive_images"
checkMkdir(save_PATH)
flag=true
image_PATH.each do | _ |;_.each{ | image_URI |
# give a filename & its saving path
file_NAME=File.basename(image_URI)
file_PATH="#{save_PATH}/ALL"
if flag; checkMkdir(file_PATH); flag=false; end
if FileTest.exist?(file_PATH+'/'+file_NAME)
# already retrieved for past more than one time
p "EXIST!"
else
# save a image
File.open(file_PATH+'/'+file_NAME, 'wb') do | f |
begin; image_DATA=URI.open(image_URI).read
rescue;retry
end
f.write(image_DATA)
p "SAVED!(#{file_NAME})"
end
end
};end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment