Class: Squarepusher::Client
- Inherits:
-
Object
- Object
- Squarepusher::Client
- Defined in:
- lib/squarepusher/core.rb
Class Method Summary collapse
- .from_config_path(path, args = {}) ⇒ Object
- .from_config_stream(stream, args = {}) ⇒ Object
- .parse_config_stream(stream) ⇒ Object
Instance Method Summary collapse
- #download_favorites(output_dir, page = 1, &photo_call) ⇒ Object
- #download_photoset(photoset, output_dir) ⇒ Object
- #each_photoset ⇒ Object
- #get_photo(photo_id) ⇒ Object
- #get_photos_for_set(pset_id, params = {}) ⇒ Object
- #get_photoset(pset_id) ⇒ Object
-
#initialize(key, secret, token, token_secret, args = {}) ⇒ Client
constructor
optional items for args: :overwrite - overwrite local files :verbose - verbose logging :privacy - privacy level for requests :event_listener - listener object that must provide “event” method.
- #random_contact ⇒ Object
- #random_favorite_of_contact(contact_id, args = {}) ⇒ Object
- #random_favorite_of_mine(args = {}) ⇒ Object
- #search_by_tags(user_id, tags, args = {}) ⇒ Object
Constructor Details
#initialize(key, secret, token, token_secret, args = {}) ⇒ Client
optional items for args: :overwrite - overwrite local files :verbose - verbose logging :privacy - privacy level for requests :event_listener - listener object that must provide “event” method
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/squarepusher/core.rb', line 116 def initialize(key, secret, token, token_secret, args={}) @overwrite = args[:overwrite] || false @verbose = args[:verbose] || false @privacy = args[:privacy] || 5 @event_listener = args[:event_listener] || nil if not @event_listener.nil? if not @event_listener.respond_to?(:event) puts "ignoring event listener #{@event_listener}; does not provide :event method" @event_listener = nil else puts "using event listener #{@event_listener}" end end FlickRawOptions['timeout'] = args[:timeout] || 5 require 'flickraw' FlickRaw.api_key = key FlickRaw.shared_secret = secret # flickr.auth.checkToken(:auth_token => token) flickr.access_token = token flickr.access_secret = token_secret size = args[:size] || :small_square # TODO: use url_for() method @url_for_photo = case size when :original lambda { |p| FlickRaw.url_o(p) } when :large lambda { |p| FlickRaw.url_b(p) } when :medium_640 lambda { |p| FlickRaw.url_z(p) } when :medium_500 lambda { |p| FlickRaw.url(p) } when :small lambda { |p| FlickRaw.url_m(p) } when :thumb lambda { |p| FlickRaw.url_t(p) } when :small_square lambda { |p| FlickRaw.url_s(p) } else raise Exception("unrecognized size: #{size}") end end |
Class Method Details
.from_config_path(path, args = {}) ⇒ Object
75 76 77 78 79 |
# File 'lib/squarepusher/core.rb', line 75 def from_config_path(path, args={}) open(path) do |stream| from_config_stream(stream, args) end end |
.from_config_stream(stream, args = {}) ⇒ Object
81 82 83 84 |
# File 'lib/squarepusher/core.rb', line 81 def from_config_stream(stream, args={}) key, secret, token, token_secret = parse_config_stream(stream) Squarepusher::Client.new(key, secret, token, token_secret, args) end |
.parse_config_stream(stream) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/squarepusher/core.rb', line 86 def parse_config_stream(stream) parsed = YAML.load(stream) if not parsed.respond_to?(:has_key?) raise Exception.new("invalid YAML in #{path}; parsed #{parsed.inspect}") end puts "parsed config: #{parsed.inspect}" required_keys = ['key', 'secret', 'token', 'token_secret'] missing_keys = [] vals = [] required_keys.each do |k| v = parsed.fetch(k, nil) if not v.nil? vals << v else missing_keys << k end end if not missing_keys.empty? raise Exception.new("missing keys in #{path}: #{missing_keys.inspect}") end vals end |
Instance Method Details
#download_favorites(output_dir, page = 1, &photo_call) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/squarepusher/core.rb', line 232 def download_favorites(output_dir, page=1, &photo_call) puts "page: #{page}" count = 0 flickr.favorites.getList(:page => page, :extras => 'owner_name, original_format').each do |p| ext = if p.respond_to?(:original_format) then p.originalformat else "jpg" end path = "#{output_dir}/#{p.owner}-#{p.id}.#{ext}" count += 1 next if not @overwrite and File.exists?(path) u = Squarepusher.url_for(p, :medium_640) download_image(u, path) if block_given? then photo_call.call(p, u, path) end end puts "count: #{count}" if count == 100 then download_favorites(output_dir, page+1, &photo_call) end end |
#download_photoset(photoset, output_dir) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/squarepusher/core.rb', line 248 def download_photoset(photoset, output_dir) dirname = Squarepusher.normalize(photoset.title) set_dir = File.join(output_dir, dirname) $fileutils.mkdir_p set_dir # handles socket timeout when loading photoset info results = {} status, photoset_result = handle_error { flickr.photosets.getPhotos(:photoset_id => photoset.id, :privacy_filter => @privacy, :extras => "original_format,url_o")["photo"] } if status == :error error photoset_result results[status] = 1 return results else photos = photoset_result end photos.each do |p| # TODO: use name of file in url since titles can be duplicate # copies string? name = "#{p.id}" if not p.title.empty? normalized_title = Squarepusher.normalize(p.title) name << "-#{normalized_title}" end url = @url_for_photo[p] # TODO: only add .jpg suffix if it's not in the image name already path = File.join(set_dir, name) path << ".jpg" if not path =~ /.jpg$/ if not @overwrite and File.exists?(path) debug "#{path} exists; skipping" result = :exists else result = download_image(url, path) end if results.has_key?(result) results[result] = results[result] + 1 else results[result] = 1 end end results end |
#each_photoset ⇒ Object
179 180 181 182 183 |
# File 'lib/squarepusher/core.rb', line 179 def each_photoset flickr.photosets.getList.each do |pset| yield pset end end |
#get_photo(photo_id) ⇒ Object
166 167 168 |
# File 'lib/squarepusher/core.rb', line 166 def get_photo(photo_id) flickr.photos.getInfo(:photo_id => photo_id) end |
#get_photos_for_set(pset_id, params = {}) ⇒ Object
174 175 176 177 |
# File 'lib/squarepusher/core.rb', line 174 def get_photos_for_set(pset_id, params = {}) params[:photoset_id] = pset_id flickr.photosets.getPhotos(params) end |
#get_photoset(pset_id) ⇒ Object
170 171 172 |
# File 'lib/squarepusher/core.rb', line 170 def get_photoset(pset_id) flickr.photosets.getInfo(:photoset_id => pset_id) end |
#random_contact ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/squarepusher/core.rb', line 191 def random_contact contact_hash = flickr.contacts.getList().to_hash() pages = contact_hash["pages"].to_i puts "WARNING - #{pages} pages of contacts found - only using first one" if pages > 1 contacts = contact_hash["contact"] contacts[rand(contacts.size)] end |
#random_favorite_of_contact(contact_id, args = {}) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/squarepusher/core.rb', line 215 def random_favorite_of_contact(contact_id, args={}) args[:user_id] = contact_id faves = flickr.favorites.getPublicList(args) raise Exception.new("no favorites found") if faves.size == 0 faves_hash = faves.to_hash # puts "hash: #{faves_hash.inspect}" photos = faves_hash["photo"] pages = faves_hash["pages"] fire_event(:domain => :flickr, :user => contact_id, :favorite_page_count => pages) choice = photos[rand(photos.size)] choice end |
#random_favorite_of_mine(args = {}) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/squarepusher/core.rb', line 199 def random_favorite_of_mine(args={}) faves = flickr.favorites.getList(args) raise Exception.new("no favorites found") if faves.size == 0 faves_hash = faves.to_hash # puts "hash: #{faves_hash.inspect}" photos = faves_hash["photo"] pages = faves_hash["pages"] fire_event(:domain => :flickr, :user => :self, :favorite_page_count => pages) choice = photos[rand(photos.size)] choice end |
#search_by_tags(user_id, tags, args = {}) ⇒ Object
185 186 187 188 189 |
# File 'lib/squarepusher/core.rb', line 185 def (user_id, , args={}) args[:user_id] = user_id args[:tags] = return flickr.photos.search(args) end |