Class: Picasa::Picasa

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#picasa_sessionObject

Returns the value of attribute picasa_session.



22
23
24
# File 'lib/picasa.rb', line 22

def picasa_session
  @picasa_session
end

Class Method Details

.entries(xml_response) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/picasa.rb', line 296

def self.entries(xml_response)
  document = XmlSimple.xml_in(xml_response, { 'ForceArray' => false });
  return [] if (!document['totalResults'].nil? && document['totalResults'].to_i == 0)

  entries = (document['totalResults']).to_i > 1 ? document["entry"] : [document["entry"]]
  return entries.compact
end

.parse_album_entry(album_entry_xml) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/picasa.rb', line 304

def self.parse_album_entry(album_entry_xml)
  #album_hash = XmlSimple.xml_in(album_entry_xml.to_s, { 'ForceArray' => false })
  album_hash = album_entry_xml

  album = Album.new
  album.xml = album_entry_xml.to_s

  album.id = album_hash["id"][1]
  album.name = album_hash["name"]
  album.user = album_hash["user"]
  album.number_of_photos = album_hash["numphotos"]
  album.number_of_comments = album_hash["commentCount"]
  album.is_commentable = album_hash["commentingEnabled"] == true ? true : false
  album.access = album_hash["access"]

  album.author_name = album_hash["author"]["name"]
  album.author_uri = album_hash["author"]["uri"]

  album.title = album_hash["group"]["title"]["content"]
  album.title_type = album_hash["group"]["title"]["type"]
  album.description = album_hash["group"]["description"]["content"]
  album.description_type = album_hash["group"]["description"]["type"]
  album.image_url = album_hash["group"]["content"]["url"]
  album.image_type = album_hash["group"]["content"]["type"]
  album.thumbnail = Thumbnail.new()
  album.thumbnail.url = album_hash["group"]["thumbnail"]["url"]
  album.thumbnail.width = album_hash["group"]["thumbnail"]["width"]
  album.thumbnail.height = album_hash["group"]["thumbnail"]["height"]

  # make self xml url
  links_from_hash = album_hash["link"]
  if(links_from_hash.respond_to?(:each))
    links_from_hash.each do |link|
      if(link["rel"] == "self")
        album.self_xml_url = link["href"]
      elsif(link["rel"] == "edit")
        album.edit_url = link["href"]
      end
    end
  else
    album.self_xml_url = nil
    album.edit_url = nil
  end

  return album
end

.parse_photo_entry(photo_entry_xml) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/picasa.rb', line 351

def self.parse_photo_entry(photo_entry_xml)
  #photo_hash = XmlSimple.xml_in(photo_entry_xml.to_s, { 'ForceArray' => false })
  photo_hash = photo_entry_xml

  photo = Photo.new
  photo.xml = photo_entry_xml.to_s

  photo.id = photo_hash["id"][1]
  photo.album_id = photo_hash["albumid"]
  photo.version_number = photo_hash["version"]
  photo.is_commentable = photo_hash["commentingEnabled"]
  photo.size = photo_hash["size"]
  photo.client = photo_hash["client"]
  photo.title = photo_hash["group"]["title"]["content"]
  photo.description = photo_hash["group"]["description"]["content"]
  photo.url = photo_hash["group"]["content"]["url"]
  photo.width = photo_hash["group"]["content"]["width"]
  photo.height = photo_hash["group"]["content"]["height"]
  photo.type = photo_hash["group"]["content"]["type"]
  photo.medium = photo_hash["group"]["content"]["medium"]

  # make thumbnails
  photo.thumbnails = []
  thumbnails_from_hash = photo_hash["group"]["thumbnail"]
  if(thumbnails_from_hash.respond_to?(:each))
    thumbnails_from_hash.each do |thumb|
      thumbnail = Thumbnail.new
      thumbnail.url = thumb["url"]
      thumbnail.width = thumb["width"]
      thumbnail.height = thumb["height"]

      photo.thumbnails << thumbnail
    end
  else

  end

  # make self xml url
  links_from_hash = photo_hash["link"]
  if(links_from_hash.respond_to?(:each))
    links_from_hash.each do |link|
      if(link["rel"] == "self")
        photo.self_xml_url = link["href"]
      elsif(link["rel"] == "edit")
        photo.edit_url = link["href"]
      end
    end
  else
    photo.self_xml_url = nil
    photo.edit_url = nil
  end

  return photo
end

Instance Method Details

#album(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/picasa.rb', line 47

def album(options = {})

  if(options[:name] == nil)
    return nil
  end

  albums = self.albums(options)
  for album in albums
    if(album.name == options[:name])
      return album
    end
  end

  return nil
end

#albums(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/picasa.rb', line 63

def albums(options = {})

  userId = options[:user_id] == nil ? self.picasa_session.user_id : options[:user_id]
  access = options[:access] == nil ? "public" : options[:access]
  url = "http://picasaweb.google.com/data/feed/api/user/#{userId}?kind=album&access=#{access}"

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}

  response, xml_response = http.get(uri.path, headers)

  #xml_response = Net::HTTP.get_response(URI.parse(url)).body.to_s
  albums = create_albums_from_xml(xml_response)

  return albums
end

#create_album(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/picasa.rb', line 94

def create_album(options = {})

  title = options[:title].nil? ? "" : options[:title]
  summary = options[:summary].nil? ? "" : options[:summary]
  location = options[:location].nil? ? "" : options[:location]
  access = options[:access].nil? ? "public" : options[:access]
  commentable = options[:commentable].nil? ? "true" : options[:commentable].to_s
  keywords = options[:keywords].nil? ? "" : options[:keywords]
  time_i = (Time.now).to_i * 1000

  createAlbumRequestXml = "<entry xmlns='http://www.w3.org/2005/Atom'
                xmlns:media='http://search.yahoo.com/mrss/'
                xmlns:gphoto='http://schemas.google.com/photos/2007'>
                  <title type='text'>#{title}</title>
                  <summary type='text'>#{summary}</summary>
                  <gphoto:location>#{location}</gphoto:location>
                  <gphoto:access>#{access}</gphoto:access>
                  <gphoto:commentingEnabled>#{commentable}</gphoto:commentingEnabled>
                  <gphoto:timestamp>#{time_i}</gphoto:timestamp>
                  <media:group>
                    <media:keywords>#{keywords}</media:keywords>
                  </media:group>
                  <category scheme='http://schemas.google.com/g/2005#kind'
                    term='http://schemas.google.com/photos/2007#album'></category>
                </entry>"

  url = "http://picasaweb.google.com/data/feed/api/user/#{self.picasa_session.user_id}"

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
  response, data = http.post(uri.path, createAlbumRequestXml, headers)
  data = response.body
  album = create_album_from_xml(data)
  return album
end

#create_album_from_xml(xml_response) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/picasa.rb', line 268

def create_album_from_xml(xml_response)
  album = nil

  #parse the entry xml element and get the album object
  album = Picasa.parse_album_entry(XmlSimple.xml_in(xml_response, { 'ForceArray' => false }))

  #enter session values in album object
  album.picasa_session = PicasaSession.new
  album.picasa_session.auth_key = self.picasa_session.auth_key
  album.picasa_session.user_id = self.picasa_session.user_id

  return album
end

#create_albums_from_xml(xml_response) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/picasa.rb', line 248

def create_albums_from_xml(xml_response)
  albums = []
  #response_hash = XmlSimple.xml_in(xml_response, { 'ForceArray' => false })
  #puts response_hash.inspect

  Picasa.entries(xml_response).each do |entry|
    #parse the entry xml element and get the album object
    album = Picasa.parse_album_entry(entry)

    #enter session values in album object
    album.picasa_session = PicasaSession.new
    album.picasa_session.auth_key = self.picasa_session.auth_key
    album.picasa_session.user_id = self.picasa_session.user_id

    albums << album
  end

  return albums
end

#create_photo_from_xml(xml_response) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/picasa.rb', line 282

def create_photo_from_xml(xml_response)
  photo = nil

  #parse the entry xml element and get the photo object
  photo = Picasa.parse_photo_entry(XmlSimple.xml_in(xml_response, { 'ForceArray' => false }))

  #enter session values in photo object
  photo.picasa_session = PicasaSession.new
  photo.picasa_session.auth_key = self.picasa_session.auth_key
  photo.picasa_session.user_id = self.picasa_session.user_id

  return photo
end

#delete_album(album) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/picasa.rb', line 229

def delete_album(album)
  url = album.edit_url

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}

  response, data = http.delete(uri.path, headers)
  data = response.body

  if(response.code == "200")
    return true
  else
    return false
  end

end

#delete_photo(photo) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/picasa.rb', line 210

def delete_photo(photo)
  url = photo.edit_url

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}

  response, data = http.delete(uri.path, headers)
  data = response.body

  if(response.code == "200")
    return true
  else
    return false
  end

end

#load_album(album_url) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/picasa.rb', line 132

def load_album(album_url)
  uri = URI.parse(album_url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}

  response, album_entry_xml_response = http.get(uri.path, headers)

  if(response.code == "200")
    #parse the entry xml element and get the photo object
    album = self.create_album_from_xml(album_entry_xml_response)
    return album
  else
    return nil
  end
end

#load_album_with_id(album_id) ⇒ Object



149
150
151
152
153
# File 'lib/picasa.rb', line 149

def load_album_with_id(album_id)
  album_url = "http://picasaweb.google.com/data/entry/api/user/#{self.picasa_session.user_id}/albumid/#{album_id}"

  return load_album(album_url)
end

#load_photo(photo_url) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/picasa.rb', line 155

def load_photo(photo_url)
  uri = URI.parse(photo_url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}

  response, photo_entry_xml_response = http.get(uri.path, headers)

  if(response.code == "200")
    #parse the entry xml element and get the photo object
    photo = self.create_photo_from_xml(photo_entry_xml_response)
    return photo
  else
    return nil
  end
end

#load_photo_with_id(photo_id, album_id) ⇒ Object



172
173
174
175
176
# File 'lib/picasa.rb', line 172

def load_photo_with_id(photo_id, album_id)
  photo_url = "http://picasaweb.google.com/data/entry/api/user/#{self.picasa_session.user_id}/albumid/#{album_id}/photoid/#{photo_id}"

  return load_photo(photo_url)
end

#login(email, password) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/picasa.rb', line 24

def (email, password)
  url = "https://www.google.com/accounts/ClientLogin"
  source = "MyCompany-TestProject-1.0.0" # source will be of CompanyName-ProjectName-ProjectVersion format

  uri = URI.parse(url)

  request = Net::HTTP.new(uri.host, uri.port)
  request.use_ssl = true
  request.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response, data = request.post(uri.path, "accountType=GOOGLE&Email=#{email}&Passwd=#{password}&service=lh2&source=#{source}")
  data = response.body
  authMatch = Regexp.compile("(Auth=)([A-Za-z0-9_\-]+)\n").match(data.to_s)
  if authMatch
    authorizationKey = authMatch[2].to_s # substring that matched the pattern ([A-Za-z0-9_\-]+)
  end

  self.picasa_session = PicasaSession.new
  self.picasa_session.auth_key = authorizationKey
  self.picasa_session.user_id = email

  return authorizationKey
end

#photos(options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/picasa.rb', line 82

def photos(options = {})

  options[:user_id] = options[:user_id].nil? ? self.picasa_session.user_id : options[:user_id]
  options[:album] = options[:album].nil? ? "" : options[:album]

  album = Album.new
  album.picasa_session = self.picasa_session
  photos = album.photos(options)

  return photos
end

#post_photo(image_data = nil, options = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/picasa.rb', line 178

def post_photo(image_data = nil, options = {})
  summary = options[:summary] == nil ? "" : options[:summary]
  album_name = options[:album] == nil ? "" : options[:album]
  album_id = options[:album_id] == nil ? "" : options[:album_id]
  local_file_name = options[:local_file_name] == nil ? "" : options[:local_file_name]
  title = options[:title] == nil ? local_file_name : options[:title]

  if(image_data == nil)
    return nil
    # Or throw an exception in next update
  end

  if(album_id != "")
    url = "http://picasaweb.google.com/data/feed/api/user/#{self.picasa_session.user_id}/albumid/#{album_id}"
  else
    url = "http://picasaweb.google.com/data/feed/api/user/#{self.picasa_session.user_id}/album/#{album_name}"
  end

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  headers = {"Content-Type" => "image/jpeg",
    "Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}",
    "Slug" => title, "Content-Transfer-Encoding" => "binary"}

  response, data = http.post(uri.path, image_data, headers)
  data = response.body
  photo = self.create_photo_from_xml(data)

  return photo
end