-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.ts
122 lines (107 loc) · 3.94 KB
/
pack.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
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
import * as coda from "@codahq/packs-sdk";
import * as helpers from "./helpers";
import * as schemas from "./schemas";
import * as types from "./types/api-types";
import * as params from "./params";
import GPhotos from "./api";
export const pack = coda.newPack();
pack.addNetworkDomain("googleapis.com");
pack.setUserAuthentication({
type: coda.AuthenticationType.OAuth2,
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
tokenUrl: "https://oauth2.googleapis.com/token",
scopes: [
"profile",
"https://www.googleapis.com/auth/photoslibrary.readonly",
],
additionalParams: {
access_type: "offline",
prompt: "consent",
},
getConnectionName: helpers.getConnectionName,
});
pack.addSyncTable({
name: "Medias",
schema: schemas.MediaSchema,
identityName: "Media",
formula: {
name: "SyncMedias",
description: "Sync medias from the user's library.",
parameters: [
params.MediaDateRange,
params.MediaCategoriesIncludeOpt,
params.MediaCategoriesExcludeOpt,
params.MediaTypeOptional,
params.MediaFavoritesOptional,
params.MediaArchivedOptional,
],
execute: async function ([dateRange, categoriesToInclude, categoriesToExclude, mediaType, favorite, archived], context) {
let photos = new GPhotos(context.fetcher);
const filters = new photos.Filters(archived);
// Date filter
if (!dateRange) {
throw new coda.UserVisibleError("Date range is required.");
}
const dateFilter = new photos.DateFilter();
dateFilter.addRange(dateRange[0], dateRange[1]);
filters.setDateFilter(dateFilter);
// Content filter
const contentFilter = new photos.ContentFilter();
switch (categoriesToInclude || categoriesToExclude) {
case categoriesToInclude:
categoriesToInclude?.forEach((category) => {
contentFilter.addIncludedCategory(category as types.MediasContentCategories);
});
break;
case categoriesToExclude:
categoriesToExclude?.forEach((category) => {
contentFilter.addExcludedCategory(category as types.MediasContentCategories);
});
break;
}
filters.setContentFilter(contentFilter);
// Media type filter
const mediaTypeFilter = new photos.MediaTypeFilter(mediaType as types.MediaTypes);
// mediaTypeFilter.setType(mediaType as types.MediaTypes)
filters.setMediaTypeFilter(mediaTypeFilter);
// Feature filter (favorites)
if (favorite) {
const featureFilter = new photos.FeatureFilter(types.MediaFeature.Favorites)
filters.setFeatureFilter(featureFilter);
}
const response = await photos.mediaItems.search(filters, (context.sync.continuation?.nextPageToken as string | undefined))
const { nextPageToken, mediaItems } = response?.body ?? {};
return {
result: mediaItems ? helpers.mediaItemsParser(mediaItems as types.MediaItemResponse[]) : null,
continuation: nextPageToken ? { nextPageToken } : undefined
}
},
},
});
pack.addSyncTable({
name: "Albums",
schema: schemas.AlbumSchema,
identityName: "Album",
formula: {
name: "SyncAlbums",
description: "Sync all albums.",
parameters: [],
execute: async function ([], context) {
const photos = new GPhotos(context.fetcher);
const response = await photos.albums.list(20, (context.sync.continuation?.nextPageToken as string | undefined));
const { albums, nextPageToken } = response?.body;
const parsedAlbums = albums ? helpers.albumParser(albums) : null;
if (parsedAlbums) {
for (const album of parsedAlbums) {
const { albumId } = album;
const { mediaItems } = await helpers.getMediaItemsFromAlbum(albumId, context);
album.mediaItems = mediaItems ?? undefined;
}
}
return {
result: parsedAlbums,
continuation: nextPageToken ? { nextPageToken } : undefined
}
}
}
});