Class: Photoapp::Session

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

Constant Summary collapse

CONFIG_FILE =

relative to root

'photoapp.yml'
UPLOAD =
'upload'
'print'
ROOT =

where photos are stored

File.expand_path('~/cave.pics')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



29
30
31
32
33
# File 'lib/photoapp.rb', line 29

def initialize(options={})
  @photos = []
  @config = config(options)
  FileUtils.mkdir_p(config['reprint'])
end

Instance Attribute Details

#photosObject

Returns the value of attribute photos.



20
21
22
# File 'lib/photoapp.rb', line 20

def photos
  @photos
end

Returns the value of attribute print.



20
21
22
# File 'lib/photoapp.rb', line 20

def print
  @print
end

#uploadObject

Returns the value of attribute upload.



20
21
22
# File 'lib/photoapp.rb', line 20

def upload
  @upload
end

Instance Method Details

#config(options = {}) ⇒ Object



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

def config(options={})
  @config || begin

    options['source'] ||= root('import')

    config = {
      'source' => Dir.pwd, # where photos are located
      'url_base' => 'cave.pics',
      'watermark' => Photoapp.gem_dir('assets', 'watermark.png'),
      'font' => Photoapp.gem_dir('assets', "SourceSansPro-Semibold.ttf"),
      'font_size' => 36,
      'date_font_size' => 24,
      'config' => 'photoapp.yml',
      'upload' => 'upload',
      'upload_queue' => 'upload_queue',
      'print' => 'print',
      'print_duplicate' => false,
      'photos_import' => 'photos_import',
      'import_alt' => false,
      'reprint' => 'reprint',
      'interval' => 60
    }
 
    config_file = root(options['config'] || config['config'])

    config['source'] = options['source'] || config['source']

    if File.exist?(config_file)
      config.merge!(YAML.load(File.read(config_file)) || {})
    end

    config['upload']  = root(config['upload'])
    config['upload_queue']  = root(config['upload_queue'])
    config['print']   = root(config['print'])
    config['photos_import']  = root(config['photos_import'])
    config['reprint'] = root(config['reprint'])
    config['import_alt'] = File.expand_path(config['import_alt']) if config['import_alt']
    config['print_duplicate'] = File.expand_path(config['print_duplicate']) if config['print_duplicate']

    config
  end

end

#empty_print_queue?Boolean

Check to see if the print queue is empty

Returns:

  • (Boolean)


177
178
179
180
181
182
183
# File 'lib/photoapp.rb', line 177

def empty_print_queue?
  if printer = `lpstat -d`
    if printer = printer.scan(/:\s*(.+)/).flatten.first
      `lpstat -o -P #{printer}`.strip == ''
    end
  end
end

#import(path = nil) ⇒ Object

Import to Photos.app via AppleScript



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/photoapp.rb', line 119

def import(path=nil)
  path ||= config['photos_import']
  script = %Q{osascript -e 'set filePosixPath to "#{path}"
set importFolder to (POSIX file filePosixPath) as alias

set extensionsList to {"jpg", "jpeg"}
tell application "Finder" to set theFiles to every file of importFolder whose name extension is in extensionsList

if (count of theFiles) > 0 then
  set imageList to {}
  repeat with i from 1 to number of items in theFiles
set this_item to item i of theFiles as alias
set the end of imageList to this_item
  end repeat

  tell application "Photos"
  activate
  delay 1
  import imageList skip check duplicates yes
  end tell
end if'}
  `#{script}`
  FileUtils.rm load_photos(path)
end

#import_altObject



114
115
116
# File 'lib/photoapp.rb', line 114

def import_alt
  import(config['import_alt']) if config['import_alt']
end

#load_photos(path = nil) ⇒ Object

grab all photos from config source



169
170
171
172
173
174
# File 'lib/photoapp.rb', line 169

def load_photos(path=nil)
  path ||= config['source']
  files = ['*.jpg', '*.JPG', '*.JPEG', '*.jpeg'].map! { |f| File.join(path, f) }

  Dir[*files].uniq
end

#logoObject



83
84
85
# File 'lib/photoapp.rb', line 83

def 
   ||= Magick::Image.read(config['watermark']).first
end

#optimize(path = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/photoapp.rb', line 157

def optimize(path=nil)
  path ||= config['upload']
  exec = Photoapp.gem_dir("bin/imageOptim")

  if File.directory?(path)
    `#{exec} -d #{path}`
  else
    `find #{path} | #{exec}`
  end
end

#plistObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/photoapp.rb', line 230

def plist
  %Q{<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
<key>KeepAlive</key>
<dict>
  <key>SuccessfulExit</key>
  <false/>
</dict>
<key>Label</key>
<string>com.desotocaverns.photoapp</string>
<key>ProgramArguments</key>
<array>
  <string>#{Photoapp.gem_dir('bin', 'process.sh')}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>#{root}</string>
<key>StartInterval</key>
<integer>#{config['interval']}</integer>
  </dict>
</plist>}
end

#processObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/photoapp.rb', line 87

def process

  photos = load_photos
  unless photos.empty?

    # Announce photos
    noun = photos.size == 1 ? "photo" : "photos"
    system "say -v 'Daniel' 'processing #{photos.size} #{noun}'"

    tmp = root('.tmp')
    FileUtils.mkdir_p tmp

    photos.map! do |f|
      p = process_image(f, tmp)
      p.write
      p
    end

    optimize

    import
    print

    FileUtils.rm_rf tmp
  end
end

#process_image(photo, destination) ⇒ Object



151
152
153
154
155
# File 'lib/photoapp.rb', line 151

def process_image(photo, destination)
  path = File.join(destination, File.basename(photo))
  FileUtils.mv photo, path
  Photo.new(path, , self)
end

#reprint(prints = 1) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/photoapp.rb', line 200

def reprint(prints = 1)
  prints = prints.to_i < 1 ? 1 : prints.to_i

  files = ['*.jpg', '*.JPG', '*.JPEG', '*.jpeg'].map! { |f| File.join(config['reprint'], f) }
  photos = Dir[*files].uniq
  if !photos.empty?
    count  = photos.size == 1 ? "photo" : "photos"
    copies = photos.size == 1 ? "copy" : "copies"
    puts "Printing #{prints} #{copies} of #{photos.size} #{count}"

    system "lpr #{photos.join(' ')} -#{prints}"
  else
    puts "No photos to print"
  end

  sleep 4
  FileUtils.rm(photos)
end

#root(path = '') ⇒ Object



35
36
37
# File 'lib/photoapp.rb', line 35

def root(path='')
  File.expand_path(File.join(ROOT, path))
end

#test_image(photo) ⇒ Object

For processing a single image for testing purposes



221
222
223
224
225
226
227
228
# File 'lib/photoapp.rb', line 221

def test_image(photo)
  photo = File.expand_path(photo)
  path = photo.sub(/\.jpe?g/i, '.copy.jpg')
  FileUtils.cp photo, path

  Photo.new(path, , self).write(path)
  optimize path
end