forked from tanaikech/GPhotoApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPhotoApp.js
242 lines (214 loc) · 5.72 KB
/
GPhotoApp.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
const PhotoApp = new (function () {
// I chose the older-style `new (function() {})()` syntax for
// encpsulating this class so that the interface is more consistent with
// Google's existing ...App singleton instances. --Yuval
let _accessToken = null;
const encodeQueryString = (obj) => {
const pairs = [];
for (const key of Object.keys(obj)) {
const value = obj[key];
if (typeof value === "undefined") {
continue;
}
if ((typeof value === "object") && (value.constructor === Array)) {
for (const el of value) {
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(el.toString()));
}
}
else {
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(value.toString()));
}
}
return pairs.join('&');
};
const _getAccessToken = () => {
if (!_accessToken) {
_accessToken = ScriptApp.getOAuthToken();
}
return _accessToken;
};
const _getUploadToken = ({filename, blob}) => {
const params = {
url: "https://photoslibrary.googleapis.com/v1/uploads",
method: "POST",
muteHttpExceptions: true,
headers: {
"authorization": `Bearer ${_getAccessToken()}`,
"x-goog-upload-file-name": filename,
"x-goog-upload-protocol": "raw"
},
contentType: "application/octet-stream",
payload: blob
};
const res = UrlFetchApp.fetchAll([params])[0];
if (res.getResponseCode() !== 200) {
throw new Error(res.getContentText());
}
return res.getContentText();
};
const _apiCall = ({path, params, payload}) => {
let url = `https://photoslibrary.googleapis.com/v1/${path}`;
if (params) {
const q = encodeQueryString(params);
if (q.length) {
url += '?' + q;
}
}
let request = {
method: "GET",
muteHttpExceptions: true,
headers: {
"authorization": `Bearer ${_getAccessToken()}`
}
}
if (payload) {
request = {
...request,
method: "POST",
contentType: "application/json",
payload: JSON.stringify(payload)
};
}
response = UrlFetchApp.fetch(url, request);
if (response.getResponseCode() !== 200) {
throw new Error(response.getContentText());
}
return JSON.parse(response.getContentText());
};
function* _paginatedApiCall({path, params, payload, ...opts}) {
if (!payload) {
params = params || {};
}
let pageToken = undefined;
while (true) {
// attach pageToken to payload body or query params, depending on request type
if (payload) {
payload = {...payload, pageToken: pageToken};
}
else {
params = {...params, pageToken: pageToken};
}
const body = _apiCall({path, params, payload, ...opts});
if (Object.keys(body).length === 0) {
break;
}
yield body;
pageToken = body.nextPageToken;
if (!pageToken) {
break;
}
}
};
this.createAlbum = (opts) => {
if (!opts) {
throw new Error("Please input resource object.");
}
if (typeof opts === "string") {
opts = {
album: {
title: opts
}
};
}
return _apiCall({
path: "albums",
payload: opts
});
};
this.getAlbumList = function* (opts) {
const pages = _paginatedApiCall({
path: "albums",
params: {
fields: '*',
pageSize: 50,
excludeNonAppCreatedData: opts.excludeNonAppCreatedData
}
});
for (const page of pages) {
for (const album of page.albums) {
yield album;
}
}
};
this.getMediaItemList = function* () {
const pages = _paginatedApiCall({
path: "mediaItems",
params: {
fields: '*',
pageSize: 100
}
});
for (const page of pages) {
for (const mediaItem of page.mediaItems) {
yield mediaItem;
}
}
};
this.searchMediaItems = function* (opts) {
opts = {
pageSize: 100,
...(opts || {})
};
const pages = _paginatedApiCall({
path: "mediaItems:search",
payload: opts
});
for (const page of pages) {
for (const mediaItem of page.mediaItems) {
yield mediaItem;
}
}
};
this.getMediaItems = (opts) => {
return _apiCall({
path: "mediaItems:batchGet",
params: {
mediaItemIds: opts.mediaItemIds
}
});
};
this.getMediaItem = (opts) => {
return _apiCall({
path: `mediaItems/${encodeURIComponent(opts.mediaItemId)}`
});
};
this.getMediaItemBlob = (mediaItem) => {
// TODO : support other baseUrl modifications, as per
// https://developers.google.com/photos/library/guides/access-media-items#base-urls
const url = mediaItem.baseUrl + "=d";
const request = {
method: "GET",
muteHttpExceptions: true,
headers: {
"authorization": `Bearer ${_getAccessToken()}`
}
};
const response = UrlFetchApp.fetch(url, request);
if (response.getResponseCode() !== 200) {
throw new Error(response.getContentText());
}
return response.getBlob();
}
this.uploadMediaItems = (opts) => {
// NOTE : Media items can be created only within the albums created by your app.
if (!opts) {
throw new Error("Please input resource object.");
}
const newMediaItems = opts.items.map((item) => (
{
description: item.description,
simpleMediaItem: {
fileName: item.filename,
uploadToken: _getUploadToken(item)
}
}
));
return _apiCall({
path: "mediaItems:batchCreate",
payload: {
albumId: opts.albumId,
newMediaItems: newMediaItems
}
});
};
})();