Class: PicasaPlucker::CLI

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

Class Method Summary collapse

Class Method Details

.execute(stdout, stderr, arguments = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/picasa_plucker/cli.rb', line 5

def self.execute(stdout, stderr, arguments=[])

  # NOTE: the option -p/--path= is given as an example, and should be replaced in your application.

  options = {
    :path     => '.'
  }

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      Downloads all images from a picasa web album.

      Usage: #{File.basename($0)} album_url [options]

      Options are:
    BANNER
    opts.separator ""
    # opts.on("-p", "--path PATH", String,
    #         "This is a sample message.",
    #         "For multiple lines, add more strings.",
    #         "Default: .") { |arg| options[:path] = arg }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)
  end
  
  require_curl stderr

  begin
    path = options[:path]
    url  = arguments.shift
    if url =~ %r{picasaweb\.google\.[^\\]+\/([^\\]+)\/([^\\?]+)}
      picasa_user, picasa_album = $1, $2
      FileUtils.mkdir_p(path)
      FileUtils.chdir(path) do
        FileUtils.mkdir_p(album_path = File.join(picasa_user, picasa_album))
        FileUtils.chdir(album_path) do
          fetch_album_images(url, stdout, stderr)
        end
      end
    else
      stdout.puts parser; exit
    end
  rescue Interrupt
  rescue SystemExit
  end
end

.fetch_album_images(url, stdout = STDOUT, stderr = STDERR) ⇒ Object



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
# File 'lib/picasa_plucker/cli.rb', line 53

def self.fetch_album_images(url, stdout = STDOUT, stderr = STDERR)
  require "open-uri"
  require "hpricot"
  require "progressbar"

  stderr.puts "Fetching album information..."
  begin
    doc = Hpricot(open(url))
  rescue OpenURI::HTTPError
    stderr.puts "Cannot find album at url: #{url}"; exit
  end
  images = doc.search("//noscript/div/a")
  if images.size > 0
    pbar = ProgressBar.new("Downloading", images.size, stderr)
    pbar.set(0)
    images.each do |image|
      thumbnail_url = image.search("/img").first["src"]
      image_url = thumbnail_url.sub(%r{/s\d+}, '')
      
      image_file = File.basename(image_url)
      system "curl '#{image_url}' -o '#{image_file}' --silent"
      pbar.inc
    end
    pbar.finish
  else
    stderr.puts "Cannot find any images"; exit
  end
end

.require_curl(stderr) ⇒ Object



82
83
84
85
86
# File 'lib/picasa_plucker/cli.rb', line 82

def self.require_curl(stderr)
  if `which curl`.strip == ""
    stderr.puts "Please install 'curl' to use picasa_plucker. Sorry about that."; exit
  end
end