Advertisement
Guest User

ebook-dl.rb

a guest
Dec 14th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.96 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'mechanize'
  4. require 'fileutils'
  5. require 'uri'
  6. require 'net/http'
  7.  
  8. exit if ARGV[0].nil?
  9.  
  10. url    = URI.parse(URI.encode(ARGV[0].to_s.strip))
  11. mech   = Mechanize::new
  12. page   = mech.get(url)
  13. books  = []
  14. pg_num = 1
  15.  
  16. FileUtils::mkdir_p folder = ARGV[1] || 'ebooks'
  17.  
  18. begin
  19.   page.links.map.with_index { |link, i|
  20.     Thread.new {
  21.         (page = link.click;
  22.          puts "[+] #{page.title}" ;
  23.         page.links.each { |lnk|
  24.           books << lnk.href.to_s if lnk.href.to_s =~ /http\:\/\/file\.allitebooks\.com/im
  25.         }) if link.rel.to_s =~ /bookmark/im
  26.     }
  27.   }.each(&:join)
  28. rescue Mechanize::ResponseCodeError, Net::HTTPNotFound
  29.   page = mech.get("http://www.allitebooks.com/page/#{pg_num+=1}/")
  30.   puts "[!] Going to next page..."
  31.   retry
  32. end
  33.  
  34. books.each_with_index { |book, i|
  35.   puts "(#{-~i}) Downloading #{book}"
  36.   File.open(folder + File::SEPARATOR + File.basename(book), 'wb') { |f| f << mech.get(book).body }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement