Module: KpFolder

Extended by:
Kuaipan::OpenAPI
Defined in:
lib/kp_folder/config.rb,
lib/kp_folder.rb

Overview

conding: utf-8

Defined Under Namespace

Modules: Config

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.k_sessionObject (readonly)

Returns the value of attribute k_session.



12
13
14
# File 'lib/kp_folder.rb', line 12

def k_session
  @k_session
end

Class Method Details

.add(folder) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kp_folder.rb', line 64

def self.add(folder)
  arr = []
  File.open(@folders_file, 'r')do |f|
    arr = JSON.parse(f.read)
  end
  set = arr.to_set
  set.add(folder)
  File.open(@folders_file, 'w')do |f|
    f.write(JSON.generate(set.to_a))
  end
  puts "success to add #{folder}"
end

.initObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kp_folder.rb', line 14

def self.init
  @oauth_result_file = "#{ Dir.home }/.kf_oauth_result.json"
  input_config(Config.consumer_key, Config.consumer_secret) 
  @k_session = nil
  if File.exist?(@oauth_result_file)
    File.open(@oauth_result_file, 'r')do |f|
      oauth_result = JSON.parse(f.read)
      @k_session = g_session_skip_oauth(oauth_result['oauth_token'],
                                        oauth_result['oauth_token_secret'],
                                        oauth_result['user_id'])
    end
  else
    @k_session = g_session  
    authorize_url = @k_session[:authorize_url]
    puts 'please copy the url to your brower,then get number here:'
    puts authorize_url
    oauth_verifier = gets
    oauth_result = @k_session.set_atoken oauth_verifier
    File.open(@oauth_result_file, 'w+')do |f|
      f.write(JSON.generate(oauth_result))
    end
  end
  @folders_file = "#{ Dir.home }/.kf_folders"
  @db_file = "#{ Dir.home }/.kf_db"
  File.open(@folders_file, 'w'){|f| f.write('[]')} unless File.exist?(@folders_file)
  File.open(@db_file, 'w'){|f| f.write('{}')} unless File.exist?(@db_file)
end

.listObject



42
43
44
45
46
47
# File 'lib/kp_folder.rb', line 42

def self.list
  File.open(@folders_file, 'r')do |f|
    arr = JSON.parse(f.read)
    arr.each {|item| puts item}
  end
end

.remove(folder) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kp_folder.rb', line 49

def self.remove(folder)
  arr = []
  File.open(@folders_file, 'r')do |f|
    arr = JSON.parse(f.read)
  end
  if arr.delete(folder)
    File.open(@folders_file, 'w')do |f|
      f.write(JSON.generate(arr))
    end
    puts "success!remove #{ folder }"
  else
    puts "failed to remove #{ folder }"
  end
end

.start(opts = {}) ⇒ Object

1.upload local files that aren’t in file ‘.db’,and update .db 2.clear remote files



79
80
81
82
83
84
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
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kp_folder.rb', line 79

def self.start(opts={})
  filter = Regexp.compile(filter) if (filter = opts.delete(:filter))
  @local_files = []
  File.open(@folders_file, 'r'){|f| @folders = JSON.parse(f.read)}
  @folders.each do |folder|
    files_in_folder(folder)
  end
  
  File.open(@db_file, 'r')do |f|
    @db = JSON.parse(f.read)
  end
  @db_tmp = {}
  @local_files.each do |file_name|
    next if filter && (filter =~ File.basename(file_name))
    m = %r{/.*/}.match(file_name)
    file = File.open(file_name, 'rb')
    if @db[file_name] == nil 
      puts "upload file: \t\t#{ file_name }"
      k_session.upload_file(file, :path => m[0])
      @db_tmp[file_name] = k_session.(file_name)
    elsif @db[file_name]['size'] != File.size(file_name)
      k_session.delete(file_name)
      puts "update file: \t\t#{ file_name }"
      k_session.upload_file(file, :path => m[0])
      @db_tmp[file_name] = k_session.(file_name)
    else
      @db_tmp[file_name] = @db[file_name]
    end
  end
  @db = @db_tmp
  File.open(@db_file, 'w'){|f| f.write(JSON.generate(@db))}
  
  @files_remote = {}
  @folders.each do |folder|
    check_folder(folder)
  end
  
  diff = @files_remote.select {|k,_| @db[k] == nil }
  diff.each do |k,_| 
    k_session.delete(k) 
    puts "clear remote file: \t#{ k }"
  end
end