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
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
|