-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
88 lines (73 loc) · 2.89 KB
/
index.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
import dotenv from "dotenv";
import { googleAuth } from "./auth";
import { enumerateAlbumMediaItems, getOrCreateAlbum } from "./photosApi";
import { PhotosWebApi } from "./photosWebApi";
import { MediaItem, traverseDirectory } from "./takeout";
interface AlbumRecords {
[key: string]: {
albumName: string;
id?: string;
webItemId?: string;
mediaItems: MediaItem[];
};
}
async function main() {
dotenv.config();
const results = traverseDirectory("Takeout");
const photosWebApi = new PhotosWebApi();
console.log("Summary:");
console.log(`Earliest date: ${results.earliestDate}`);
console.log(`Latest date: ${results.latestDate}`);
console.log(`${Object.keys(results.partner).length} partner photos`);
console.log(`${Object.keys(results.whatsApp).length} WhatsApp photos`);
console.log(`${results.otherCount} other photos`);
const totalPhotos = Object.keys(results.partner).length + Object.keys(results.whatsApp).length + results.otherCount;
console.log(`Total photos: ${totalPhotos}`);
const oauth2Client = await googleAuth();
const albums: AlbumRecords = {
partner: { albumName: "Cleanup - Partner Photos", mediaItems: Object.values(results.partner) },
whatsApp: { albumName: "Cleanup - WhatsApp Photos", mediaItems: Object.values(results.whatsApp) },
};
try {
await photosWebApi.loadCookiesFromFirefoxDb();
for (const album of Object.values(albums)) {
const id = await getOrCreateAlbum(oauth2Client, album.albumName);
album.id = id;
const privateUrl = await photosWebApi.fetchPrivateItemUrl(`https://photos.google.com/lr/album/${id}`);
album.webItemId = privateUrl.split("/").pop();
}
for (const [key, album] of Object.entries(albums)) {
console.log(`Would add ${album.mediaItems.length} photos to album ${album.albumName} - filtering out items already in album...`);
const withoutMediaIds: MediaItem[] = [];
const byMediaId = album.mediaItems.reduce<Record<string, MediaItem>>((acc, item) => {
const mediaItemUrl = photosWebApi.getCachedMediaItemUrl(item.webUrl);
if (mediaItemUrl) {
acc[mediaItemUrl.split("/").pop()] = item;
} else {
withoutMediaIds.push(item);
}
return acc;
}, {});
for await (const mediaItem of enumerateAlbumMediaItems(oauth2Client, album.id!)) {
if (byMediaId[mediaItem.id]) {
delete byMediaId[mediaItem.id];
}
}
const filtered = [...withoutMediaIds, ...Object.values(byMediaId)].map(m => m.webItemId);
console.log("Adding", filtered.length, "photos to album", album.albumName, "...");
await photosWebApi.addMediaItemsToAlbum(album.webItemId!, filtered);
}
} finally {
await photosWebApi.closeDb();
}
}
if (require.main === module) {
(async () => {
try {
await main();
} catch (error) {
console.error(error);
process.exit(1);
}
})();
}