-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
390 lines (355 loc) · 15.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Export Album to Google Slides</title>
<meta charset="utf-8" />
<!-- Bootstrap imported CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
</head>
<body class="m-4">
<!-- Bootstrap imported JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
<h1>Export Album to Google Slides</h1>
<p>
Given a Google Slides presentation and a Google Photos album, this web app will export
the album to the presentation, one photo per slide.
</p>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<!-- ACC HEADER -->
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Set Presentation
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<!-- ACC BODY -->
<div class="accordion-body">
<p>Currently exporting to presentation ID: <span id="pres-name">None</span></p>
<form>
<input id="presentation-link" type="text" class="form-control">
<button type="submit" class="btn btn-secondary mt-2" onclick="savePresentationLink();return false;">Save</button>
</form>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingTwo">
<!-- ACC HEADER -->
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Choose Photo Album
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
<!-- ACC BODY -->
<div class="accordion-body">
<p>Currently exporting photos from: <span id="album-name">None</span></p>
<button id="album_button" type="button" class="btn btn-secondary" onclick="handleAlbumClick()">Choose Album</button>
<form id="album-names" class="m-3">
<!-- These are automatically generated by accessing the user's account when they click the button -->
</form>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingThree">
<!-- ACC HEADER -->
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Export Photos to Presentation
</button>
</h2>
<div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
<!-- ACC BODY -->
<div class="accordion-body">
<form>
<div class="form-check">
<input class="form-check-input" type="radio" name="export-size" id="export-size-1" value="all">
<label class="form-check-label" for="export-size-1">
Export all photos
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="export-size" id="export-side-2" checked>
<label class="form-check-label" for="export-size-2">
<div class="row g-3 align-items-center">
<div class="col-auto">
<label for="export-size-specify" class="form-label">Export specific number:</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="export-size-specify" aria-describedby="size-specify-help">
</div>
<div id="size-specify-help" class="form-text col-auto">
If this exceeds album size, all photos will be exported.
</div>
</div>
</label>
</div>
</form>
<button id="export_button" type="button" class="btn btn-primary" onclick="handleExportClick()">Export</button>
</div>
</div>
</div>
</div>
<pre id="content" style="white-space: pre-wrap;"></pre>
<script type="text/javascript">
const CLIENT_ID = '1020653963797-3f84m28d0b0luec011o3co6fppcc4o00.apps.googleusercontent.com';
const API_KEY = 'AIzaSyBWN81RTcZL9c4RnDButprJQey0EWjS7XY';
// Discovery doc URL for APIs used by the quickstart
const DISCOVERY_DOC = 'https://slides.googleapis.com/$discovery/rest?version=v1';
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
const SCOPES = 'https://www.googleapis.com/auth/presentations https://www.googleapis.com/auth/photoslibrary.readonly';
let tokenClient;
let gapiInited = false;
let gisInited = false;
let presentationInfo = {};
let albumInfo;
/**
* Callback after api.js is loaded.
*/
function gapiLoaded() {
gapi.load('client', intializeGapiClient);
}
/**
* Callback after the API client is loaded. Loads the
* discovery doc to initialize the API.
*/
async function intializeGapiClient() {
await gapi.client.init({
apiKey: API_KEY,
discoveryDocs: [DISCOVERY_DOC],
});
gapiInited = true;
}
/**
* Callback after Google Identity Services are loaded.
*/
function gisLoaded() {
tokenClient = google.accounts.oauth2.initTokenClient({
client_id: CLIENT_ID,
scope: SCOPES,
callback: '', // defined later
});
gisInited = true;
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick() {
tokenClient.callback = async (resp) => {
if (resp.error !== undefined) {
throw (resp);
}
document.getElementById('signout_button').style.visibility = 'visible';
document.getElementById('authorize_button').innerText = 'Refresh';
};
if (gapi.client.getToken() === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
tokenClient.requestAccessToken({prompt: 'consent'});
} else {
// Skip display of account chooser and consent dialog for an existing session.
tokenClient.requestAccessToken({prompt: ''});
}
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick() {
const token = gapi.client.getToken();
if (token !== null) {
google.accounts.oauth2.revoke(token.access_token);
gapi.client.setToken('');
document.getElementById('content').innerText = '';
document.getElementById('authorize_button').innerText = 'Authorize';
document.getElementById('signout_button').style.visibility = 'hidden';
}
}
function savePresentationLink() {
// link looks like https://docs.google.com/presentation/d/{DESIRED_ID}/{OTHER_STUFF}
console.log("trying to save prez link");
const linkParts = document.getElementById('presentation-link').value.split('/d/');
presentationInfo.id = linkParts[1].split('/')[0];
// TODO: set to pres name, not Id ugh this is hard
document.getElementById('pres-name').innerText = presentationInfo.id;
}
/*
* Export the user's photos upon button click.
*/
function handleExportClick() {
tokenClient.callback = async (resp) => {
if (resp.error !== undefined) {
throw (resp);
}
await addPhotoSlides();
};
if (gapi.client.getToken() === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
tokenClient.requestAccessToken({prompt: 'consent'});
} else {
// Skip display of account chooser and consent dialog for an existing session.
tokenClient.requestAccessToken({prompt: ''});
}
}
function handleAlbumClick() {
console.log("handler called");
tokenClient.callback = async (resp) => {
if (resp.error !== undefined) {
console.log("error");
throw (resp);
} else {
console.log("noterror");
}
console.log("awaiting");
await getAlbums();
};
if (gapi.client.getToken() === null) {
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
tokenClient.requestAccessToken({prompt: 'consent'});
} else {
// Skip display of account chooser and consent dialog for an existing session.
tokenClient.requestAccessToken({prompt: ''});
}
}
async function getAlbums() {
console.log("called");
gapi.client.request({
'path': 'https://photoslibrary.googleapis.com/v1/albums',
'method': 'GET',
}).then(function (response) {
console.log(JSON.parse(response.body));
albums = JSON.parse(response.body).albums;
const options = document.getElementById('album-names');
for (album of albums) {
const container = document.createElement("div");
container.setAttribute("class", "form-check");
const option = document.createElement("input");
const label = document.createElement("label");
option.setAttribute("class", "form-check-input");
option.setAttribute("id", album.title);
option.setAttribute('value',
`{"size": ${album.mediaItemsCount},
"id": "${album.id}",
"name": "${album.title}"}`);
option.setAttribute("type", "radio");
option.setAttribute('name', 'album');
label.setAttribute("class", "form-check-label");
label.setAttribute('for', album.title);
label.appendChild(document.createTextNode(`${album.title} (${album.mediaItemsCount} photos)`));
container.appendChild(option);
container.appendChild(label);
options.appendChild(container);
}
const submit = document.createElement("button");
submit.setAttribute("type", "submit");
submit.setAttribute('class', "btn btn-primary mt-1");
submit.appendChild(document.createTextNode("Save"));
submit.setAttribute('onclick', "albumInfo = getSelectedAlbumInfo();return false;");
options.appendChild(submit);
// TODO get info out of URL somehow
console.log('albuminfo is', albumInfo);
});
}
function getSelectedAlbumInfo() {
const selected = document.querySelector('input[type=radio][name=album]:checked');
console.log('s is', selected);
const info = JSON.parse(selected.value);
document.getElementById('album-name').innerText = info.name;
return info;
}
/*
* Add the slides from a specified Google Photos album to a specific Google Slides slideshow,
* one photo per slide.
*/
async function addPhotoSlides() {
const createrequests = [];
const addrequests = [];
let pageSize;
const selected = document.querySelector('input[type=radio][name=export-size]:checked');
if (selected.value == "all") { // User wants all photos
pageSize = albumInfo.size;
} else { // User specified how many photos
pageSize = document.getElementById('export-size-specify').value;
}
console.log(pageSize);
gapi.client.request({
'path': 'https://photoslibrary.googleapis.com/v1/mediaItems:search',
'method': 'POST',
'body': {
"albumId": albumInfo.id,
"pageSize": pageSize, // This code only reads the first page
}
}).then(function (response) {
const album = (JSON.parse(response.body)).mediaItems;
// Change this to change which slideshow they export to
pageId = 'pageid';
imageId = 'imageid';
counter = 0; // TODO should start the counter at the length of the current presentation to allow for double-pushes ugh this is hard
for (image of album) {
createrequests.push({
createSlide: {
objectId: pageId + counter,
slideLayoutReference: {
// This uses Google's layout names - if you change the corresponding layout by editing
// the slide master in the presentation, that change will be reflected here
predefinedLayout: 'TITLE_AND_TWO_COLUMNS',
},
},
});
const imageUrl = image.baseUrl + "=w1500";
const emu4M = {
magnitude: 4000000,
unit: 'EMU',
};
addrequests.push({
createImage: {
objectId: imageId + counter,
url: imageUrl,
elementProperties: {
pageObjectId: pageId + counter,
size: {
height: emu4M,
width: emu4M,
},
transform: {
scaleX: 1,
scaleY: 1,
translateX: 500000,
translateY: 1000000,
unit: 'EMU',
},
},
},
});
counter += 1;
}
try {
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationInfo.id,
requests: createrequests,
}).then((createSlideResponse) => {
console.log(`Created slide with ID: ${createSlideResponse.result.replies[0].createSlide.objectId}`);
console.log(`Created ${createSlideResponse.result.replies.length} slides`);
gapi.client.slides.presentations.batchUpdate({
presentationId: presentationInfo.id,
requests: addrequests,
}).then((createImageResponse) => {
console.log(`Created image with ID: ${createImageResponse.result.replies[0].createImage.objectId}`);
console.log(`Created ${createImageResponse.result.replies.length} images`);
// TODO add a user-visible success statement (probably smth that also changes with the
// content thing in the error catch, that should die)
});
});
} catch (err) {
document.getElementById('content').innerText = err.message;
return;
}
});
}
</script>
<script async defer src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
<script async defer src="https://accounts.google.com/gsi/client" onload="gisLoaded()"></script>
</body>
</html>