-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.ts
125 lines (106 loc) · 4.06 KB
/
list.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
123
124
125
import fs from 'fs';
import { OAuth2Client } from 'google-auth-library';
import { google } from 'googleapis';
import path from 'path';
import { authorize } from './auth';
const FULL_SEND = process.env['FULL_SEND']?.toLowerCase() === 'true';
const OUTPUT_PATH = './out';
(async () => {
fs.mkdirSync(OUTPUT_PATH, { recursive: true });
const rootFolder = {
id: '1AQ27Ov9hwjg3-KXXv-GvPq8hul4nxafV',
name: 'Private'
};
const auth = await authorize();
const modelFolders = await fetchModelFolders(auth, rootFolder);
for (const modelFolder of modelFolders) {
const fileFolder = await fetchFolderByName(auth, modelFolder.name);
const mediaFiles = await fetchMediaFiles(auth, fileFolder);
console.log(`Discovered ${mediaFiles.length} media files`);
const outputFilename = `${path.resolve(OUTPUT_PATH)}/${modelFolder.name}_files.json`;
fs.writeFileSync(outputFilename, JSON.stringify(mediaFiles, null, ' '), { encoding: 'utf-8' });
console.log('Media files list written to', outputFilename);
console.log();
}
})();
interface FolderFile {
id: string;
name: string;
}
export interface MediaFile {
id: string;
name: string;
size: string;
mimeType: string;
}
/**
* Retrieve a list of model folders.
* @param auth An authorized OAuth2 client.
* @param rootFolder The root folder containing the model folders.
* @returns A promise that resolve toa list of model folders.
*/
async function fetchModelFolders(auth: OAuth2Client, rootFolder: FolderFile): Promise<FolderFile[]> {
const drive = google.drive({ version: 'v3', auth });
const response = await drive.files.list({
q: `mimeType = 'application/vnd.google-apps.folder' and '${rootFolder.id}' in parents`,
pageSize: 50,
fields: 'files(id, name)',
});
if (response.data.files?.length) {
return response.data.files as FolderFile[];
} else {
throw new Error(`Unable to retrieve model folders from "${rootFolder.name}" (${rootFolder.id}).`);
}
}
/**
* Retrieve a folder by its name.
* @param auth An authorized OAuth2 client.
* @param folderName The folder's name.
* @returns A promise that resolves to a folder file object.
*/
async function fetchFolderByName(auth: OAuth2Client, folderName: string): Promise<FolderFile> {
const drive = google.drive({ version: 'v3', auth });
const response = await drive.files.list({
q: `mimeType = 'application/vnd.google-apps.folder' and name = '${folderName}'`,
pageSize: 1,
fields: 'files(id, name)',
});
if (response.data.files?.length) {
return response.data.files[0] as FolderFile;
} else {
throw new Error(`A folder with the name "${folderName}" was not found.`);
}
}
/**
* Recursively retrieve a list of media files.
* @param auth An authorized OAuth2 client.
* @param folder The folder to start from.
* @returns A promise that resolves to a list of media file objects.
*/
async function fetchMediaFiles(auth: OAuth2Client, folder: FolderFile): Promise<MediaFile[]> {
const drive = google.drive({ version: 'v3', auth });
let fileList: MediaFile[] = [];
let pageToken: string | undefined = undefined;
do {
const response = await drive.files.list({
q: `'${folder.id}' in parents`,
orderBy: 'name_natural',
pageSize: FULL_SEND ? 1000 : 10,
fields: 'nextPageToken, files(id, name, size, mimeType)',
pageToken: pageToken
});
pageToken = FULL_SEND ? response.data.nextPageToken : undefined;
const files: MediaFile[] = response.data.files;
if (files) {
for (const file of files) {
if (file.mimeType == 'application/vnd.google-apps.folder') {
fileList = fileList.concat(await fetchMediaFiles(auth, file));
} else {
fileList.push(file);
console.debug(`${file.name} | ${file.id} | ${file.size}`);
}
}
}
} while (pageToken);
return fileList;
}