Created
June 11, 2017 10:05
-
-
Save Konstantinusz/c218fac17df97a5d968af649f719eec4 to your computer and use it in GitHub Desktop.
Save gallery from imagebam by its GID. It handles filename collisions by renaming.
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
#!/usr/bin/ruby | |
require "pry" | |
require "peach" rescue nil | |
gallery=ARGV[0].chomp("/") | |
page_num=1 | |
gallery_id=gallery.split("/").last | |
%x{mkdir "#{gallery_id}"} | |
Dir.chdir(gallery_id) | |
begin | |
ret=%x{curl -s -L #{gallery}/#{page_num}} | |
links=ret.scan(/href\=[\'\"](https?\:\/\/www\.imagebam\.com\/image\/[a-f0-9]+)[\'\"]/i).flatten.uniq | |
if links.respond_to?(:peach) then | |
links.peach(4){|l| | |
page=%x{curl -s -L "#{l}"} | |
md=(/href\=[\'\"](https?\:\/\/[0-9]+\.imagebam\.com\/download\/.*?\/[0-9]+\/[0-9]+\/.*?)\?download\=1[\'\"]/i).match(page) | |
url=md[1] | |
STDERR.puts "#{l} -> #{url}" | |
fname=url.split("/").last | |
if !File.exists?(fname) then | |
%x{curl -# -L -O -J "#{url}"} | |
else | |
index=1 | |
begin | |
extension=File.extname(fname) | |
new_fname=fname.chomp(extension)+"."+index.to_s+extension | |
index+=1 | |
end while File.exists?(new_fname) | |
%x{curl -# -L "#{url}" > '#{new_fname}'} | |
end | |
} | |
else | |
links.each{|l| | |
page=%x{curl -s -L "#{l}"} | |
md=(/href\=[\'\"](https?\:\/\/[0-9]+\.imagebam\.com\/download\/.*?\/[0-9]+\/[0-9]+\/.*?)\?download\=1[\'\"]/i).match(page) | |
url=md[1] | |
STDERR.puts "#{l} -> #{url}" | |
fname=url.split("/").last | |
if !File.exists?(fname) then | |
%x{curl -# -L -O -J "#{url}"} | |
else | |
index=1 | |
begin | |
extension=File.extname(fname) | |
new_fname=fname.chomp(extension)+"."+index.to_s+extension | |
index+=1 | |
end while File.exists?(new_fname) | |
%x{curl -# -L "#{url}" > '#{new_fname}'} | |
end | |
} | |
end | |
page_num+=1 | |
end while links.size>0 | |
Dir.chdir("..") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work on Linux with ruby 3.0.1p64 (2021-04-05 revision)
'error:
<internal:/usr/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in
require': cannot load such file -- pry (LoadError)Did you mean? pty
from internal:/usr/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb:85:in
require' from imagebamdl.rb:3:in
`
idea how to fix it? @Konstantinusz
btw. another attempt in bash here