-
Notifications
You must be signed in to change notification settings - Fork 2
/
expensive_expenses.js
61 lines (49 loc) · 1.66 KB
/
expensive_expenses.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const today = new Date();
const firstDayOfMonth = new Date(Date.UTC(today.getFullYear(), today.getMonth(), 1, 8));
const { Client } = require('@notionhq/client');
const databaseId = process.env.NOTION_DATABASE_ID;
const apiKey = process.env.NOTION_API_KEY;
const notion = new Client({ auth: apiKey });
async function fetchAllPages(databaseId) {
let allResults = [];
let hasNextPage = true;
let startCursor = null;
while (hasNextPage) {
try {
const requestOptions = {
database_id: databaseId,
filter: {
property: '时间',
date: {
on_or_after: firstDayOfMonth.toISOString() }
}
};
if (startCursor) {
requestOptions.start_cursor = startCursor;
}
const response = await notion.databases.query(requestOptions);
const data = response;
console.log('Current response data:', data);
if (data.results) {
console.log('Current page results:', data.results);
allResults.push(...data.results);
}
hasNextPage = data.has_more;
startCursor = data.next_cursor;
} catch (error) {
console.log('Error in request:', error);
}
}
return allResults;
}
async function main() {
const pages = await fetchAllPages(databaseId);
console.log('Pages fetched:', pages);
const expensiveCount = pages.filter((page) => {
const priceProperty = Object.entries(page.properties).find(([key, value]) => key === "价格");
const price = priceProperty ? (priceProperty[1].number !== null ? priceProperty[1].number : 0) : 0;
return price > 500;
}).length;
console.log('Number of expensive expenses:', expensiveCount);
}
main();