Class: Squarepusher::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/squarepusher/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.error_msg(msg) ⇒ Object



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

def error_msg(msg)
  $stderr.puts "ERROR - #{msg}"
end

.fail(msg, code = 1) ⇒ Object



16
17
18
19
# File 'lib/squarepusher/cli.rb', line 16

def fail(msg, code=1)
  error_msg(msg)
  exit code
end

.mainObject



21
22
23
24
25
26
27
28
29
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/squarepusher/cli.rb', line 21

def main
  actions = {}

  actions[:get_full_token] = lambda do |client, args|
    puts "hi"
  end
  
  actions[:list_sets] = lambda do |client, args|
    client.each_photoset do |pset|
      puts Squarepusher.describe_photoset(pset)
    end
  end

  actions[:grab_all_sets] = lambda do |client, args|
    if args.size != 1
      fail("expected: <output_dir>")
    end
  
    output_dir = args[0]
  
    total_results = {}
    client.each_photoset do |pset|
      pset_description = Squarepusher.describe_photoset(pset)
      puts "[set] #{pset_description}"
      pset_results = client.download_photoset(pset, output_dir)
      puts "[results] #{pset_description} #{pset_results.inspect}"
      total_results[pset_description] = pset_results
      puts
    end
  
    total_results.each_pair do |k, v|
      puts "#{k}: #{v.inspect}"
    end
  end

  actions[:grab_set] = lambda do |client, args|
    if args.size != 2
      fail("expected: <photoset_id> <output_dir>")
    end
  
    pset_id, output_dir = args
    pset = client.get_photoset(pset_id)
    results = client.download_photoset(pset, output_dir)
    puts results.inspect
  end

  actions[:find_set] = lambda do |client, args|
    if args.size != 1
      fail("expected: <photoset_id>")
    end
  
    pset_id, output_dir = args
    pset = client.get_photoset(pset_id)
    puts Squarepusher.describe_photoset(pset)
  end


  def action_str(actions)
    actions.keys.join(",")
  end

  def size_str
    Squarepusher.sizes.join(",")
  end

  options = { :size => :original }
  args = nil
  action = nil
  client = nil
  config_path = "#{ENV['HOME']}/.squarepusher.yaml"
  OptionParser.new do |opts|
    opts.banner = "USAGE: squarepusher [options] <action:(#{action_str(actions)})> [args]"

    opts.on("-c", "--config PATH", "path to config file (defaults to #{config_path})") do |v|
      options[:config] = v
    end

    opts.on("-o", "--overwrite", "overwrite already downloaded files") do |v|
      options[:overwrite] = v
    end

    opts.on("-p", "--privacy-filter LEVEL", "privacy level of photos to download") do |v|
      options[:privacy] = v
    end

    opts.on("-s", "--size SIZE", "size of photos to download (#{size_str()})") do |v|
      options[:size] = v.to_sym
    end
  
    opts.on("-v", "--verbose", "log verbosely") do |v|
      options[:verbose] = v
    end
  
    opts.parse!(ARGV)
  
    if ARGV.size < 1
      $stderr.puts opts
      exit 1
    end
  
    action = ARGV.delete_at(0)
    args = ARGV
  
    action = action.to_sym
    if not actions.has_key?(action)
      $stderr.puts opts
      exit 2
    end
  
    config_path = options[:config] || config_path
    if not File.exists?(config_path)
      fail "#{config_path} not found" 
    else
      puts "parsing #{config_path}"
    end
    
    # client = Squarepusher::Client.from_config_path(key, secret, token, token_secret, options)
    client = Squarepusher::Client.from_config_path(config_path, options)
  end
  
  puts "args: #{args.inspect}"
  actions[action].call(client, args || [])
end

Instance Method Details

#action_str(actions) ⇒ Object



78
79
80
# File 'lib/squarepusher/cli.rb', line 78

def action_str(actions)
  actions.keys.join(",")
end

#size_strObject



82
83
84
# File 'lib/squarepusher/cli.rb', line 82

def size_str
  Squarepusher.sizes.join(",")
end