-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.js
38 lines (36 loc) · 1.01 KB
/
fetcher.js
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
// fetcher.js
const getDriveService = require("./service");
require('dotenv').config()
const fetchLatestNewsletter = async () => {
const folderId = process.env.FOLDER_ID;
const driveService = getDriveService();
console.log("Fetching latest newsletter...");
const res = await driveService.files.list({
includeItemsFromAllDrives: true,
supportsAllDrives: true,
q: `'${folderId}' in parents and trashed = false`,
fields: "nextPageToken, files(*)",
orderBy: "createdTime desc",
pageSize: 1,
});
if (res.data.files) {
const fileFound = res.data.files[0];
console.log("file found", fileFound.name);
const fileId = fileFound.id;
const title = fileFound.name.replace(".pdf", "").replace(/_/g, " ");
return {
url: `https://drive.google.com/file/d/${fileId}/preview`,
fileFound: true,
fileId: fileId,
title,
};
} else {
return {
url: "",
fileFound: false,
fileId: "",
title: "",
};
}
};
module.exports = fetchLatestNewsletter;