-
Notifications
You must be signed in to change notification settings - Fork 24
/
picasa.js
427 lines (357 loc) · 13.5 KB
/
picasa.js
1
2
3
4
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
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
'use strict'
const querystring = require('querystring')
const promisify = require('./promisify')
const executeRequest = require('./executeRequest')
const GOOGLE_AUTH_ENDPOINT = 'https://accounts.google.com/o/oauth2/auth'
const GOOGLE_API_HOST = 'https://www.googleapis.com'
const GOOGLE_API_PATH = '/oauth2/v3/token'
const GOOGLE_API_PATH_NEW = '/oauth2/v4/token'
const PICASA_SCOPE = 'https://picasaweb.google.com/data'
const PICASA_API_FEED_PATH = '/feed/api/user/default'
const PICASA_API_ENTRY_PATH = '/entry/api/user/default'
const FETCH_AS_JSON = 'json'
/** Main class */
class Picasa {
/**
* Creates an instance of Picasa.
*/
constructor() {
this.executeRequest = executeRequest
this.getAccessToken = getAccessToken.bind(this)
}
/**
* Get Photos
* @param {string} accessToken - See {@link Picasa#getTokens}
* @param {object} options - Can be empty object
* @param {integer} options.maxResults - By default get all photos
* @param {string} options.albumId - By default all photos are selected
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {Promise}
*/
getPhotos() {
return promisify.bind(this)(getPhotos, arguments)
}
/**
* Create Photos
* @param {string} accessToken - See {@link Picasa#getTokens}
* @param {string} albumId
* @param {object} photoData - Photo's propperties
* @param {string} photoData.title
* @param {string} photoData.summary
* @param {string} photoData.contentType - image/bmp, image/gif, image/png
* @param {blob} photoData.binary - Blob binary
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {Promise}
*/
postPhoto() {
return promisify.bind(this)(postPhoto, arguments)
}
/**
* Delete Photo
* @param {string} accessToken - See {@link Picasa#getTokens}
* @param {string} albumId
* @param {string} photoId
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {Promise}
*/
deletePhoto() {
return promisify.bind(this)(deletePhoto, arguments)
}
/**
* Get all Albums
* @param {string} accessToken - See {@link Picasa#getTokens}
* @param {object} options - Can be empty object
* @param {integer} options.TODO - TODO
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {Promise}
*/
getAlbums() {
return promisify.bind(this)(getAlbums, arguments)
}
/**
* Create an albums
* @param {string} accessToken - See {@link Picasa#getTokens}
* @param {object} albumData - Can be empty object
* @param {string} albumData.title
* @param {string} albumData.summary
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {Promise}
*/
createAlbum() {
return promisify.bind(this)(createAlbum, arguments)
}
/**
* Get access token and refresh token
* @param {object} config - Get config here: {@link https://console.developers.google.com/home/dashboard} (API Manager > Credentials)
* @param {string} config.clientId
* @param {string} config.redirectURI - URL that user was redirected. After google displays a consent screen to the user, user will be redirect to this URL with a `code` in the URL
* @param {string} config.clientSecret
* @param {string} code - Get code from URL param, when user is redirected from authURL. See {@link Picasa#getAuthURL}
* @param {function} callback - (error, response{accessToken, refreshToken}). If not provided, a promise will be returned
* @returns {Promise} - Object{accessToken, refreshToken}
*/
getTokens() {
return promisify.bind(this)(getTokens, arguments)
}
/**
* Renews access token
* @param {object} config - Get config here: {@link https://console.developers.google.com/home/dashboard} (API Manager > Credentials)
* @param {string} config.clientId
* @param {string} config.redirectURI - URL that user was redirected. After google displays a consent screen to the user, user will be redirect to this URL with a `code` in the URL
* @param {string} config.clientSecret
* @param {string} refreshToken - The refreshToken is retrived after getTokens is executed. See {@link Picasa#getTokens}
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {Promise}
*/
renewAccessToken() {
return promisify.bind(this)(renewAccessToken, arguments)
}
/**
* Get Auth URL. Redirect user to this URL to get code. The code will be used later for {@link Picasa#getTokens}
* @param {object} config - Get config here: https://console.developers.google.com/home/dashboard (API Manager > Credentials)
* @param {string} config.clientId
* @param {string} config.redirectURI - URL to user will be redirected. After google displays a consent screen to the user, user will be redirect to this URL with a `code` in the URL
* @param {function} callback - (error, response). If not provided, a promise will be returned
* @returns {string}
*/
getAuthURL(config) {
const authenticationParams = {
access_type: 'offline',
scope: `${PICASA_SCOPE}`,
response_type: 'code',
client_id: config.clientId,
redirect_uri: config.redirectURI
}
const authenticationQuery = querystring.stringify(authenticationParams)
return `${GOOGLE_AUTH_ENDPOINT}?${authenticationQuery}`
}
}
function getAlbums(accessToken, options, callback) {
const accessTokenParams = {
alt: FETCH_AS_JSON,
access_token: accessToken
}
options = options || {}
const requestQuery = querystring.stringify(accessTokenParams)
const requestOptions = {
url: `${PICASA_SCOPE}${PICASA_API_FEED_PATH}?${requestQuery}`,
headers: {
'GData-Version': '2'
}
}
this.executeRequest('get', requestOptions, (error, body) => {
if (error) return callback(error)
const albums = body.feed.entry.map(
entry => parseEntry(entry, albumSchema)
)
callback(null, albums)
})
}
function deletePhoto(accessToken, albumId, photoId, callback) {
const requestQuery = querystring.stringify({
alt: FETCH_AS_JSON,
access_token: accessToken
})
const requestOptions = {
url: `${PICASA_SCOPE}${PICASA_API_ENTRY_PATH}/albumid/${albumId}/photoid/${photoId}?${requestQuery}`,
headers: {
'If-Match': '*'
}
}
this.executeRequest('del', requestOptions, callback)
}
function createAlbum(accessToken, albumData, callback) {
const requestQuery = querystring.stringify({
alt: FETCH_AS_JSON,
access_token: accessToken
})
const albumInfoAtom = `<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'>${albumData.title}</title>
<summary type='text'>${albumData.summary}</summary>
<gphoto:access>private</gphoto:access>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/photos/2007#album'></category>
</entry>`
const requestOptions = {
url: `${PICASA_SCOPE}${PICASA_API_FEED_PATH}?${requestQuery}`,
body: albumInfoAtom,
headers: { 'Content-Type': 'application/atom+xml' }
}
this.executeRequest('post', requestOptions, (error, body) => {
if (error) return callback(error)
const album = parseEntry(body.entry, albumSchema)
callback(error, album)
})
}
function postPhoto(accessToken, albumId, photoData, callback) {
const requestQuery = querystring.stringify({
alt: FETCH_AS_JSON,
access_token: accessToken
})
const photoInfoAtom = `<entry xmlns="http://www.w3.org/2005/Atom">
<title>${photoData.title}</title>
<summary>${photoData.summary}</summary>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/photos/2007#photo"/>
</entry>`
const requestOptions = {
url: `${PICASA_SCOPE}${PICASA_API_FEED_PATH}/albumid/${albumId}?${requestQuery}`,
multipart: [
{ 'Content-Type': 'application/atom+xml', body: photoInfoAtom },
{ 'Content-Type': photoData.contentType, body: photoData.binary }
]
}
this.executeRequest('post', requestOptions, (error, body) => {
if (error) return callback(error)
const photo = parseEntry(body.entry, photoSchema)
callback(error, photo)
})
}
function getPhotos(accessToken, options, callback) {
const accessTokenParams = {
alt: FETCH_AS_JSON,
kind: 'photo',
access_token: accessToken
}
options = options || {}
if (options.maxResults) accessTokenParams['max-results'] = options.maxResults
if (options.startIndex) accessTokenParams['start-index'] = options.startIndex
if (options.imgMax) accessTokenParams['imgmax'] = options.imgMax
const albumPart = options.albumId ? `/albumid/${options.albumId}` : ''
const requestQuery = querystring.stringify(accessTokenParams)
const requestOptions = {
url: `${PICASA_SCOPE}${PICASA_API_FEED_PATH}${albumPart}?${requestQuery}`,
headers: {
'GData-Version': '2'
}
}
this.executeRequest('get', requestOptions, (error, body) => {
if (error) return callback(error)
const photos = body.feed.entry.map(
entry => parseEntry(entry, photoSchema)
)
callback(null, photos)
})
}
const albumSchema = {
'media$group.media$thumbnail': 'thumbnail',
'gphoto$id': 'id',
'gphoto$name': 'name',
'gphoto$numphotos': 'num_photos',
'published': 'published',
'title': 'title',
'summary': 'summary',
'gphoto$location': 'location',
'gphoto$nickname': 'nickname',
'rights': 'rights',
'gphoto$access': 'access'
}
const photoSchema = {
'gphoto$id': 'id',
'gphoto$albumid': 'album_id',
'gphoto$access': 'access',
'gphoto$width': 'width',
'gphoto$height': 'height',
'gphoto$size': 'size',
'gphoto$checksum': 'checksum',
'gphoto$timestamp': 'timestamp',
'gphoto$imageVersion': 'image_version',
'gphoto$commentingEnabled': 'commenting_enabled',
'gphoto$commentCount': 'comment_count',
'content': 'content',
'title': 'title',
'summary': 'summary'
}
function parseEntry(entry, schema) {
let photo = {}
Object.keys(schema).forEach(schemaKey => {
const key = schema[schemaKey]
if (key) {
const value = extractValue(entry, schemaKey, key)
photo[key] = value
}
})
return photo
}
function extractValue(entry, schemaKey) {
if (schemaKey.indexOf('.') !== -1) {
const tempKey = schemaKey.split('.')[0]
return extractValue(checkParam(entry[tempKey]), schemaKey.replace(`${tempKey}.`, ''))
}
return checkParam(entry[schemaKey])
}
function getAuthURL(config) {
const authenticationParams = {
access_type: 'offline',
scope: `${PICASA_SCOPE}`,
response_type: 'code',
client_id: config.clientId,
redirect_uri: config.redirectURI
}
const authenticationQuery = querystring.stringify(authenticationParams)
return `${GOOGLE_AUTH_ENDPOINT}?${authenticationQuery}`
}
/**
* Get access token. To be deprecated soon. Use {@link Picasa#getTokens} instead
* @param {object} config - Get config here: {@link https://console.developers.google.com/home/dashboard} (API Manager > Credentials)
* @param {string} config.clientId
* @param {string} config.redirectURI - URL that user was redirected. After google displays a consent screen to the user, user will be redirect to this URL with a `code` in the URL
* @param {string} config.clientSecret
* @param {string} code - Get code from URL param, when user is redirected from authURL. See {@link Picasa#getAuthURL}
* @param {function} callback - (error, response{accessToken, refreshToken}). If not provided, a promise will be returned
* @returns {Promise} - String accessToken
*/
function getAccessToken(config, code, callback) {
const accessTokenParams = {
grant_type: 'authorization_code',
code: code,
redirect_uri: config.redirectURI,
client_id: config.clientId,
client_secret: config.clientSecret
}
const requestQuery = querystring.stringify(accessTokenParams)
const options = {
url: `${GOOGLE_API_HOST}${GOOGLE_API_PATH}?${requestQuery}`
}
this.executeRequest('post', options, (error, body) => {
if (error) return callback(error)
callback(null, body.access_token, body.refresh_token)
})
}
function getTokens(config, code, callback) {
return getAccessToken(config, code, (error, accessToken, refreshToken) => {
if (error) return callback(error)
const tokens = {
accessToken,
refreshToken,
}
callback(null, tokens)
})
}
function renewAccessToken(config, refresh_token, callback) {
const refreshTokenParams = {
grant_type: 'refresh_token',
client_id: config.clientId,
client_secret: config.clientSecret,
refresh_token: refresh_token
}
const requestQuery = querystring.stringify(refreshTokenParams)
const options = {
url: `${GOOGLE_API_HOST}${GOOGLE_API_PATH_NEW}?${requestQuery}`
}
this.executeRequest('post', options, (error, body) => {
if (error) return callback(error)
callback(null, body.access_token)
})
}
function checkParam(param) {
if (param === undefined) return ''
else if (isValidType(param)) return param
else if (isValidType(param['$t'])) return param['$t']
else return param
}
function isValidType(value) {
return typeof value === 'string' || typeof value === 'number'
}
module.exports = Picasa