class MyJobDojin

Attributes

args[RW]

Public Class Methods

new(args = { }) click to toggle source
# File lib/lib/job.rb, line 600
def initialize(args = { })
  require 'net/http'
  @args = args

  #sample http://1patu.net/data/20591/preview/000.jpg
  @args[:path]   = "/data/#{@args[:book].to_s}/preview/" +
    sprintf("%0#{3}d", @args[:page]) + ".jpg"
  @args[:cookie] ||= { 'Cookie' => '1patu_view=1'}
  @args[:status] = :new
  @args[:try]    = 0

  @args[:savedir] ||= "/Users/seijiro/Downloads/jpg"
  @args[:savebookdir] = "#{@args[:savedir].to_s}/#{@args[:book].to_s}"
  checkdir
  @args[:savepath] = "#{@args[:savebookdir]}/" +
    sprintf("%0#{3}d", @args[:page]) + ".jpg"
  @machine = @args[:machine]

  #debug
  @args[:debug]  ||= false
  @args[:savepath] = '/dev/null' if @args[:dryrun]
end

Public Instance Methods

run() click to toggle source
# File lib/lib/job.rb, line 623
def run
  do_connect
end

Private Instance Methods

checkdir() click to toggle source

ダウンロード保存先を作る

# File lib/lib/job.rb, line 669
def checkdir
  begin
    Dir::mkdir(@args[:savebookdir], 0777)
  rescue => ex
    #      warn ex
  end
end
do_connect() click to toggle source
# File lib/lib/job.rb, line 631
def do_connect
  puts "Do Connect".green
  return if @machine.bookended?(@args[:book])
  return if file_already_saved?
  
  Net::HTTP.start(@args[:server]) do |http|
    response = http.get(@args[:path],@args[:cookie])

    case response
    when Net::HTTPSuccess     then
      save_content(response.body)
    when Net::HTTPClientError     then
      @machine.bookend(@args[:book])
    when Net::HTTPServerError     then
      puts "Net::HTTPSereverError".red
      Thread.sleep 2
      @args[:try] += 1
      if @args[:try] < 6
        @machine.retry(self)
      else
      end
    when Net::HTTPRedirection then
      puts "Net::HTTPRedirection"
    else
      puts (response.error!).red.bold
    end        
  end
end
file_already_saved?() click to toggle source
# File lib/lib/job.rb, line 677
def file_already_saved?
  File.exist?(@args[:savepath]) && FileTest.size(@args[:savepath]) > 0
end
save_content(content) click to toggle source
# File lib/lib/job.rb, line 660
def save_content(content)
  open(@args[:savepath],"wb") do |io|
    io.write(content)
  end
  print "fetched:".green.bold + @args[:path]
  @machine.savecontent(@args[:savepath])
end