Module: Flickru

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

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.config_filenameObject



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

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

.die(code, message) ⇒ Object



27
28
29
30
# File 'lib/flickru.rb', line 27

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

.flickru(photo_dir) ⇒ Object



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
90
91
92
93
94
95
96
97
98
# File 'lib/flickru.rb', line 60

def self.flickru 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
      photo_id = Flickr.upload_photo photo
      photoset_ids << Flickr.classify(photo, photo_id)
    rescue ArgumentError || Errno::ENOENT || Errno::EAGAIN || FlickRaw::FailedResponse
      Printer.failure "#{photo}: #{$!}"
    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
  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[0].split(':')
  Flickru.die "#{File.basename file_line[-3]}:#{file_line[-2]}", e.message
end

.read_configObject



36
37
38
39
40
41
42
43
# File 'lib/flickru.rb', line 36

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
# 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} [-h|--help|-v|--version] <photo directory>\n"
  Printer.show "example: #{filename} my_photos\n"
  readme = File.expand_path(File.join(File.dirname(__FILE__), '..', 'README'))
  Printer.show "\n#{IO.read(readme)}"
end

.write_config(token, secret) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/flickru.rb', line 45

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