Class: Saber::Tracker2::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/saber/tracker2/base.rb

Direct Known Subclasses

BIB, Gazelle, What

Constant Summary collapse

BASE_URL =

Implement

""
TYPES =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/saber/tracker2/base.rb', line 16

def initialize(options={})
  @name = self.class.name.demodulize.underscore
  @agent = Watir::Browser.new *Rc.browser 
  @options = options
  #@agent.load_cookies("#{Rc.p.home}/#{name}.cookies")

  # share cookies
  launcher = agent.driver.instance_variable_get("@bridge").instance_variable_get("@launcher")
  origin_profile_dir = launcher.instance_variable_get("@profile").instance_variable_get("@model")
  profile_dir = launcher.instance_variable_get("@profile_dir")

  Pa.symln_f "#{origin_profile_dir}/cookies.sqlite", "#{profile_dir}/cookies.sqlite"
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



14
15
16
# File 'lib/saber/tracker2/base.rb', line 14

def agent
  @agent
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/saber/tracker2/base.rb', line 14

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/saber/tracker2/base.rb', line 14

def options
  @options
end

Class Method Details

.inherited(child) ⇒ Object



6
7
8
# File 'lib/saber/tracker2/base.rb', line 6

def self.inherited(child)
  Tracker2.trackers[child.name.demodulize.underscore] = child
end

Instance Method Details

#upload(format, filemap, o = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/saber/tracker2/base.rb', line 30

def upload(format, filemap, o={})
  failed_counts = 0

  filemap.each {|file, torrent_file|
    info0 = YAML.load_file("#{file}.yml")

    # current release only support ebook type.
    Saber.ui.error "Current doesn't support this upload_type -- #{info0['upload_type']}." unless 
      %w[ebook magazine journal newspaper manual article comic].include? info0["upload_type"]

    info = {}

    # convert {"tracker.tags" => x} to {"trackers" => {"tags" => x}}
    info0.each {|k, v|
      if k.include?(".")
        tracker, key = k.split(".")
        info[tracker] ||= {}
        info[tracker][key] = v
      elsif Hash === info[k] and Hash === v
        info[k].merge!(v)
      else
        info[k] = v
      end
    }

    info.deep_symbolize_keys!
    info[:format] = format.upcase
    info[:upload_type2] = self.class::TYPES[info[:upload_type]] 
    info[:torrent_file] = Pa.absolute2(torrent_file)
    info.merge! info.delete(name.to_sym){ {} }
    
    # skip empty description.
    if info[:description].empty?
      Saber.ui.error "SKIP: Empty description -- #{file}.yml"
      next
    end

    # check exists?
    if Tracker[name].respond_to?(:exists?) 
      tracker = Tracker[name].new
      tracker.
      if tracker.exists?(isbn: info[:isbn])
        Saber.ui.say "SKIP: File existing in #{name} site. -- (#{info[:isbn]}) #{info[:title]}"
        next
      end
    end

    upload_method = o[:add] ? :add_format : :new_upload
    if send(upload_method, info)
      Saber.ui.say "Upload Complete: #{file}"

      Pa.mv "#{file}.yml", Rc.upload.move_yml if Rc._has_key?("upload.move_yml")
      Pa.mv torrent_file, Rc.upload.move_torrent if Rc._has_key?("upload.move_torrent")
    else
      failed_counts += 1
      Saber.ui.error "SKIP: Upload failed -- #{file}"
    end
  }

  agent.quit unless failed_counts > 0
end