nhentai.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'json'
  2. require 'down'
  3. require 'rest-client'
  4. require 'ruby-progressbar'
  5. require 'fileutils'
  6. $id = ARGV[0]
  7. if $id.nil? || $id.empty?
  8. puts "usage: ruby nhentai.rb <id>"
  9. exit 1
  10. end
  11. def main
  12. nhentai = RestClient::Request.new(
  13. :method => :get,
  14. :url => "https://nhentai.net/api/gallery/#{$id}"
  15. ).execute
  16. doujin = JSON.parse(nhentai.to_s)
  17. id = doujin["id"]
  18. title = doujin["title"]["pretty"]
  19. pages = doujin["num_pages"]
  20. media_id = doujin["media_id"]
  21. puts "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  22. puts "+ Title : #{title}"
  23. puts "+ ID : #{id}"
  24. puts "+ Pages : #{pages}"
  25. puts "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  26. puts "\n"
  27. print "+ Do you want download this doujin? (y/n): "; choose = STDIN.gets.strip
  28. begin
  29. if choose == "y"
  30. print "+ Path: "; path = STDIN.gets.strip
  31. print "\n"
  32. puts "+ Downloading #{pages} images:"
  33. ext = doujin["images"]["cover"]["t"]
  34. if ext == "j"
  35. for total in 1..pages
  36. progressbar = ProgressBar.create(format: '%a |%b>>%i| %p%% %t %e')
  37. dl = Down.download("https://i.nhentai.net/galleries/#{media_id}/#{total}.jpg",
  38. content_length_proc: -> (content_length) { progressbar.total = content_length },
  39. progress_proc: ->(progress) { progressbar.progress = progress })
  40. FileUtils.mv(dl.path, "#{path}#{dl.original_filename}")
  41. puts "+ Downloaded!"
  42. end
  43. elsif ext == "p"
  44. for total in 1..pages
  45. progressbar = ProgressBar.create(format: '%a |%b>>%i| %p%% %t %e')
  46. dl = Down.download("https://i.nhentai.net/galleries/#{media_id}/#{total}.png",
  47. content_length_proc: -> (content_length) { progressbar.total = content_length },
  48. progress_proc: ->(progress) { progressbar.progress = progress })
  49. FileUtils.mv(dl.path, "#{path}#{dl.original_filename}")
  50. puts "+ Downloaded!"
  51. end
  52. else
  53. for total in 1..pages
  54. progressbar = ProgressBar.create(format: '%a |%b>>%i| %p%% %t %e')
  55. dl = Down.download("https://i.nhentai.net/galleries/#{media_id}/#{total}.gif",
  56. content_length_proc: -> (content_length) { progressbar.total = content_length },
  57. progress_proc: ->(progress) { progressbar.progress = progress })
  58. FileUtils.mv(dl.path, "#{path}#{dl.original_filename}")
  59. puts "+ Downloaded!"
  60. end
  61. end
  62. puts "+ Finish downloaded #{pages} images!"
  63. elsif choose == "n"
  64. puts "+ Ok, have a nice day"
  65. exit 1
  66. else
  67. puts "+ ERROR: exit from program"
  68. exit 1
  69. end
  70. rescue RestClient::ExceptionWithResponse => e
  71. puts e.response
  72. exit 1
  73. end
  74. end
  75. main()