Class: Picasaweb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/picasaweb-backup.rb

Constant Summary collapse

OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/picasaweb-backup.rb', line 29

def initialize
  scope = 'https://picasaweb.google.com/data/'
  client_id = Google::Auth::ClientId.from_file('client_id.json')
  token_store = Google::Auth::Stores::FileTokenStore.new(:file => 'tokens.yaml')
  authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)

  user_id = "picasaweb-backup"

  credentials = authorizer.get_credentials(user_id)

  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI )
    puts "Open #{url} in your browser and enter the resulting code:"
    code = gets
    @credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI)
  else
    puts "Found token #{credentials.access_token}. Refreshing..."
    credentials.fetch_access_token!
    @credentials = credentials
  end
end

Instance Method Details

#download(photo) ⇒ Object



88
89
90
91
# File 'lib/picasaweb-backup.rb', line 88

def download(photo)
  response = HTTP.get(photo[:url])
  File.open(photo[:title], 'w') { |f| f.write response.body }
end

#get(url) ⇒ Object



83
84
85
86
# File 'lib/picasaweb-backup.rb', line 83

def get(url)
  res = HTTP[:authorization => "Bearer #{@credentials.access_token}"].get(url)
  Nokogiri::XML(res.body)
end

#get_albumsObject

Retrieves all albums for a user.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/picasaweb-backup.rb', line 53

def get_albums
  uri = "https://picasaweb.google.com/data/feed/api/user/default"
  feed = get(uri)
  entries = feed.css("entry")

  entries.map do |entry|
    { :id => entry.css('gphoto|id').text,
      :user => entry.css('gphoto|user').text,
      :title => entry.css('title').text }
  end
end

#get_photos(album) ⇒ Object

Retrieves all photos from an album.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/picasaweb-backup.rb', line 66

def get_photos(album)
  uri = "https://picasaweb.google.com/data/feed/api/user/" +
    "default/albumid/#{album[:id]}?kind=photo&imgmax=d"

  entries = get(uri).css("entry")

  entries.map do |entry|
    url = entry.css('media|group > media|content[url]').first.attribute("url").value
    photo = { :id       => entry.css('gphoto|id').text,
              :album_id => entry.css('gphoto|albumid').text,
              :title    => entry.css('title').text,
              :url      => url
            }
  end
end