require "active_support/core_ext/string/inflections" module Saber module Tracker2 class Base def self.inherited(child) Tracker2.trackers[child.name.demodulize.underscore] = child end # Implement BASE_URL = "" TYPES = {} attr_reader :agent, :name, :options 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 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.login 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 protected # check login and save cookies def check_login(url_pat) #unless agent.url =~ url_pat # agent.wait_until { agent.url =~ url_pat } # agent.cookies.dump("#{Rc.p.home}/#{name}.cookies") #end agent.wait_until { agent.url =~ url_pat } end # Implement # # Upload one torrent file to the site. # # @param [Optimism] info comes from .yml data file. # # @return [Boolean] result-code def new_upload(info) raise NotImplementedError end # Implement # # Upload via add formt # def add_format(info) raise NotImplementedError end end end end # vim: fdn=4