Class: Bookscan::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/bookscan/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd_options, options) ⇒ Commands

Returns a new instance of Commands.



17
18
19
20
21
22
23
24
25
# File 'lib/bookscan/commands.rb', line 17

def initialize(cmd_options,options)
  @options = options
  @command_options = cmd_options
  @agent = Agent.new
  @cache_file =  ENV['HOME']+"/.bookscan.cache"
  @cache = Cache.new(@cache_file)
  @help = false
  @banner = ""
end

Instance Method Details

#downloadObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/bookscan/commands.rb', line 159

def download
  opt = OptionParser.new
  directory = "."
  hash = nil;type = nil;pattern = ".*"; dry_run = false
  opt.on('-d DIR','--directory=DIR', 'download directory') do |v|
    directory = v
  end
  opt.on('-g HASH','--group=HASH', 'group hash') do |v|
    hash = v
  end
  opt.on('-t TYPE','--tuned=TYPE', 'download tuned') do |v|
    type = v
  end
  opt.on('-m PATTERN','--match=PATTERN','pattern match') do |v|
    pattern  = v
  end
  opt.on('--dry-run', 'dry-run mode') do |v|
    dry_run = true
  end
  opt.parse!(@command_options)
  @banner = "[command options] isbn|all"
  return opt if @help

  book_id = @command_options.shift

  if book_id == "all"
    if type
      bs = @cache.tuned.select { |i|
        /#{pattern}/ =~ i.title
      }
    else
      bs = @cache.books.select { |i|
        /#{pattern}/ =~ i.title
      }
    end
    bs.each { |book|
      next unless book.tune_type == type
      if Dir.glob(directory + "/**/*" + book.book_id + "*.pdf").length == 0
        path = directory + "/" +book.filename
        puts "=> " + path
        unless dry_run
          start
          @agent.download(book.url,path)
        end
      end
    }
  else
    if type
      book = ask_tuned_book_id(book_id,type,pattern)
    else
      book = ask_book_id(book_id,hash,pattern)
    end
    
    # download
    path = directory + "/" +book.filename
    puts "download: " + path
    unless dry_run
      start
      @agent.download(book.url,path)
    end
  end

end

#groupsObject



125
126
127
128
129
130
131
132
# File 'lib/bookscan/commands.rb', line 125

def groups
  opt = OptionParser.new
  opt.parse!(@command_options)
  @banner = ""
  return opt if @help
  gs = @cache.groups
  puts gs.to_s
end

#helpObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bookscan/commands.rb', line 27

def help
  opt = OptionParser.new
  opt.parse!(@command_options)
  @banner = "command"
  return opt if @help
  @help = true
  command = @command_options.shift
  raise "Unknown command: " + command unless COMMANDS.include?(command)
  opt = send(command)
  opt.banner="Usage: bookscan [options] #{command} #{@banner}"
  puts opt

end

#listObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bookscan/commands.rb', line 134

def list
  gs = @cache.groups

  opt = OptionParser.new
  hash = nil;type = nil;pattern = ".*"
  browsing = true;
  opt.on('-g HASH','--group=HASH', 'group hash') { |v|  hash = v; browsing = false }
  opt.on('-t TYPE','--tuned=TYPE', 'download tuned') { |v| type = v; browsing = false }
  opt.on('-m PATTERN','--match=PATTERN','pattern match') { |v| pattern  = v; browsing = false }
  opt.parse!(@command_options)
  @banner = "[command options]"
  return opt if @help

  if hash or browsing
    puts ask_group(hash,gs).books.to_s
  else
    if type
      bs = @cache.tuned.delete_if { |i| !(/#{pattern}/ =~ i.title) or i.tune_type != type }
    else
      bs = @cache.books.delete_if { |i| !(/#{pattern}/ =~ i.title) }
    end
    puts bs.to_s
  end
end

#loginObject



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
# File 'lib/bookscan/commands.rb', line 41

def 
  opt = OptionParser.new
  opt.parse!(@command_options)
  @banner = ""
  return opt if @help

  email = ask('Enter email: ') do |q|
    q.validate = /\w+/
  end

  ok = false
  while(!ok) 
    password = ask("Enter your password: ") do |q|
      q.validate = /\w+/
      q.echo = false
    end

    @agent.(email,password)
    if @agent.login? # => OK
      ok = true
      Keystorage.set("bookscan",email,password)
      puts "login OK"
    else
      puts "password mismatch"
    end

  end
end

#logoutObject



70
71
72
73
74
75
76
77
# File 'lib/bookscan/commands.rb', line 70

def logout
  opt = OptionParser.new
  opt.parse!(@command_options)
  @banner = ""
  return opt if @help

  Keystorage.delete("bookscan")
end

#tuneObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bookscan/commands.rb', line 223

def tune
  opt = OptionParser.new
  hash = nil; pattern = ".*"; dry_run = false; to = nil
  opt.on('-g HASH','--group=HASH', 'group hash') do |v|
    hash = v
  end
  opt.on('--dry-run', 'dry-run mode') do |v|
    dry_run = true
  end
  opt.on('-m PATTERN','--match=PATTERN','pattern match') do |v|
    pattern  = v
  end
  opt.on('--to=dest', 'file to external storage: e.g. Dropbox') do |v|
    to = v
  end
  opt.parse!(@command_options)
  @banner = "[command options] isbn|all tune_type"
  return opt if @help

  book_id = @command_options.shift
  type = ask_tune_type(@command_options.shift)

  if book_id == "all"
    start unless dry_run
    unless dry_run
      @cache.transaction do |cache|
        cache["tuned"] = @agent.tuned
      end
    end
    @cache.books(hash).each { |book|
      next unless /#{pattern}/ =~ book.title
      unless @cache.tuned?(book,type)
        # tune
        puts "tune for %s: %s" % [type, book.title] if dry_run or @agent.tune(book,{:type => type, :to => to, :is_premium => true})
        # puts "tune for %s: %s" % [type, book.title]
      end
    }
  else
    book = ask_book_id(book_id,hash,pattern)
    # tune
    puts "tune for %s: %s" % [type, book.title]
    unless dry_run
      start
      @agent.tune(book,{:to => to,:type => type})
    end
  end
end

#tunedObject



282
283
284
285
286
287
288
# File 'lib/bookscan/commands.rb', line 282

def tuned
  opt = OptionParser.new
  opt.parse!(@command_options)
  @banner = ""
  return opt if @help
  puts @cache.tuned.to_s
end

#tuningObject



271
272
273
274
275
276
277
278
279
280
# File 'lib/bookscan/commands.rb', line 271

def tuning
  opt = OptionParser.new
  opt.parse!(@command_options)
  @banner = ""
  return opt if @help

  start
  books = @agent.tuning
  puts books.to_s
end

#updateObject



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
122
123
# File 'lib/bookscan/commands.rb', line 79

def update
  all = false
  hash = false

  opt = OptionParser.new
  opt.on('-a','--all', 'update all cache') do
    all = true
  end
  opt.on('-g HASH','--group=HASH', 'update group') do |v|
    hash = v
  end
  opt.parse!(@command_options)
  @banner = ""
  return opt if @help

  start
  gs = @agent.groups
  if all
    gs.each_index do |index|
      gs[index].books = @agent.books(gs[index])
    end
  elsif hash
    i = gs.to_index(hash)
    gs[i].books = @agent.books(gs[i]) if gs[i]
  else
    gs.each_index do |index|
      books = @cache.books(gs[index]) rescue {}

      if gs[index].completed?
        if (books == nil or books.length == 0 or !books.is_a?(Books))
          gs[index].books = @agent.books(gs[index])
        else
          gs[index].books = books
        end
      end

    end
  end
  ts  = @agent.tuned

  @cache.transaction do |cache|
    cache["groups"] = gs
    cache["tuned"] = ts
  end
end