Class: PhotoHelper::Smugmug

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/photo-helper/smugmug.rb

Instance Method Summary collapse

Instance Method Details

#albumsObject



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
# File 'lib/photo-helper/smugmug.rb', line 39

def albums
  @smugmug = SmugmugAPI.new
  albums = @smugmug.albums_long

  albums_tree = {}
  output = ['# Photos']

  albums.each do |a|
    parts = a[:path].split('/')
    next if parts[0] == 'Trash'

    album_name = parts.pop
    parts.each_with_index do |part, i|
      if i == 0
        albums_tree[part] ||= {}
      else
        parts[0..(i - 1)].inject(albums_tree, :fetch)[part] ||= {}
      end
    end

    parts[0..-1].inject(albums_tree, :fetch)[album_name] = "[#{a[:name]}](#{a[:web_uri]})"
  end

  # depth first search
  stack = albums_tree.keys.map { |a| [a] }
  stack.sort_by! do |key|
    next key.first.to_i if key.first =~ /^\d+$/
    next Float::INFINITY
  end

  until stack.empty?
    key = stack.pop
    item = key.inject(albums_tree, :fetch)
    next if key.first == 'dl'

    if item.is_a?(Hash)
      stack.concat(item.keys.map { |a| key.clone.push(a) })
      output.push("#{'#' * key.count} #{key.last}")
      next
    end

    begin
      dl_item = ['dl'].concat(key).inject(albums_tree, :fetch)
      output.push("  **Selects: ** #{item}\n  **All: ** #{dl_item}")
    rescue
      output.push(item)
    end
  end

  puts output.join("\n\n")
end

#oauthObject



34
35
36
# File 'lib/photo-helper/smugmug.rb', line 34

def oauth
  SmugmugAPI.new.request_access_token
end

#sync(folder = nil, album_name = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/photo-helper/smugmug.rb', line 17

def sync(folder = nil, album_name = nil)
  search_path = File.expand_path(folder)

  if options[:recursive]
    SmugmugAlbumHelper.recursive_sync(search_path)
    return
  end

  if album_name
    @smugmug = SmugmugAlbumHelper.new(search_path, album_name)
    @smugmug.upload_dl(album_name)
  else
    SmugmugAlbumHelper.sync(search_path)
  end
end