This repository was archived by the owner on Nov 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGuardfile
More file actions
78 lines (68 loc) · 2.52 KB
/
Guardfile
File metadata and controls
78 lines (68 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
require 'guard/guard'
require 'koala'
require 'fileutils'
require 'pathname'
module RunOnAdditions
def on_add(method_name)
define_method :run_on_additions do |paths|
paths.each do |path|
send(method_name, path)
end
end
end
end
module ::Guard
class Watermarker < Guard
extend RunOnAdditions
on_add :do_watermark
def do_watermark(path)
pn = Pathname.new(path)
dest = pn.dirname + "../watermarked/" + pn.basename
watermark_path = File.expand_path('../watermark.jpg', __FILE__)
bendylogo_path = File.expand_path('../photos/bendyworks-logo.png', __FILE__)
watermark_size = `identify -format "%G" #{watermark_path}`.split("x").map(&:to_i)
logo_size = `identify -format "%G" #{bendylogo_path}`.split("x").map(&:to_i)
image_size = `identify -format "%G" #{path}`
watermark_width = (image_size.split("x").last.to_i * 0.2).to_i
watermark_height = (watermark_width.to_f * watermark_size[1].to_f / watermark_size[0]).to_i
logo_width = (image_size.split("x").last.to_i * 0.25).to_i
logo_height = (logo_width.to_f * logo_size[1].to_f / logo_size[0]).to_i
puts "watermarking #{path} to #{dest}"
`convert -composite #{path} #{watermark_path} -geometry #{watermark_width}x#{watermark_height}+50+1000 -depth 8 #{path}`
`convert -composite #{path} #{bendylogo_path} -geometry #{logo_width}x#{logo_height}+1575+1190 -depth 8 #{dest}`
end
end
class FacebookUpload < Guard
extend RunOnAdditions
on_add :add_to_facebook
def initialize(watchers = [], options = {})
super
access_token = ENV['ACCESS_TOKEN']
raise "No Access Token" unless access_token
@album_id = ENV['ALBUM_ID']
raise "No Album Id" unless @album_id
@graph = Koala::Facebook::API.new(access_token)
end
def add_to_facebook(path)
puts "adding #{path} to Facebook"
resp = @graph.put_picture(path, {caption: "Taken on #{path_to_time(path)}"}, @album_id)
puts "got #{resp}"
dest = path.sub('/watermarked/', '/uploaded/')
FileUtils.mv(path, dest)
end
def path_to_time path
m = path.match(/[^-]*-(\d{4})-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)/)
year, month, day, hour_mil, min, sec = m[1], m[2], m[3], m[4], m[5], m[6]
time = Time.new(year, month, day, hour_mil, min, sec)
time.strftime '%B %e, %Y at %l:%M:%S %p'
end
end
end
group :camera do
guard :watermarker do
watch %r{photos/raw/(.*)}
end
guard :facebook_upload do
watch %r{photos/watermarked/(.*)}
end
end