Module: Flickru

Defined in:
lib/flickru.rb,
lib/flickru/version.rb

Constant Summary collapse

VERSION =
"0.6.5"

Class Method Summary collapse

Class Method Details

.config_filenameObject



36
37
38
# File 'lib/flickru.rb', line 36

def Flickru.config_filename
  File.join ENV['HOME'], "." + File.basename(__FILE__, File.extname(__FILE__)) + "rc"
end

.die(code, message) ⇒ Object



31
32
33
34
# File 'lib/flickru.rb', line 31

def Flickru.die code, message
  Printer.error "error:#{code}: #{message}"
  exit 1
end

.flickru(rmFlag, photo_dir) ⇒ Object



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flickru.rb', line 64

def self.flickru rmFlag, photo_dir
  begin
    Flickru.read_config
  rescue RuntimeError
    token, secret = Flickr.
    write_config token, secret
  end

  # upload and classify
  photos       = File.find(photo_dir) {|f| File.image? f or File.video? f }
  total_size   = photos.reduce(0) {|t,p| t + File.size(p)}
  Printer.info "#{File.human_readable_size total_size} to upload"
  journey      = Journey.new total_size
  photoset_ids = Set.new
  photos.sort.each do |photo|
      Printer.info "file '#{File.join File.basename(File.dirname(photo)), File.basename(photo)}' under process"
    begin
      retries ||= 0
      photo_id = Flickr.upload_photo photo
      photoset_ids << Flickr.classify(photo, photo_id)
    rescue ArgumentError || Errno::ENOENT || Errno::EAGAIN || Errno::EPIPE || FlickRaw::FailedResponse
      if (retries += 1) < 3
        Printer.warn "retrying #{photo}: #{$!}"
        sleep (Math.exp retries)
        retry
      else
        Printer.failure "#{photo}: #{$!}"
      end
    end
    journey.take File.size(photo)
    Printer.info "#{File.human_readable_size journey.progress} uploaded, " +
                 "#{File.human_readable_size journey.distance} remaining. " +
                 "ETA: #{Printer.human_readable_seconds journey.eta}" if journey.eta > 0
    File.delete photo if rmFlag
  end

  photoset_ids.each do |set_id|
    Flickr.arrangePhotos set_id
  end

  Printer.ask "Please, review whether: any of your photos need to be rotated,\n" +
              "  better primary photos for your sets have been uploaded,\n" +
              "  and better collection mosaics can be randomised."
rescue Exception => e
  file_line = e.backtrace.empty? ? "?" : e.backtrace[0].split(':')
  Flickru.die "#{File.basename file_line[-3]}:#{file_line[-2]}", e.message
end

.read_configObject



40
41
42
43
44
45
46
47
# File 'lib/flickru.rb', line 40

def self.read_config
  file = File.open config_filename, "r"
  token, secret = file.readlines.map { |line| line.sub(/\#.*$/, '').strip } # remove line comments
  file.close
  Flickr.access token, secret
rescue
  raise RuntimeError, "unable to open configuration file #{config_filename}"
end

.usageObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flickru.rb', line 18

def Flickru.usage
  filename = File.basename __FILE__
  Printer.show "#{filename} -- Flickr upload command-line automator\n"
  Printer.show "usage: #{filename} [<options>] <photo directory>\n"
  Printer.show "example: #{filename} my_photos\n"
  Printer.show "options:\n"
  Printer.show "  -h, --help      show help\n"
  Printer.show "  -r, --rm        delete each photo when uploaded\n"
  Printer.show "  -v, --version   show version\n"
  readme = File.expand_path(File.join(File.dirname(__FILE__), '..', 'README'))
  Printer.show "\n#{IO.read(readme)}"
end

.write_config(token, secret) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/flickru.rb', line 49

def Flickru.write_config token, secret
    Printer.show "writing configuration file #{config_filename}... "
  if File.exists? config_filename
    file = File.new config_filename, "w"
  else
    file = File.open config_filename, "w"
  end
  file.puts "#{token} \# access token"
  file.puts "#{secret} \# access secret"
  file.close
    Printer.success
rescue
  raise RuntimeError, "unable to write configuration file #{config_filename}"
end