Skip to content

Instantly share code, notes, and snippets.

@luosheng
Last active December 11, 2015 04:49
Show Gist options
  • Save luosheng/4548284 to your computer and use it in GitHub Desktop.
Save luosheng/4548284 to your computer and use it in GitHub Desktop.
178 downloader
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'execjs'
require 'net/http'
require "em-synchrony"
require "em-synchrony/em-http"
require "em-synchrony/fiber_iterator"
HOST = 'http://imgfast.manhua.178.com/'
url = ARGV[0]
doc = Nokogiri::HTML(open(url))
script = doc.css('script')[0]
dir = doc.css('title')[0].content
Dir.mkdir dir unless Dir.exists? dir
context = ExecJS.compile(script.content)
urls = context.eval("arr_pages").map { |page| "#{HOST}#{URI.encode(URI.decode(page))}" }
EM.synchrony do
concurrency = 10
EM::Synchrony::FiberIterator.new(urls, concurrency).each do |url|
index = urls.index(url) + 1
p "#{index} of #{urls.count}"
http = EventMachine::HttpRequest.new(url).get
file = open("#{dir}/#{'%03d' % index}.jpg", "wb")
file.write(http.response)
file.close
end
EventMachine.stop
system("zip #{dir} #{dir}/*")
system("rm -rf #{dir}")
end
source 'https://rubygems.org/'
gem 'nokogiri'
gem 'execjs'
gem 'em-synchrony'
gem 'em-http-request'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment