Class: PicasaDownloader::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/picasa-downloader/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Client

Returns a new instance of Client.



3
4
5
6
# File 'lib/picasa-downloader/client.rb', line 3

def initialize(credentials)
  @gd = GData::Client::Photos.new
  @gd.clientlogin(credentials.username, credentials.password)
end

Instance Method Details

#download_photo(photo) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/picasa-downloader/client.rb', line 33

def download_photo(photo)
  if photo.has_video?
    # Downloading videos is not supported yet
  else
    with_retry {
      uri = URI.parse(photo.url)
      http = Net::HTTP.new(uri.host, uri.port)
      request = Net::HTTP::Get.new(uri.request_uri)
      http.use_ssl = true
      response = http.request(request)
      raise "Response not ok, was #{response.code}" unless
        response.code.to_i.between? 200, 299
      response.body
    }
  end
end

#list_albumsObject



8
9
10
11
12
13
14
15
16
# File 'lib/picasa-downloader/client.rb', line 8

def list_albums
  doc = to_xml('https://picasaweb.google.com/data/feed/api/user/default')
  doc.css('entry').map { |e|
    id = e.xpath("id/text()").
      select { |x| x.to_s.match(/^\d+$/) }.
      map { |x| x.inner_text }.first
    Album.new(e.css('title').first.inner_text, id)
  }
end

#list_photos(album_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/picasa-downloader/client.rb', line 18

def list_photos(album_id)
  doc = to_xml(
    "https://picasaweb.google.com/data/feed/api/user/default/albumid/#{album_id}?imgmax=d")
  doc.css("entry").map { |e|
    timestamp = e.css("tags time")
    timestamp = e.css("timestamp") unless timestamp
    Photo.new(
      e.xpath("group/content/@url").to_s,
      # Google includes the milliseconds in the timestamp:
      timestamp.inner_text.to_i / 1000,
      e.css("size").inner_text.to_i,
      e.css("title").first.inner_text)
  }
end