-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
345 lines (292 loc) · 9.1 KB
/
index.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { authenticate } from "@google-cloud/local-auth";
import { google } from "googleapis";
import path from "path";
import fs from "fs/promises";
import { GoogleGenerativeAI } from "@google/generative-ai";
import dotenv from "dotenv";
import { Client } from "@notionhq/client";
const LAST_PROCESSED_DATE_PATH = path.join(
__dirname,
"last-processed-date.json"
);
dotenv.config();
const __dirname = path.resolve();
const SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"];
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const notion = new Client({ auth: process.env.NOTION_API_KEY });
const NOTION_DATABASE_ID = process.env.NOTION_DATABASE_ID;
async function searchEmails(gmail) {
try {
const query = lastProcessedDate
? `from:anurag.jptnp@gmail.com after:${lastProcessedDate}`
: "from:anurag.jptnp@gmail.com";
const response = await gmail.users.messages.list({
userId: "me",
q: query,
maxResults: 10,
});
const messages = response.data.messages || [];
const filteredMessages = [];
for (const message of messages) {
const fullMessage = await gmail.users.messages.get({
userId: "me",
id: message.id,
format: "full",
});
let emailBody = "";
if (fullMessage.data.payload.parts) {
for (const part of fullMessage.data.payload.parts) {
if (part.body.data) {
const buff = Buffer.from(part.body.data, "base64");
emailBody += buff.toString();
}
}
} else if (fullMessage.data.payload.body.data) {
const buff = Buffer.from(fullMessage.data.payload.body.data, "base64");
emailBody += buff.toString();
}
if (emailBody.toLowerCase().includes("congratulations")) {
filteredMessages.push(message);
}
}
return filteredMessages;
} catch (error) {
console.error("Error searching emails:", error);
return [];
}
try {
// First, get all messages
const response = await gmail.users.messages.list({
userId: "me",
q: "from:anurag.jptnp@gmail.com",
maxResults: 30,
});
const messages = response.data.messages || [];
const filteredMessages = [];
// Step 2: Check each message's body for "congratulations"
for (const message of messages) {
const fullMessage = await gmail.users.messages.get({
userId: "me",
id: message.id,
format: "full",
});
// Get the email body
let emailBody = "";
if (fullMessage.data.payload.parts) {
// If the message has parts (usually for HTML and plain text)
for (const part of fullMessage.data.payload.parts) {
if (part.body.data) {
const buff = Buffer.from(part.body.data, "base64");
emailBody += buff.toString();
}
}
} else if (fullMessage.data.payload.body.data) {
// If the message is plain text only
const buff = Buffer.from(fullMessage.data.payload.body.data, "base64");
emailBody += buff.toString();
}
// Check if "congratulations" exists in the body (case insensitive)
if (emailBody.toLowerCase().includes("congratulations")) {
filteredMessages.push(message);
}
}
return filteredMessages;
} catch (error) {
console.error("Error searching emails:", error);
return [];
}
}
async function getEmailContent(gmail, messageId) {
try {
const message = await gmail.users.messages.get({
userId: "me",
id: messageId,
format: "full",
});
const headers = message.data.payload.headers;
const subject = headers.find((header) => header.name === "Subject").value;
let body = "";
if (message.data.payload.parts) {
body = Buffer.from(
message.data.payload.parts[0].body.data,
"base64"
).toString();
}
return {
id: message.data.id,
subject,
body,
};
} catch (error) {
console.error("Error fetching email content:", error);
return null;
}
}
async function parseEmailWithGemini(email) {
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const prompt = `
Extract the following information from this email about a job offer:
- Company_Name
- Job_Role
- Internship_Stipend
- PPO_Package (if mentioned)
- Employment_Type (Full-time/Part-time/Internship)
- Base_Salary
- Job_Location (Remote/On-site)
- Equity_Stock_Options (if applicable)
- Bonuses
- CGPA_Criteria
- Date
Return the information in JSON format.
Email subject:
${email.subject}
Email content:
${email.body}
`;
try {
const result = await model.generateContent(prompt);
const response = result.response;
// console.log("Response: ", response.text());
// remove ```json from the start and ``` from the end of the response.text()
const responseText = response
.text()
.replace("```json", "")
.replace("```", "");
return JSON.parse(responseText);
} catch (error) {
console.error("Error parsing with Gemini:", error);
return null;
}
}
async function addToNotion(parsedData) {
try {
const response = await notion.pages.create({
parent: { database_id: NOTION_DATABASE_ID },
properties: {
Company_Name: {
title: [{ text: { content: parsedData.Company_Name } }],
},
Job_Role: {
rich_text: [{ text: { content: parsedData.jobRole } }],
},
Internship_Stipend: {
rich_text: [
{ text: { content: parsedData.internshipStipend.toString() } },
],
},
PPO_Package: {
rich_text: [
{ text: { content: parsedData.ppoPackage || "Not mentioned" } },
],
},
Employment_Type: {
rich_text: [
{ text: { content: parsedData.employmentType || "Not mentioned" } },
],
},
Base_Salary: {
rich_text: [
{ text: { content: parsedData.salary || "Not mentioned" } },
],
},
Job_Location: {
rich_text: [
{ text: { content: parsedData.jobLocation || "Not mentioned" } },
],
},
Stock_Options: {
rich_text: [
{
text: {
content: parsedData.equityStockOptions || "Not mentioned",
},
},
],
},
Bonuses: {
rich_text: [
{ text: { content: parsedData.bonuses || "Not mentioned" } },
],
},
CGPA_Criteria: {
rich_text: [
{ text: { content: parsedData.cgpa || "Not mentioned" } },
],
},
},
Date: {
title: [{ text: { content: parsedData.Date } }],
},
});
console.log("Page created:", response);
} catch (error) {
console.error("Error adding to Notion:", error);
}
}
async function main() {
try {
const auth = await getAuth();
const gmail = google.gmail({ version: "v1", auth });
const messages = await searchEmails(gmail);
for (const message of messages) {
// Get email content
const emailContent = await getEmailContent(gmail, message.id);
if (!emailContent) continue;
console.log("Email Processing: ", emailContent.subject);
// Parse with Gemini
const parsedData = await parseEmailWithGemini(emailContent);
if (!parsedData) continue;
console.log(parsedData);
console.log(parsedData.Company_Name);
// Add to Notion
await addToNotion(parsedData);
// console.log(`Processed email: ${emailContent.subject}`);
// console.log("Processing email:", message.id);
return;
}
} catch (error) {
console.error("Error in main:", error);
}
try {
const auth = await getAuth();
const gmail = google.gmail({ version: "v1", auth });
const lastProcessedDate = await loadLastProcessedDate();
const messages = await searchEmails(gmail, lastProcessedDate);
let newLastProcessedDate = lastProcessedDate;
for (const message of messages) {
const emailContent = await getEmailContent(gmail, message.id);
if (!emailContent) continue;
console.log("Email Processing: ", emailContent.subject);
const parsedData = await parseEmailWithGemini(emailContent);
if (!parsedData) continue;
console.log(parsedData);
console.log(parsedData.Company_Name);
await addToNotion(parsedData);
newLastProcessedDate = emailContent.date;
}
if (newLastProcessedDate !== lastProcessedDate) {
await saveLastProcessedDate(newLastProcessedDate);
}
} catch (error) {
console.error("Error in main:", error);
}
}
async function loadLastProcessedDate() {
try {
const content = await fs.readFile(LAST_PROCESSED_DATE_PATH, "utf-8");
return JSON.parse(content).date;
} catch (error) {
return null;
}
}
async function saveLastProcessedDate(date) {
try {
await fs.writeFile(
LAST_PROCESSED_DATE_PATH,
JSON.stringify({ date }, null, 2)
);
} catch (error) {
console.error("Error saving last processed date:", error);
}
}
main();