Class: Sfacg::Chapter
- Inherits:
-
Object
- Object
- Sfacg::Chapter
- Defined in:
- lib/sfacg/chapter.rb
Constant Summary collapse
- SF_HOST =
'http://hotpic.sfacg.com/'.freeze
Instance Method Summary collapse
- #download(options = {to: '.'}) ⇒ Object
- #images ⇒ Object
-
#initialize(url) ⇒ Chapter
constructor
A new instance of Chapter.
- #js_uri ⇒ Object
Constructor Details
#initialize(url) ⇒ Chapter
Returns a new instance of Chapter.
8 9 10 11 |
# File 'lib/sfacg/chapter.rb', line 8 def initialize url @uri = URI(url) @js_uri = @images = nil end |
Instance Method Details
#download(options = {to: '.'}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sfacg/chapter.rb', line 27 def download = {to: '.'} to = [:to] FileUtils::mkdir_p to threads = [] images.each_with_index do |img_uri, i| threads << Thread.new{ file_name = "#{i}#{File.extname(img_uri.to_s)}" file_path = File.join(to, file_name) begin Net::HTTP.start(img_uri.host, img_uri.port, read_timeout: 5) do |http| File.write file_path, open(img_uri, 'Referer' => SF_HOST).read, mode: 'wb' puts "#{img_uri} -> #{file_path}" end rescue $stderr.puts "#{img_uri} -> #{file_path}", $!.inspect, $@ end } end threads.each(&:join) rescue $stderr.puts $!.inspect, $@ end |
#images ⇒ Object
20 21 22 23 24 25 |
# File 'lib/sfacg/chapter.rb', line 20 def images return @images if @images js = open(js_uri).read @hosts = js[/hosts\s*=\s*\[([^\]]*)\]/, 1].gsub(/[\s"']/, '').split(',') @images = js.scan(/picAy\[\d+\]\s*=\s*"([^"]*)"/).map{|pattern| URI.join(@hosts.first, pattern.first)} end |
#js_uri ⇒ Object
13 14 15 16 17 18 |
# File 'lib/sfacg/chapter.rb', line 13 def js_uri return @js_uri if @js_uri chapter_number = @uri.to_s[/\/([^\/]*)\/?$/, 1] doc = Nokogiri::HTML(open(@uri)) @js_uri = @uri.merge(doc.at_css("script[src*=\"#{chapter_number}.js\"]")['src']) end |