class Photoapp::Session

Constants

CONFIG_FILE

relative to root

PRINT
ROOT
UPLOAD

Attributes

photos[RW]
print[RW]
upload[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/photoapp.rb, line 29
def initialize(options={})
  @photos = []
  @config = config(options)
  FileUtils.mkdir_p(config['reprint'])
end

Public Instance Methods

config(options={}) click to toggle source
# 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?() click to toggle source

Check to see if the print queue is empty

# 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) click to toggle source

Import to Photos.app via AppleScript

# 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_alt() click to toggle source
# File lib/photoapp.rb, line 114
def import_alt
  import(config['import_alt']) if config['import_alt']
end
load_photos(path=nil) click to toggle source

grab all photos from config source

# 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
optimize(path=nil) click to toggle source
# 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
plist() click to toggle source
# 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
process() click to toggle source
# 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) click to toggle source
# 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, logo, self)
end
reprint(prints = 1) click to toggle source
# 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='') click to toggle source
# File lib/photoapp.rb, line 35
def root(path='')
  File.expand_path(File.join(ROOT, path))
end
test_image(photo) click to toggle source

For processing a single image for testing purposes

# 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, logo, self).write(path)
  optimize path
end