google-picasa 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/google-picasa.rb +555 -562
- data/lib/google-picasa/version.rb +1 -1
- metadata +5 -5
data/lib/google-picasa.rb
CHANGED
@@ -10,568 +10,561 @@ module Google
|
|
10
10
|
module Picasa
|
11
11
|
|
12
12
|
class PicasaSession
|
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
|
-
|
52
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
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
|
-
|
350
|
-
|
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
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
end
|
569
|
-
|
570
|
-
end
|
571
|
-
|
572
|
-
class Thumbnail
|
573
|
-
attr_accessor :url, :width, :height
|
574
|
-
end
|
13
|
+
attr_accessor :auth_key
|
14
|
+
attr_accessor :user_id
|
15
|
+
end
|
16
|
+
|
17
|
+
class Picasa
|
18
|
+
attr_accessor :picasa_session
|
19
|
+
|
20
|
+
def login(email, password)
|
21
|
+
url = "https://www.google.com/accounts/ClientLogin"
|
22
|
+
source = "MyCompany-TestProject-1.0.0" # source will be of CompanyName-ProjectName-ProjectVersion format
|
23
|
+
|
24
|
+
uri = URI.parse(url)
|
25
|
+
|
26
|
+
request = Net::HTTP.new(uri.host, uri.port)
|
27
|
+
request.use_ssl = true
|
28
|
+
request.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
29
|
+
response, data = request.post(uri.path, "accountType=HOSTED_OR_GOOGLE&Email=#{email}&Passwd=#{password}&service=lh2&source=#{source}")
|
30
|
+
|
31
|
+
authMatch = Regexp.compile("(Auth=)([A-Za-z0-9_\-]+)\n").match(data.to_s)
|
32
|
+
if authMatch
|
33
|
+
authorizationKey = authMatch[2].to_s # substring that matched the pattern ([A-Za-z0-9_\-]+)
|
34
|
+
end
|
35
|
+
|
36
|
+
self.picasa_session = PicasaSession.new
|
37
|
+
self.picasa_session.auth_key = authorizationKey
|
38
|
+
self.picasa_session.user_id = email
|
39
|
+
|
40
|
+
return authorizationKey
|
41
|
+
end
|
42
|
+
|
43
|
+
def album(options = {})
|
44
|
+
|
45
|
+
if(options[:name] == nil)
|
46
|
+
return nil
|
47
|
+
end
|
48
|
+
|
49
|
+
albums = self.albums(options)
|
50
|
+
for album in albums
|
51
|
+
if(album.name == options[:name])
|
52
|
+
return album
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
return nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def albums(options = {})
|
60
|
+
|
61
|
+
userId = options[:user_id] == nil ? self.picasa_session.user_id : options[:user_id]
|
62
|
+
access = options[:access] == nil ? "public" : options[:access]
|
63
|
+
url = "http://picasaweb.google.com/data/feed/api/user/#{userId}?kind=album&access=#{access}"
|
64
|
+
|
65
|
+
uri = URI.parse(url)
|
66
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
67
|
+
|
68
|
+
headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
69
|
+
|
70
|
+
response, xml_response = http.get(uri.path, headers)
|
71
|
+
|
72
|
+
#xml_response = Net::HTTP.get_response(URI.parse(url)).body.to_s
|
73
|
+
albums = create_albums_from_xml(xml_response)
|
74
|
+
|
75
|
+
return albums
|
76
|
+
end
|
77
|
+
|
78
|
+
def photos(options = {})
|
79
|
+
|
80
|
+
options[:user_id] = options[:user_id].nil? ? self.picasa_session.user_id : options[:user_id]
|
81
|
+
options[:album] = options[:album].nil? ? "" : options[:album]
|
82
|
+
|
83
|
+
album = Album.new
|
84
|
+
album.picasa_session = self.picasa_session
|
85
|
+
photos = album.photos(options)
|
86
|
+
|
87
|
+
return photos
|
88
|
+
end
|
89
|
+
|
90
|
+
def create_album(options = {})
|
91
|
+
|
92
|
+
title = options[:title].nil? ? "" : options[:title]
|
93
|
+
summary = options[:summary].nil? ? "" : options[:summary]
|
94
|
+
location = options[:location].nil? ? "" : options[:location]
|
95
|
+
access = options[:access].nil? ? "public" : options[:access]
|
96
|
+
commentable = options[:commentable].nil? ? "true" : options[:commentable].to_s
|
97
|
+
keywords = options[:keywords].nil? ? "" : options[:keywords]
|
98
|
+
time_i = (Time.now).to_i
|
99
|
+
|
100
|
+
createAlbumRequestXml = "<entry xmlns='http://www.w3.org/2005/Atom'
|
101
|
+
xmlns:media='http://search.yahoo.com/mrss/'
|
102
|
+
xmlns:gphoto='http://schemas.google.com/photos/2007'>
|
103
|
+
<title type='text'>#{title}</title>
|
104
|
+
<summary type='text'>#{summary}</summary>
|
105
|
+
<gphoto:location>#{location}</gphoto:location>
|
106
|
+
<gphoto:access>#{access}</gphoto:access>
|
107
|
+
<gphoto:commentingEnabled>#{commentable}</gphoto:commentingEnabled>
|
108
|
+
<gphoto:timestamp>#{time_i}</gphoto:timestamp>
|
109
|
+
<media:group>
|
110
|
+
<media:keywords>#{keywords}</media:keywords>
|
111
|
+
</media:group>
|
112
|
+
<category scheme='http://schemas.google.com/g/2005#kind'
|
113
|
+
term='http://schemas.google.com/photos/2007#album'></category>
|
114
|
+
</entry>"
|
115
|
+
|
116
|
+
url = "http://picasaweb.google.com/data/feed/api/user/#{self.picasa_session.user_id}"
|
117
|
+
|
118
|
+
uri = URI.parse(url)
|
119
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
120
|
+
|
121
|
+
headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
122
|
+
|
123
|
+
response, data = http.post(uri.path, createAlbumRequestXml, headers)
|
124
|
+
|
125
|
+
album = create_album_from_xml(data)
|
126
|
+
return album
|
127
|
+
end
|
128
|
+
|
129
|
+
def load_album(album_url)
|
130
|
+
uri = URI.parse(album_url)
|
131
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
132
|
+
|
133
|
+
headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
134
|
+
|
135
|
+
response, album_entry_xml_response = http.get(uri.path, headers)
|
136
|
+
|
137
|
+
if(response.code == "200")
|
138
|
+
#parse the entry xml element and get the photo object
|
139
|
+
album = self.create_album_from_xml(album_entry_xml_response)
|
140
|
+
return album
|
141
|
+
else
|
142
|
+
return nil
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def load_album_with_id(album_id)
|
147
|
+
album_url = "http://picasaweb.google.com/data/entry/api/user/#{self.picasa_session.user_id}/albumid/#{album_id}"
|
148
|
+
|
149
|
+
return load_album(album_url)
|
150
|
+
end
|
151
|
+
|
152
|
+
def load_photo(photo_url)
|
153
|
+
uri = URI.parse(photo_url)
|
154
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
155
|
+
|
156
|
+
headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
157
|
+
|
158
|
+
response, photo_entry_xml_response = http.get(uri.path, headers)
|
159
|
+
|
160
|
+
if(response.code == "200")
|
161
|
+
#parse the entry xml element and get the photo object
|
162
|
+
photo = self.create_photo_from_xml(photo_entry_xml_response)
|
163
|
+
return photo
|
164
|
+
else
|
165
|
+
return nil
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def load_photo_with_id(photo_id, album_id)
|
170
|
+
photo_url = "http://picasaweb.google.com/data/entry/api/user/#{self.picasa_session.user_id}/albumid/#{album_id}/photoid/#{photo_id}"
|
171
|
+
|
172
|
+
return load_photo(photo_url)
|
173
|
+
end
|
174
|
+
|
175
|
+
def post_photo(image_data = nil, options = {})
|
176
|
+
summary = options[:summary] == nil ? "" : options[:summary]
|
177
|
+
album_name = options[:album] == nil ? "" : options[:album]
|
178
|
+
album_id = options[:album_id] == nil ? "" : options[:album_id]
|
179
|
+
local_file_name = options[:local_file_name] == nil ? "" : options[:local_file_name]
|
180
|
+
title = options[:title] == nil ? local_file_name : options[:title]
|
181
|
+
|
182
|
+
if(image_data == nil)
|
183
|
+
return nil
|
184
|
+
# Or throw an exception in next update
|
185
|
+
end
|
186
|
+
|
187
|
+
if(album_id != "")
|
188
|
+
url = "http://picasaweb.google.com/data/feed/api/user/#{self.picasa_session.user_id}/albumid/#{album_id}"
|
189
|
+
else
|
190
|
+
url = "http://picasaweb.google.com/data/feed/api/user/#{self.picasa_session.user_id}/album/#{album_name}"
|
191
|
+
end
|
192
|
+
|
193
|
+
uri = URI.parse(url)
|
194
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
195
|
+
|
196
|
+
headers = {"Content-Type" => "image/jpeg",
|
197
|
+
"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}",
|
198
|
+
"Slug" => title, "Content-Transfer-Encoding" => "binary"}
|
199
|
+
|
200
|
+
response, data = http.post(uri.path, image_data, headers)
|
201
|
+
photo = self.create_photo_from_xml(data)
|
202
|
+
|
203
|
+
return photo
|
204
|
+
end
|
205
|
+
|
206
|
+
def delete_photo(photo)
|
207
|
+
url = photo.edit_url
|
208
|
+
|
209
|
+
uri = URI.parse(url)
|
210
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
211
|
+
|
212
|
+
headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
213
|
+
|
214
|
+
response, data = http.delete(uri.path, headers)
|
215
|
+
|
216
|
+
if(response.code == "200")
|
217
|
+
return true
|
218
|
+
else
|
219
|
+
return false
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
223
|
+
|
224
|
+
def delete_album(album)
|
225
|
+
url = album.edit_url
|
226
|
+
|
227
|
+
uri = URI.parse(url)
|
228
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
229
|
+
|
230
|
+
headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
231
|
+
|
232
|
+
response, data = http.delete(uri.path, headers)
|
233
|
+
|
234
|
+
if(response.code == "200")
|
235
|
+
return true
|
236
|
+
else
|
237
|
+
return false
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
def create_albums_from_xml(xml_response)
|
243
|
+
albums = []
|
244
|
+
#response_hash = XmlSimple.xml_in(xml_response, { 'ForceArray' => false })
|
245
|
+
#puts response_hash.inspect
|
246
|
+
|
247
|
+
Picasa.entries(xml_response).each do |entry|
|
248
|
+
#parse the entry xml element and get the album object
|
249
|
+
album = Picasa.parse_album_entry(entry)
|
250
|
+
|
251
|
+
#enter session values in album object
|
252
|
+
album.picasa_session = PicasaSession.new
|
253
|
+
album.picasa_session.auth_key = self.picasa_session.auth_key
|
254
|
+
album.picasa_session.user_id = self.picasa_session.user_id
|
255
|
+
|
256
|
+
albums << album
|
257
|
+
end
|
258
|
+
|
259
|
+
return albums
|
260
|
+
end
|
261
|
+
|
262
|
+
def create_album_from_xml(xml_response)
|
263
|
+
album = nil
|
264
|
+
|
265
|
+
#parse the entry xml element and get the album object
|
266
|
+
album = Picasa.parse_album_entry(XmlSimple.xml_in(xml_response, { 'ForceArray' => false }))
|
267
|
+
|
268
|
+
#enter session values in album object
|
269
|
+
album.picasa_session = PicasaSession.new
|
270
|
+
album.picasa_session.auth_key = self.picasa_session.auth_key
|
271
|
+
album.picasa_session.user_id = self.picasa_session.user_id
|
272
|
+
|
273
|
+
return album
|
274
|
+
end
|
275
|
+
|
276
|
+
def create_photo_from_xml(xml_response)
|
277
|
+
photo = nil
|
278
|
+
|
279
|
+
#parse the entry xml element and get the photo object
|
280
|
+
photo = Picasa.parse_photo_entry(XmlSimple.xml_in(xml_response, { 'ForceArray' => false }))
|
281
|
+
|
282
|
+
#enter session values in photo object
|
283
|
+
photo.picasa_session = PicasaSession.new
|
284
|
+
photo.picasa_session.auth_key = self.picasa_session.auth_key
|
285
|
+
photo.picasa_session.user_id = self.picasa_session.user_id
|
286
|
+
|
287
|
+
return photo
|
288
|
+
end
|
289
|
+
|
290
|
+
def self.entries(xml_response)
|
291
|
+
document = XmlSimple.xml_in(xml_response, { 'ForceArray' => false });
|
292
|
+
return [] if (!document['totalResults'].nil? && document['totalResults'].to_i == 0)
|
293
|
+
|
294
|
+
entries = (document['totalResults']).to_i > 1 ? document["entry"] : [document["entry"]]
|
295
|
+
return entries.compact
|
296
|
+
end
|
297
|
+
|
298
|
+
def self.parse_album_entry(album_entry_xml)
|
299
|
+
#album_hash = XmlSimple.xml_in(album_entry_xml.to_s, { 'ForceArray' => false })
|
300
|
+
album_hash = album_entry_xml
|
301
|
+
|
302
|
+
album = Album.new
|
303
|
+
album.xml = album_entry_xml.to_s
|
304
|
+
|
305
|
+
album.id = album_hash["id"][1]
|
306
|
+
album.name = album_hash["name"]
|
307
|
+
album.user = album_hash["user"]
|
308
|
+
album.number_of_photos = album_hash["numphotos"]
|
309
|
+
album.number_of_comments = album_hash["commentCount"]
|
310
|
+
album.is_commentable = album_hash["commentingEnabled"] == true ? true : false
|
311
|
+
album.access = album_hash["access"]
|
312
|
+
|
313
|
+
album.author_name = album_hash["author"]["name"]
|
314
|
+
album.author_uri = album_hash["author"]["uri"]
|
315
|
+
|
316
|
+
album.title = album_hash["group"]["title"]["content"]
|
317
|
+
album.title_type = album_hash["group"]["title"]["type"]
|
318
|
+
album.description = album_hash["group"]["description"]["content"]
|
319
|
+
album.description_type = album_hash["group"]["description"]["type"]
|
320
|
+
album.image_url = album_hash["group"]["content"]["url"]
|
321
|
+
album.image_type = album_hash["group"]["content"]["type"]
|
322
|
+
album.thumbnail = Thumbnail.new()
|
323
|
+
album.thumbnail.url = album_hash["group"]["thumbnail"]["url"]
|
324
|
+
album.thumbnail.width = album_hash["group"]["thumbnail"]["width"]
|
325
|
+
album.thumbnail.height = album_hash["group"]["thumbnail"]["height"]
|
326
|
+
|
327
|
+
# make self xml url
|
328
|
+
links_from_hash = album_hash["link"]
|
329
|
+
if(links_from_hash.respond_to?(:each))
|
330
|
+
links_from_hash.each do |link|
|
331
|
+
if(link["rel"] == "self")
|
332
|
+
album.self_xml_url = link["href"]
|
333
|
+
elsif(link["rel"] == "edit")
|
334
|
+
album.edit_url = link["href"]
|
335
|
+
end
|
336
|
+
end
|
337
|
+
else
|
338
|
+
album.self_xml_url = nil
|
339
|
+
album.edit_url = nil
|
340
|
+
end
|
341
|
+
|
342
|
+
return album
|
343
|
+
end
|
344
|
+
|
345
|
+
def self.parse_photo_entry(photo_entry_xml)
|
346
|
+
#photo_hash = XmlSimple.xml_in(photo_entry_xml.to_s, { 'ForceArray' => false })
|
347
|
+
photo_hash = photo_entry_xml
|
348
|
+
|
349
|
+
photo = Photo.new
|
350
|
+
photo.xml = photo_entry_xml.to_s
|
351
|
+
|
352
|
+
photo.id = photo_hash["id"][1]
|
353
|
+
photo.album_id = photo_hash["albumid"]
|
354
|
+
photo.version_number = photo_hash["version"]
|
355
|
+
photo.is_commentable = photo_hash["commentingEnabled"]
|
356
|
+
photo.size = photo_hash["size"]
|
357
|
+
photo.client = photo_hash["client"]
|
358
|
+
photo.title = photo_hash["group"]["title"]["content"]
|
359
|
+
photo.description = photo_hash["group"]["description"]["content"]
|
360
|
+
photo.url = photo_hash["group"]["content"]["url"]
|
361
|
+
photo.width = photo_hash["group"]["content"]["width"]
|
362
|
+
photo.height = photo_hash["group"]["content"]["height"]
|
363
|
+
photo.type = photo_hash["group"]["content"]["type"]
|
364
|
+
photo.medium = photo_hash["group"]["content"]["medium"]
|
365
|
+
|
366
|
+
# make thumbnails
|
367
|
+
photo.thumbnails = []
|
368
|
+
thumbnails_from_hash = photo_hash["group"]["thumbnail"]
|
369
|
+
if(thumbnails_from_hash.respond_to?(:each))
|
370
|
+
thumbnails_from_hash.each do |thumb|
|
371
|
+
thumbnail = Thumbnail.new
|
372
|
+
thumbnail.url = thumb["url"]
|
373
|
+
thumbnail.width = thumb["width"]
|
374
|
+
thumbnail.height = thumb["height"]
|
375
|
+
|
376
|
+
photo.thumbnails << thumbnail
|
377
|
+
end
|
378
|
+
else
|
379
|
+
|
380
|
+
end
|
381
|
+
|
382
|
+
# make self xml url
|
383
|
+
links_from_hash = photo_hash["link"]
|
384
|
+
if(links_from_hash.respond_to?(:each))
|
385
|
+
links_from_hash.each do |link|
|
386
|
+
if(link["rel"] == "self")
|
387
|
+
photo.self_xml_url = link["href"]
|
388
|
+
elsif(link["rel"] == "edit")
|
389
|
+
photo.edit_url = link["href"]
|
390
|
+
end
|
391
|
+
end
|
392
|
+
else
|
393
|
+
photo.self_xml_url = nil
|
394
|
+
photo.edit_url = nil
|
395
|
+
end
|
396
|
+
|
397
|
+
return photo
|
398
|
+
end
|
399
|
+
|
400
|
+
end
|
401
|
+
|
402
|
+
class Album
|
403
|
+
attr_accessor :picasa_session
|
404
|
+
|
405
|
+
attr_accessor :id, :name
|
406
|
+
attr_accessor :user
|
407
|
+
attr_accessor :title, :title_type
|
408
|
+
attr_accessor :description, :description_type
|
409
|
+
attr_accessor :access
|
410
|
+
attr_accessor :author_name, :author_uri
|
411
|
+
attr_accessor :number_of_photos, :number_of_comments
|
412
|
+
attr_accessor :is_commentable
|
413
|
+
attr_accessor :image_url, :image_type
|
414
|
+
attr_accessor :thumbnail
|
415
|
+
attr_accessor :xml
|
416
|
+
attr_accessor :self_xml_url, :edit_url
|
417
|
+
|
418
|
+
def initialize()
|
419
|
+
thumbnail = Thumbnail.new()
|
420
|
+
end
|
421
|
+
|
422
|
+
def photos(options = {})
|
423
|
+
|
424
|
+
userId = options[:user_id].nil? ? self.user : options[:user_id]
|
425
|
+
albumName = options[:album].nil? ? self.name : options[:album]
|
426
|
+
albumId = options[:album_id].nil? ? self.id : options[:album_id]
|
427
|
+
|
428
|
+
if(albumId != nil && albumId != "")
|
429
|
+
url = "http://picasaweb.google.com/data/feed/api/user/#{userId}/albumid/#{albumId}?kind=photo"
|
430
|
+
else
|
431
|
+
url = "http://picasaweb.google.com/data/feed/api/user/#{userId}/album/#{albumName}?kind=photo"
|
432
|
+
end
|
433
|
+
|
434
|
+
uri = URI.parse(url)
|
435
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
436
|
+
|
437
|
+
headers = {"Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
438
|
+
|
439
|
+
response, xml_response = http.get(uri.path, headers)
|
440
|
+
photos = self.create_photos_from_xml(xml_response)
|
441
|
+
|
442
|
+
return photos
|
443
|
+
end
|
444
|
+
|
445
|
+
def create_photos_from_xml(xml_response)
|
446
|
+
photos = []
|
447
|
+
#response_hash = XmlSimple.xml_in(xml_response, { 'ForceArray' => false })
|
448
|
+
#puts response_hash.inspect
|
449
|
+
|
450
|
+
Picasa.entries(xml_response).each do |entry|
|
451
|
+
#parse the entry xml element and get the photo object
|
452
|
+
photo = Picasa.parse_photo_entry(entry)
|
453
|
+
|
454
|
+
#enter session values in photo object
|
455
|
+
photo.picasa_session = PicasaSession.new
|
456
|
+
photo.picasa_session.auth_key = self.picasa_session.auth_key
|
457
|
+
photo.picasa_session.user_id = self.picasa_session.user_id
|
458
|
+
|
459
|
+
photos << photo
|
460
|
+
end
|
461
|
+
|
462
|
+
return photos
|
463
|
+
end
|
464
|
+
|
465
|
+
end
|
466
|
+
|
467
|
+
class Photo
|
468
|
+
attr_accessor :picasa_session
|
469
|
+
|
470
|
+
attr_accessor :id
|
471
|
+
attr_accessor :title, :description
|
472
|
+
attr_accessor :album_id
|
473
|
+
attr_accessor :size
|
474
|
+
attr_accessor :client
|
475
|
+
attr_accessor :is_commentable, :number_of_comments
|
476
|
+
attr_accessor :url, :width, :height, :type, :medium
|
477
|
+
attr_accessor :thumbnails
|
478
|
+
attr_accessor :xml
|
479
|
+
attr_accessor :version_number
|
480
|
+
attr_accessor :self_xml_url, :edit_url
|
481
|
+
|
482
|
+
def initialize()
|
483
|
+
thumbnails = []
|
484
|
+
end
|
485
|
+
|
486
|
+
def update()
|
487
|
+
|
488
|
+
updatePhotoXml = "<entry xmlns='http://www.w3.org/2005/Atom'
|
489
|
+
xmlns:media='http://search.yahoo.com/mrss/'
|
490
|
+
xmlns:gphoto='http://schemas.google.com/photos/2007'>
|
491
|
+
<title type='text'>#{self.title}</title>
|
492
|
+
<summary type='text'>#{self.description}</summary>
|
493
|
+
<gphoto:checksum></gphoto:checksum>
|
494
|
+
<gphoto:client></gphoto:client>
|
495
|
+
<gphoto:rotation>#{0}</gphoto:rotation>
|
496
|
+
<gphoto:timestamp>#{Time.new.to_i.to_s}</gphoto:timestamp>
|
497
|
+
<gphoto:commentingEnabled>#{self.is_commentable.to_s}</gphoto:commentingEnabled>
|
498
|
+
<category scheme='http://schemas.google.com/g/2005#kind'
|
499
|
+
term='http://schemas.google.com/photos/2007#photo'></category>
|
500
|
+
</entry>"
|
501
|
+
|
502
|
+
url = "http://picasaweb.google.com/data/entry/api/user/#{self.picasa_session.user_id}/albumid/#{self.album_id}/photoid/#{self.id}/#{self.version_number}"
|
503
|
+
|
504
|
+
uri = URI.parse(url)
|
505
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
506
|
+
|
507
|
+
headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
508
|
+
|
509
|
+
response, data = http.put(uri.path, updatePhotoXml, headers)
|
510
|
+
|
511
|
+
if(response.code == "200")
|
512
|
+
#parse the entry xml element and get the photo object
|
513
|
+
#new_photo = Picasa.parse_photo_entry(data)
|
514
|
+
new_photo = Picasa.parse_photo_entry(XmlSimple.xml_in(data.to_s, { 'ForceArray' => false }))
|
515
|
+
self.version_number = new_photo.version_number
|
516
|
+
|
517
|
+
return true
|
518
|
+
else
|
519
|
+
return false
|
520
|
+
end
|
521
|
+
end
|
522
|
+
|
523
|
+
def move_to_album(picasa_album_id)
|
524
|
+
|
525
|
+
updatePhotoXml = "<entry xmlns='http://www.w3.org/2005/Atom'
|
526
|
+
xmlns:media='http://search.yahoo.com/mrss/'
|
527
|
+
xmlns:gphoto='http://schemas.google.com/photos/2007'>
|
528
|
+
<title type='text'>#{self.title}</title>
|
529
|
+
<summary type='text'>#{self.description}</summary>
|
530
|
+
<gphoto:albumid>#{picasa_album_id}</gphoto:albumid>
|
531
|
+
<gphoto:checksum></gphoto:checksum>
|
532
|
+
<gphoto:client></gphoto:client>
|
533
|
+
<gphoto:rotation>#{0}</gphoto:rotation>
|
534
|
+
<gphoto:timestamp>#{Time.new.to_i.to_s}</gphoto:timestamp>
|
535
|
+
<gphoto:commentingEnabled>#{self.is_commentable.to_s}</gphoto:commentingEnabled>
|
536
|
+
<category scheme='http://schemas.google.com/g/2005#kind'
|
537
|
+
term='http://schemas.google.com/photos/2007#photo'></category>
|
538
|
+
</entry>"
|
539
|
+
|
540
|
+
url = "http://picasaweb.google.com/data/entry/api/user/#{self.picasa_session.user_id}/albumid/#{self.album_id}/photoid/#{self.id}/#{self.version_number}"
|
541
|
+
|
542
|
+
uri = URI.parse(url)
|
543
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
544
|
+
|
545
|
+
headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{self.picasa_session.auth_key}"}
|
546
|
+
|
547
|
+
response, data = http.put(uri.path, updatePhotoXml, headers)
|
548
|
+
|
549
|
+
if(response.code == "200")
|
550
|
+
#parse the entry xml element and get the photo object
|
551
|
+
new_photo = Picasa.parse_photo_entry(XmlSimple.xml_in(data.to_s, { 'ForceArray' => false }))
|
552
|
+
self.version_number = new_photo.version_number
|
553
|
+
self.album_id = new_photo.album_id
|
554
|
+
|
555
|
+
return true
|
556
|
+
else
|
557
|
+
return false
|
558
|
+
end
|
559
|
+
|
560
|
+
end
|
561
|
+
|
562
|
+
end
|
563
|
+
|
564
|
+
class Thumbnail
|
565
|
+
attr_accessor :url, :width, :height
|
566
|
+
end
|
575
567
|
|
576
568
|
end
|
577
569
|
end
|
570
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-picasa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-26 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: xml-simple
|
16
|
-
requirement: &
|
16
|
+
requirement: &86282960 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *86282960
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mime-types
|
27
|
-
requirement: &
|
27
|
+
requirement: &86282750 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *86282750
|
36
36
|
description: Access Picasa Web Album using pure Ruby code.
|
37
37
|
email:
|
38
38
|
- dmitry@trager.ru
|