-
Notifications
You must be signed in to change notification settings - Fork 2
/
spent_on_food.js
73 lines (60 loc) · 1.89 KB
/
spent_on_food.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
62
63
64
65
66
67
68
69
70
71
72
73
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: {
and: [
{
property: '时间',
date: {
on_or_after: firstDayOfMonth.toISOString() }
},
{
property: '类目',
select: {
equals: '餐饮'
}
}
]
}
};
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 foodAmount = pages.reduce((acc, page) => {
const priceProperty = Object.entries(page.properties).find(([key, value]) => key === "价格");
const price = priceProperty ? (priceProperty[1].number !== null ? priceProperty[1].number : 0) : 0;
console.log('Price:', price);
return acc + price;
}, 0);
console.log('Total amount spent on food:', foodAmount);
}
main();