-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
78 lines (74 loc) · 2.62 KB
/
helpers.ts
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
import * as coda from "@codahq/packs-sdk";
import * as types from "types/common-types";
import GPhotos from "./api";
export async function getConnectionName(context: coda.ExecutionContext) {
let request: coda.FetchRequest = {
method: "GET",
url: "https://www.googleapis.com/oauth2/v1/userinfo",
headers: {
"Content-Type": "application/json",
},
};
let userResponse = await context.fetcher.fetch(request);
let user = userResponse.body;
return user.name as string;
}
// Parse the response from the API to the format we want to return
// Matches the MediaSchema schema defined in ./schemas.ts
export function mediaItemsParser(mediaItems: types.MediaItemResponse[]): types.MediaItem[] {
return mediaItems.map((mediaItem) => {
let { id, filename, mimeType, description, productUrl } = mediaItem;
let { creationTime, photo, video, width, height } = mediaItem.mediaMetadata;
return {
mediaId: id,
filename,
mediaType: (photo) ? "Photo" : "Video",
mimeType,
description,
creationTime,
mediaMetadata: { photo, video },
width,
height,
image: `${mediaItem.baseUrl}=w2048-h1024`,
url: productUrl,
}
});
}
export async function getMediaItemsFromAlbum(albumId: string, context: coda.ExecutionContext) {
const photos = new GPhotos(context.fetcher);
let mediaItems: types.Album['mediaItems'] = [];
let nextPageToken: string | undefined;
do {
const response = await photos.mediaItems.search(albumId, nextPageToken, 100, 'mediaItems(id),nextPageToken')
const mediaItemsRes = response.body?.mediaItems as types.MediaItemIdRes[];
if (mediaItemsRes) {
mediaItems = mediaItems.concat(mediaItemsRes.map((mediaItem) => {
return { mediaId: mediaItem.id, filename: "Not found" }
}));
}
nextPageToken = response.body?.nextPageToken as string | undefined;
} while (nextPageToken);
return { mediaItems };
}
// Parse the response from the API to the format we want to return
// Matches the AlbumSchema schema defined in ./schemas.ts
export function albumParser(albums: types.AlbumResponse[]): types.Album[] {
return albums.map((album) => {
let { id, title, productUrl, coverPhotoBaseUrl, coverPhotoMediaItemId } = album;
let coverPhotoMediaItem: types.Album['coverPhotoMediaItem'] = undefined;
if (coverPhotoMediaItemId) {
coverPhotoMediaItem = {
filename: "Not found",
mediaId: coverPhotoMediaItemId,
}
}
return {
albumId: id,
title,
url: productUrl,
mediaItems: [],
coverPhoto: `${coverPhotoBaseUrl}=w2048-h1024`,
coverPhotoMediaItem,
}
});
}