module Wallzilla::Runner

Constants

DEFAULT_FILENAME
KEYWORDS_SIZE
KW_LEN_RANGE
NO_WORDS_FILE_FOUND

Public Instance Methods

build(kw:, result:, key:, words: nil, tiles: "4x3", bg: "black") click to toggle source

Entry point for external calling Wallzilla image generation

# File lib/wallzilla/runner.rb, line 13
def build(kw:, result:, key:, words: nil, tiles: "4x3", bg: "black")
  @_output_file = result
  @_key         = key
  @wordlist     = words
  @tiles        = tiles
  @_fill        = bg

  run!(kw)
end
clean(photos) click to toggle source
# File lib/wallzilla/runner.rb, line 53
def clean(photos)
  photos.map { |x| x.close; x.unlink }
end
dict_file() click to toggle source
# File lib/wallzilla/runner.rb, line 87
def dict_file
  return @_dict if defined?(@_dict)

  @_dict = maybe_dictionaries.
             uniq.
             compact.
             select { |f| FileTest.readable?(f) }.first || fail(NO_WORDS_FILE_FOUND)
end
fetch(kw) click to toggle source
# File lib/wallzilla/runner.rb, line 57
def fetch(kw)
  Wallzilla::Fetcher.fetch(kw)
end
fill() click to toggle source
# File lib/wallzilla/runner.rb, line 65
def fill
  return @_fill if defined?(@_fill)

  @_fill =
    ENV["background_color"] || "black"
end
flickr_api_key() click to toggle source
# File lib/wallzilla/runner.rb, line 79
def flickr_api_key
  return @_key if defined?(@_key)

  @_key = ENV.fetch("FLICKR_KEY") do |e|
    fail("You should provide Flickr API application key! (--key key_string)")
  end
end
keywords() click to toggle source
# File lib/wallzilla/runner.rb, line 61
def keywords
  @keywords
end
maybe_dictionaries() click to toggle source
# File lib/wallzilla/runner.rb, line 96
def maybe_dictionaries
  Array(@wordlist) +
    Array(ENV["words_file"]) +
    ["/usr/share/dict/words", "/usr/share/words"]
end
maybe_show_progress(msg, value = nil) click to toggle source
# File lib/wallzilla/runner.rb, line 112
def maybe_show_progress(msg, value = nil)
  return unless @progressbar
  @progressbar.call(msg, value)
end
normalize(kw) click to toggle source
# File lib/wallzilla/runner.rb, line 102
def normalize(kw)
  Array.new(KEYWORDS_SIZE) do |i|
    kw[i] || random_word
  end
end
output_file() click to toggle source
# File lib/wallzilla/runner.rb, line 72
def output_file
  return @_output_file if defined?(@_output_file)

  @_output_file =
    ENV["result_file"] || File.join(Dir.pwd, DEFAULT_FILENAME)
end
process_photos(kw) click to toggle source
# File lib/wallzilla/runner.rb, line 41
def process_photos(kw)
  kw.map.with_index(1) do |word, idx|
    fetched_photo = loop do
      url = fetch(word)
      break url if url
      word = random_word
    end
    maybe_show_progress(:progress, idx)
    fetched_photo
  end
end
random_word() click to toggle source
# File lib/wallzilla/runner.rb, line 108
def random_word
  Wallzilla::Words.random_word(KW_LEN_RANGE)
end
run!(kw, progressbar: nil) click to toggle source
# File lib/wallzilla/runner.rb, line 23
def run!(kw, progressbar: nil)
  @progressbar = progressbar
  maybe_show_progress(:started)

  @keywords = normalize(kw)

  photos = process_photos(keywords)

  Wallzilla::Montage.process!(photos, output_file, fill)

  maybe_show_progress(:finished)
rescue => e
  maybe_show_progress(:interrupted)
  raise e
ensure
  clean(photos) if photos
end