squarepusher 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/squarepusher CHANGED
@@ -12,27 +12,82 @@ def fail(msg, code=1)
12
12
  exit code
13
13
  end
14
14
 
15
+
16
+ actions = {}
17
+
18
+ actions[:list_sets] = lambda do |client, args|
19
+ client.each_photoset do |pset|
20
+ puts Squarepusher.describe_photoset(pset)
21
+ end
22
+ end
23
+
24
+ actions[:grab_all_sets] = lambda do |client, args|
25
+ if not args
26
+ fail("expected: <output_dir>")
27
+ end
28
+
29
+ client.each_photoset do |pset|
30
+ puts Squarepusher.describe_photoset(pset)
31
+ results = client.download_photoset(pset, output_dir)
32
+ puts results.inspect
33
+ end
34
+ end
35
+
36
+ actions[:grab_set] = lambda do |client, args|
37
+ if args.size != 2
38
+ fail("expected: <photoset_id> <output_dir>")
39
+ end
40
+
41
+ pset_id, output_dir = args
42
+ pset = client.get_photoset(pset_id)
43
+ client.download_photoset(pset, output_dir)
44
+ end
45
+
46
+ actions[:find_set] = lambda do |client, args|
47
+ if args.size != 1
48
+ fail("expected: <photoset_id>")
49
+ end
50
+
51
+ pset_id, output_dir = args
52
+ pset = client.get_photoset(pset_id)
53
+ puts Squarepusher.describe_photoset(pset)
54
+ end
55
+
56
+
57
+ def action_str(actions)
58
+ actions.keys.join(", ")
59
+ end
60
+
61
+
15
62
  options = { :size => :original }
63
+ args = nil
64
+ action = nil
65
+ client = nil
16
66
  OptionParser.new do |opts|
17
- opts.banner = "USAGE: squarepusher [options] <api-key> <api-secret> <token> <output_dir>"
67
+ opts.banner = "USAGE: squarepusher [options] <api-key> <api-secret> <token> <action:(#{action_str(actions)})> [args]"
18
68
 
19
- opts.on("-s", "--size SIZE", "secret") do |v|
69
+ # TODO: list sizing options
70
+ opts.on("-s", "--size SIZE", "size of photos to download (using flickr terminology)") do |v|
20
71
  options[:size] = v.to_sym
21
72
  end
22
73
 
23
74
  opts.parse!(ARGV)
24
75
 
25
- if ARGV.size != 4
76
+ if ARGV.size < 4
26
77
  $stderr.puts opts
27
78
  exit 1
28
79
  end
80
+
81
+ key, secret, token, action = ARGV.slice!(0...4)
82
+ args = ARGV
83
+
84
+ action = action.to_sym
85
+ if not actions.has_key?(action)
86
+ $stderr.puts opts
87
+ exit 2
88
+ end
89
+
90
+ client = Squarepusher::Client.new(key, secret, token, options)
29
91
  end
30
92
 
31
- key, secret, token, output_dir = ARGV
32
-
33
- client = Squarepusher::Client.new(key, secret, token, options)
34
-
35
- client.each_photoset do |pset|
36
- puts pset.inspect
37
- client.download_photoset(pset, output_dir)
38
- end
93
+ actions[action].call(client, args || [])
data/lib/squarepusher.rb CHANGED
@@ -5,8 +5,21 @@ FlickRawOptions = {}
5
5
 
6
6
  $fileutils = FileUtils::Verbose
7
7
 
8
+ # TODO: detect "photo unavailable" and don't download image
9
+ UNAVAILABLE_URL = 'http://l.yimg.com/g/images/photo_unavailable.gif'
10
+
11
+ # TODO: keep track of failures
12
+
8
13
  module Squarepusher
9
14
 
15
+ class << self
16
+
17
+ def describe_photoset(pset)
18
+ "#{pset.id} #{pset.title}"
19
+ end
20
+
21
+ end
22
+
10
23
  class Client
11
24
 
12
25
  def initialize(key, secret, token, args={})
@@ -44,6 +57,10 @@ module Squarepusher
44
57
  end
45
58
  end
46
59
 
60
+ def get_photoset(pset_id)
61
+ flickr.photosets.getInfo(:photoset_id => pset_id)
62
+ end
63
+
47
64
  def each_photoset
48
65
  flickr.photosets.getList.each do |pset|
49
66
  yield pset
@@ -54,6 +71,7 @@ module Squarepusher
54
71
  set_dir = File.join(output_dir, photoset.title.gsub(/\s+/, '-'))
55
72
  $fileutils.mkdir_p set_dir
56
73
 
74
+ results = {}
57
75
  photos = flickr.photosets.getPhotos(:photoset_id => photoset.id, :extras => "original_format,url_o")["photo"]
58
76
  photos.each do |p|
59
77
  # puts p.inspect
@@ -61,19 +79,37 @@ module Squarepusher
61
79
 
62
80
  url = @url_for_photo[p]
63
81
 
64
- path = File.join(set_dir, "#{name}.jpg")
82
+ # TODO: only add .jpg suffix if it's not in the image name already
83
+ path = File.join(set_dir, "#{name}")
84
+ path << ".jpg" if not path =~ /.jpg$/
85
+
65
86
  if File.exists?(path)
66
87
  puts "#{path} exists; skipping"
67
88
  else
68
- download_image(url, path)
89
+ result = download_image(url, path)
90
+ if results.has_key?(result)
91
+ results[result] = results[result] + 1
92
+ else
93
+ results[result] = 1
94
+ end
69
95
  end
70
96
  end
97
+ results
71
98
  end
72
99
 
73
100
  private
74
101
 
75
- def download_image(url, path)
76
- puts "#{url} -> #{path}"
102
+ def download_image(url, path, args={})
103
+ if url == UNAVAILABLE_URL
104
+ redirects = args[:redirects]
105
+ msg = "#{url} unavailable"
106
+ msg << "; redirects: #{redirects}" if not redirects.nil?
107
+ puts msg
108
+ return :unvailable
109
+ end
110
+
111
+ result = :unknown
112
+ # puts url
77
113
  uri = URI.parse(url)
78
114
  Net::HTTP.start(uri.host, uri.port) do |http|
79
115
  full_path = uri.request_uri
@@ -82,18 +118,24 @@ module Squarepusher
82
118
  # puts response.inspect
83
119
  case response
84
120
  when Net::HTTPError
121
+ result = :error
85
122
  when Net::HTTPFound
123
+ redirects = args[:redirects] || []
124
+ redirects << url
86
125
  location = response["location"]
87
126
  if not location =~ /^http.*/
88
127
  location = "#{uri.scheme}://#{uri.host}:#{uri.port}#{location}"
89
128
  end
90
- download_image(location, path)
129
+ download_image(location, path, :redirects => redirects)
91
130
  else
131
+ puts "#{url} -> #{path}"
92
132
  open(path, 'w') do |out|
93
133
  out << response.body
94
134
  end
135
+ result = :success
95
136
  end
96
137
  end
138
+ return result
97
139
  end
98
140
 
99
141
  end
@@ -1,3 +1,3 @@
1
1
  module Squarepusher
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: squarepusher
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - mhawthorne