This repository was archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion-writer.ts
103 lines (94 loc) · 2.66 KB
/
notion-writer.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
import { Client } from "@notionhq/client";
import {
CreatePageParameters,
GetDatabaseResponse,
} from "@notionhq/client/build/src/api-endpoints";
import { JobPosting } from "./scraper";
import * as _ from "lodash";
import { config } from "dotenv";
config();
const notion = new Client({ auth: process.env["NOTION_TOKEN"] });
function removePunctuation(text: string): string {
return text.replace(/[^\w\s]|_/g, "");
}
function makePropertiesData(
properties: GetDatabaseResponse["properties"],
jobData: JobPosting
): Record<string, CreatePageParameters["properties"]> {
const propertyValues: Record<string, CreatePageParameters["properties"]> = {};
type TextObject = {
type: "text";
text: { content: string };
};
let processedDescription: TextObject[] = [];
const maxLength = 2000;
for (let i = 0; i < jobData.description.length; i += maxLength) {
const part = jobData.description.substr(i, maxLength);
processedDescription.push({
type: "text",
text: { content: part },
});
}
Object.entries(properties).forEach(([name, property]) => {
if (name === "Company") {
propertyValues[name] = {
type: "title",
title: [
{
type: "text",
text: {
content: jobData.company,
},
},
],
};
} else if (name === "Apply_Date") {
propertyValues[name] = {
type: "date",
date: {
start: new Date().toISOString(),
},
};
} else if (name === "URL") {
propertyValues[name] = {
type: "url",
id: property.id,
url: jobData.url,
};
} else if (name === "Description") {
propertyValues[name] = {
type: "rich_text",
id: property.id,
rich_text: processedDescription,
};
} else if (name === "Role") {
propertyValues[name] = {
type: "select",
id: property.id,
select: { name: removePunctuation(jobData.title) },
};
}
});
return propertyValues;
}
async function exerciseWriting(
databaseId: string,
properties: GetDatabaseResponse["properties"],
data: JobPosting
) {
const propertiesData = makePropertiesData(properties, data);
const parameters: CreatePageParameters = {
parent: {
database_id: databaseId,
},
properties: propertiesData,
} as CreatePageParameters;
await notion.pages.create(parameters);
}
export async function writeToNotion(data: JobPosting | null) {
const database_id: string = process.env["DATABASE_ID"]!;
const { properties } = await notion.databases.retrieve({
database_id: database_id,
});
await exerciseWriting(database_id, properties, data!);
}