forked from benlind/random-image-emailer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom-image-emailer.gs
61 lines (48 loc) · 1.82 KB
/
random-image-emailer.gs
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
// Send random Google Photos image in an email
// Here, we send it to IFTTT as an attachment
// You can then use IFTTT to do whatever with the image
// Image date is also included in the body and can be used
function sendRandomImage() {
// Google Photos Album ID
// Can be obtained using the API tester on the right
// https://developers.google.com/photos/library/reference/rest/v1/albums/list
var albumId = "";
var headers = {"Authorization": "Bearer " + ScriptApp.getOAuthToken()};
var url = "https://photoslibrary.googleapis.com/v1/mediaItems:search";
var mediaItems = [];
var pageToken = "";
do
{
var params = {
method: "post",
headers: headers,
contentType: "application/json",
payload: JSON.stringify({albumId: albumId, pageSize: 100, pageToken: pageToken}),
}
var res = UrlFetchApp.fetch(url, params);
var obj = JSON.parse(res.getContentText());
Array.prototype.push.apply(mediaItems, obj.mediaItems);
pageToken = obj.nextPageToken || "";
} while (pageToken);
var photos = mediaItems;
// Gets random photo
var randomPhoto = photos[Math.floor(Math.random() * photos.length)]
// Gets image creation date
var date = randomPhoto.mediaMetadata.creationTime
// Email parameters
var to = "[email protected]" // comma-separated list of email addresses
var subject = "Random Google Photo" // subject line
var body = `${date}` // email body
// Takes image URL and converts to blob
// This is necessary to use it as an attachment
var randomPhotoBlob = UrlFetchApp
.fetch(randomPhoto.baseUrl)
.getBlob();
// Attach img
// https://developers.google.com/apps-script/reference/mail/mail-app#advanced-parameters
MailApp.sendEmail({
to: to,
subject: subject,
body: body,
attachments: [randomPhotoBlob]
});