Class: SmugmugAlbumHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/smugmug_album.rb

Constant Summary collapse

PATH_REGEX =
%r{^.+Pictures\/.+\/(\d{4})\/(\d{2})_.+\/[^_]+_([^\/]+)}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_path, album = nil) ⇒ SmugmugAlbumHelper

Returns a new instance of SmugmugAlbumHelper.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/helpers/smugmug_album.rb', line 37

def initialize(search_path, album = nil)
  @search_extensions = IMAGE_EXTENSIONS.concat(["XMP"])

  @search_path = Pathname.new(search_path)

  @smugmug = SmugmugAPI.new

  @album_name = album || album_name

  @album = @smugmug.get_or_create_album(@album_name, album_url: @location&.downcase)

  @dl_album_name = File.join("dl", @album_name)
  @dl_album = @smugmug.get_or_create_album(@dl_album_name, album_url: @location&.downcase)

  @folder_keywords = Set.new
end

Instance Attribute Details

#smugmug_apiObject

Returns the value of attribute smugmug_api.



8
9
10
# File 'lib/helpers/smugmug_album.rb', line 8

def smugmug_api
  @smugmug_api
end

Class Method Details

.recursive_sync(search_path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/helpers/smugmug_album.rb', line 17

def self.recursive_sync(search_path)
  folders = Dir[File.join(search_path, "*/")]
  folders.each do |folder|
    if SmugmugAlbumHelper.supported_folder?(folder)
      puts "Syncing #{folder}\n"
      sync(folder)
      puts "\n"
    else
      recursive_sync(folder)
    end
  end
end

.supported_folder?(search_path) ⇒ Boolean

KEYWORD_WHITELITS = %w(instagram exported)

Returns:

  • (Boolean)


13
14
15
# File 'lib/helpers/smugmug_album.rb', line 13

def self.supported_folder?(search_path)
  PATH_REGEX.match?(search_path)
end

.sync(search_path) ⇒ Object



30
31
32
33
34
35
# File 'lib/helpers/smugmug_album.rb', line 30

def self.sync(search_path)
  smugmug = SmugmugAlbumHelper.new(search_path)
  smugmug.upload_dl
  puts "\n"
  smugmug.collect_select
end

Instance Method Details

#album_nameObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/helpers/smugmug_album.rb', line 63

def album_name
  parse_path
  if @year && @month && @location
    @location = @location.gsub(/[-_]/, ' ')
    album_name_short = "#{@location} #{@month} #{@year}"
    File.join(@year, @month, album_name_short)
  else
    puts 'Unable to determine album from path'
  end
end

#collect_selectObject



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
270
# File 'lib/helpers/smugmug_album.rb', line 241

def collect_select
  @folder_keywords = Set.new

  pictures = image_list
  pictures = pictures.select { |p| ImageHelper.is_select?(p) }
  pictures = merge_exported(pictures)

  puts "Collecting selects to album #{@album_name} --> #{@album[:web_uri]}\n"

  @image_list = image_list_to_hash(pictures)
  @uploaded_hash ||= uploaded_to_hash(@album)
  @dl_uploaded_hash ||= uploaded_to_hash(@dl_album)

  to_collect = []

  @image_list.each do |filename, images|
    images.each do |image|
      next unless @dl_uploaded_hash.key?(filename)
      @dl_uploaded_hash[filename].each do |uploaded|
        next unless uploaded_match_requested?(image, uploaded)
        to_collect.push(uploaded[:uri]) unless to_collect.include? uploaded[:uri]
        break
      end
    end
  end

  # delete all images from album since we just collec them anyways which is cheap
  @smugmug.delete_images(@smugmug.images(@album[:id]))
  @smugmug.collect_images(to_collect, @album[:id])
end

#exported_listObject



78
79
80
# File 'lib/helpers/smugmug_album.rb', line 78

def exported_list
  Dir[File.join(@search_path, "/**/{Exported,exported}/*.*")]
end

#image_listObject



74
75
76
# File 'lib/helpers/smugmug_album.rb', line 74

def image_list
  Dir[File.join(@search_path, "/**/*.{#{@search_extensions.join(',')}}")].reject { |p| FileHelper.ingore_file?(p) }
end

#image_list_to_hash(images) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/helpers/smugmug_album.rb', line 105

def image_list_to_hash(images)
  image_list_hash = {}
  images.each do |i|
    filename = File.basename(i, ".*")

    # add keywords based on directory name and if the image is a select image
    keywords = image_dir_keywords(i) || []
    @folder_keywords.merge(keywords) if keywords.size

    # do this after the keywords list because keywords_list is actually just directory keywords...
    keywords.push("select") if ImageHelper.is_select?(i)

    push_hash_array(image_list_hash, filename, file: i,
      keywords: keywords,
      md5: Digest::MD5.file(i).hexdigest)
  end
  image_list_hash
end

#instagram_listObject



82
83
84
# File 'lib/helpers/smugmug_album.rb', line 82

def instagram_list
  Dir[File.join(@search_path, "/**/{Instagram,instagram}/*.*")]
end

#merge_exported(images = image_list, concat = false) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/helpers/smugmug_album.rb', line 86

def merge_exported(images = image_list, concat = false)
  exported = Dir["#{@search_path}/**/{Exported,exported}/*.*"]
  unless concat
    exported_basenames = exported.map { |p| File.basename(p, ".*") }
    images = images.reject { |p| exported_basenames.include? File.basename(p, ".*") }
  end
  images.concat(exported)
end

#parse_pathObject



54
55
56
57
58
59
60
61
# File 'lib/helpers/smugmug_album.rb', line 54

def parse_path
  if matches = "#{@search_path}/".to_s.match(PATH_REGEX)
    @album_root = Pathname.new(matches[0])
    @year = matches[1]
    @month = Date::MONTHNAMES[matches[2].to_i].capitalize
    @location = matches[3].split("_").map(&:capitalize).join(' ')
  end
end

#sync(album, image_list_hash, reject_trash = true, delete: true) ⇒ Object



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
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
# File 'lib/helpers/smugmug_album.rb', line 124

def sync(album, image_list_hash, reject_trash = true, delete: true)
  uploaded_hash = uploaded_to_hash(album)

  to_upload = {}
  to_update = {}
  to_update_keywords = {}
  to_delete = []

  image_list_hash.each do |filename, images|
    images.each do |image|
      next unless ImageHelper.is_jpeg?(image[:file])
      next if reject_trash && ImageHelper.color_class(image[:file]) == "Trash"

      upload_image = true

      if uploaded_hash.key?(filename)
        uploaded_hash[filename].each do |uploaded|
          if uploaded_match_requested?(image, uploaded)
            if uploaded[:md5] == image[:md5]
              # check for missing keywords using -
              if image[:keywords] - uploaded[:keywords] != []
                push_hash_array(to_update_keywords, image[:keywords], {image_uri: uploaded[:image_uri], keywords: uploaded[:keywords], filename: uploaded[:filename]})
              end
            else
              push_hash_array(to_update, image[:keywords], image.merge!(uri: uploaded[:uri]))
            end
            upload_image = false
            break
          end
        end
      end
  
      if upload_image
        push_hash_array(to_upload, image[:keywords], image[:file])
      end
    end
  end

  uploaded_hash.each do |filename, uploaded_images|
    uploaded_images.each do |uploaded|
      if image_list_hash.key?(filename)
        image_hash = image_list_hash[filename].find do |image|
          uploaded_match_requested?(image, uploaded)
        end

        if image_hash.nil?
          to_delete.push(uploaded)
          next
        end
        to_delete.push(uploaded) if reject_trash && ImageHelper.color_class(image_hash[:file]) == "Trash"
      else
        to_delete.push(uploaded)
      end
    end
  end

  to_upload.each do |keywords, images|
    upload(album, images, keywords)
  end

  to_update.each do |keywords, images|
    update(album, images, keywords)
  end

  to_update_keywords.each do |keywords, images|
    puts "Updating keywords #{keywords}"
    images.each do |i|
      @smugmug.update_keywords(i, keywords)
    end
  end

  
  # puts "delete #{to_delete.count}???"
  if delete && to_delete.any?
    puts "Deleting #{to_delete.count} images"
    trash_album_name = File.join("trash/#{album_name}")
    trash_album = @smugmug.get_or_create_album(trash_album_name)
    @smugmug.move_images(to_delete.map{ |u| u[:uri] }, trash_album[:id])
  end
end

#update(album, pictures, keywords = nil) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/helpers/smugmug_album.rb', line 214

def update(album, pictures, keywords = nil)
  puts "Updating #{pictures.count} jpegs"

  headers = {}
  headers["X-Smug-Keywords"] = keywords.join(",") unless keywords.nil?

  @smugmug.update_images(pictures, album[:id], headers, workers: 4, filename_as_title: true)
end

#upload(album, pictures, keywords = nil) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/helpers/smugmug_album.rb', line 205

def upload(album, pictures, keywords = nil)
  puts "Uploading #{pictures.count} jpegs"

  headers = {}
  headers["X-Smug-Keywords"] = keywords.join(",") unless keywords.nil?

  @smugmug.upload_images(pictures, album[:id], headers, workers: 4, filename_as_title: true)
end

#upload_dl(album_name_override = nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/helpers/smugmug_album.rb', line 223

def upload_dl(album_name_override = nil)
  album = if album_name_override
    # @smugmug.get_or_create_album(album_name)
    @smugmug.get_or_create_album(album_name_override)
  else
    @dl_album
  end

  @folder_keywords = Set.new
  puts "Uploading all images to album #{album_name || @album_name} --> #{album[:web_uri]}\n"

  @image_list = image_list_to_hash(image_list)
  @image_list = merge_hash_array(@image_list, image_list_to_hash(exported_list))
  @image_list = merge_hash_array(@image_list, image_list_to_hash(instagram_list))

  sync(album, @image_list, true)
end

#upload_selectObject



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/helpers/smugmug_album.rb', line 272

def upload_select
  @folder_keywords = Set.new

  pictures = image_list
  pictures = pictures.select { |p| ImageHelper.is_select?(p) }
  pictures = merge_exported(pictures)

  puts "Uploading selects to album #{@album_name} --> #{@album[:web_uri]}\n"

  @image_list = image_list_to_hash(pictures)

  sync(@album, @image_list, true)
end

#uploaded_to_hash(album) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/helpers/smugmug_album.rb', line 95

def uploaded_to_hash(album)
  uploaded = @smugmug.images(album[:id])
  uploaded_hash = {}
  uploaded.each do |u|
    filename = File.basename(u[:filename], ".*")
    push_hash_array(uploaded_hash, filename, u)
  end
  uploaded_hash
end