Commits

Jonathan Gilbert committed 77a50c4

Now archives photos in sets based on set creation date

  • Participants
  • Parent commits 14b95a3

Comments (0)

Files changed (3)

File lib/flickrup/ext/tag_sets.rb

 
     if sets.length == 1
       ctx.properties[:subdir] = sets[0]
+      set_date = @flickr.date_of_set(sets[0])
+      ctx.properties[:date_taken] = set_date
     end
     @delegate.archive(ctx)
   end

File lib/flickrup/flickr_client.rb

 
   def initialize(api_key, shared_secret)
     @set_name_to_id = {}
+    @set_id_to_date = {}
     FlickRaw.api_key= api_key
     FlickRaw.shared_secret=shared_secret
 
     client.upload_photo(image, config)
   end
 
-  def add_to_set(photo_id, set_name)
-    unless @set_name_to_id.include? set_name
-      sets_response = client.photosets.getList #if we don't have it, refresh
-      @set_name_to_id = Hash[*sets_response.to_a.map{ |set|
-        [set['title'], set['id']]
-      }.flatten]
+  def date_of_set(set_name)
+    id = id_from_cache(set_name)
 
-      if @set_name_to_id.include? set_name
-        client.photosets.addPhoto(:photoset_id => @set_name_to_id[set_name], :photo_id => photo_id)
-      else #if we still don't, create it
-        created = client.photosets.create(:title => set_name, :primary_photo_id => photo_id)
-        @set_name_to_id = @set_name_to_id.merge({set_name => created.id})
-      end
+    unless @set_id_to_date.include? id
+      info = client.photosets.getInfo(:photoset_id => id)
+      @set_id_to_date[id] = DateTime.strptime(info.date_create, '%s').new_offset(DateTime.now.offset)
     end
 
-    @set_name_to_id[set_name]
+    return @set_id_to_date[id]
+  end
+
+  def add_to_set(photo_id, set_name)
+    id = id_from_cache(set_name)
+
+    if id
+      client.photosets.addPhoto(:photoset_id => @set_name_to_id[set_name], :photo_id => photo_id)
+    else #if we still don't, create it
+      created = client.photosets.create(:title => set_name, :primary_photo_id => photo_id)
+      @set_name_to_id = @set_name_to_id.merge({set_name => created.id})
+    end
   end
 
   def get_token
   end
 
   private
+
+  def id_from_cache(set_name)
+    unless @set_name_to_id.include? set_name
+      sets_response = client.photosets.getList #if we don't have it, refresh
+      @set_name_to_id = Hash[*sets_response.to_a.map { |set|
+        [set['title'], set['id']]
+      }.flatten]
+
+    end
+
+    @set_name_to_id[set_name]
+  end
+
   def set_client(client)
     @client = client
   end

File lib/flickrup/processor.rb

   def get_uploader
 
     unless @uploader
-      flickr_client = FlickrClient.new(@config[:api_key], @config[:shared_secret])
 
-      context = {:flickr_client => flickr_client}
+      context = get_chain_context()
 
-      uploader = Uploader.new(flickr_client)
+      uploader = Uploader.new(context[:flickr_client])
 
       @uploader = upload_handlers.reduce(uploader) do |result, clazz|
         clazz.new(@config, result, context)
   def get_archiver
     unless @archiver
       @archiver = archive_handlers.reduce(Archiver.new) do |result, clazz|
-        clazz.new(@config, result, {})
+        clazz.new(@config, result, get_chain_context)
       end
     end
     return @archiver
   end
 
+  def get_chain_context
+    unless @chain_context
+      flickr_client = FlickrClient.new(@config[:api_key], @config[:shared_secret])
+      @chain_context = {:flickr_client => flickr_client}
+    end
+
+    return @chain_context
+  end
+
   def get_preupload_handlers
     [MachineTags.new(@config, {}), ReplacementTags.new(@config)]
   end