Class: Ruboty::YMCrawl::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/ymcrawl/main.rb

Instance Method Summary collapse

Constructor Details

#initializeCore

Returns a new instance of Core.



66
67
68
69
70
71
72
73
# File 'lib/ruboty/ymcrawl/main.rb', line 66

def initialize
  @data = DataManager.instance
  if @data.get_save_to != "local"
    @uploader = Uploader.new(@data.get_save_to, @data.get_current_app_key, @data.get_current_app_secret, @data.get_current_access_token)
    puts "env dropbox app key: #{@data.get_current_app_key}"
    puts "env dropbox app sec: #{@data.get_current_app_secret}"
  end
end

Instance Method Details

#crawl(urls) ⇒ Object

画像をクロールして保存する。保存したファイルのパスを返す。



78
79
80
81
82
# File 'lib/ruboty/ymcrawl/main.rb', line 78

def crawl(urls)
  puts "in crawl"
  ncrawler  = Crawler.new(@data.get_setting["dst_dir"], @data.get_current_uploder_info(urls[0]), @data.get_setting["wait_time"])
  urls.map{ |v| ncrawler.save_images(v) }
end

#get_uploaderObject



122
# File 'lib/ruboty/ymcrawl/main.rb', line 122

def get_uploader; @uploader end

#start(urls) ⇒ Object



75
# File 'lib/ruboty/ymcrawl/main.rb', line 75

def start(urls); upload crawl(urls) end

#upload(file_dirs) ⇒ Object

画像を指定した先へアップロード



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruboty/ymcrawl/main.rb', line 85

def upload(file_dirs)
  puts "in upload"
  setting  = @data.get_setting
  return nil if @data.get_save_to == "local"

  @uploader.(@data.get_current_access_token)
  zip_paths = file_dirs.map{ |dir| zip_dir(dir) }
  encode = (ENV["LANG"] == nil) ? "utf-8" : ENV["LANG"]
  begin
    file_dirs.each{ |dir| FileUtils::remove_entry_secure( dir.force_encoding(encode) ) }
  rescue
    if encode != "ascii-8bit"
      encode = "ascii-8bit"
      retry
    end
  end
  share_paths = []
  zip_paths.each do |path|
    puts "uploading #{path} to dropbox"
    put_result = @uploader.put([path])
    File::delete(path)
    share_paths << @uploader.get_share_link(put_result["path"])["url"]
  end
  return share_paths
end

#zip_dir(src) ⇒ Object

指定されたディレクトリ以下のファイルをzipにする。返り値はzipのパス



112
113
114
115
116
117
118
119
120
# File 'lib/ruboty/ymcrawl/main.rb', line 112

def zip_dir(src)
  dst = "#{src}.zip"
  Zip::Archive.open(dst, Zip::CREATE) do |ar|
    Dir.glob("#{src}/*").each do |item|
      ar.add_file(item)
    end
  end
  dst
end