123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- require 'json'
- require 'down'
- require 'rest-client'
- require 'ruby-progressbar'
- require 'fileutils'
- $id = ARGV[0]
- if $id.nil? || $id.empty?
- puts "usage: ruby nhentai.rb <id>"
- exit 1
- end
- def main
- nhentai = RestClient::Request.new(
- :method => :get,
- :url => "https://nhentai.net/api/gallery/#{$id}"
- ).execute
- doujin = JSON.parse(nhentai.to_s)
- id = doujin["id"]
- title = doujin["title"]["pretty"]
- pages = doujin["num_pages"]
- media_id = doujin["media_id"]
- puts "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
- puts "+ Title : #{title}"
- puts "+ ID : #{id}"
- puts "+ Pages : #{pages}"
- puts "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
- puts "\n"
-
- print "+ Do you want download this doujin? (y/n): "; choose = STDIN.gets.strip
- begin
- if choose == "y"
- print "+ Path: "; path = STDIN.gets.strip
-
- print "\n"
- puts "+ Downloading #{pages} images:"
-
- ext = doujin["images"]["cover"]["t"]
-
- if ext == "j"
- for total in 1..pages
- progressbar = ProgressBar.create(format: '%a |%b>>%i| %p%% %t %e')
- dl = Down.download("https://i.nhentai.net/galleries/#{media_id}/#{total}.jpg",
- content_length_proc: -> (content_length) { progressbar.total = content_length },
- progress_proc: ->(progress) { progressbar.progress = progress })
- FileUtils.mv(dl.path, "#{path}#{dl.original_filename}")
- puts "+ Downloaded!"
- end
- elsif ext == "p"
- for total in 1..pages
- progressbar = ProgressBar.create(format: '%a |%b>>%i| %p%% %t %e')
- dl = Down.download("https://i.nhentai.net/galleries/#{media_id}/#{total}.png",
- content_length_proc: -> (content_length) { progressbar.total = content_length },
- progress_proc: ->(progress) { progressbar.progress = progress })
- FileUtils.mv(dl.path, "#{path}#{dl.original_filename}")
- puts "+ Downloaded!"
- end
- else
- for total in 1..pages
- progressbar = ProgressBar.create(format: '%a |%b>>%i| %p%% %t %e')
- dl = Down.download("https://i.nhentai.net/galleries/#{media_id}/#{total}.gif",
- content_length_proc: -> (content_length) { progressbar.total = content_length },
- progress_proc: ->(progress) { progressbar.progress = progress })
- FileUtils.mv(dl.path, "#{path}#{dl.original_filename}")
- puts "+ Downloaded!"
- end
- end
- puts "+ Finish downloaded #{pages} images!"
- elsif choose == "n"
- puts "+ Ok, have a nice day"
- exit 1
- else
- puts "+ ERROR: exit from program"
- exit 1
- end
- rescue RestClient::ExceptionWithResponse => e
- puts e.response
- exit 1
- end
- end
- main()
|