-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.ts
82 lines (68 loc) · 2.18 KB
/
subscriber.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
import { Handler, S3Event } from "aws-lambda";
import PocketBase from "pocketbase";
import { S3, GetObjectCommand } from "@aws-sdk/client-s3";
import * as cheerio from "cheerio";
const pb = new PocketBase(process.env.POCKETBASE_URL);
const adminData = await pb
.collection("API_ACCESS")
.authWithPassword(
process.env.POCKETBASE_EMAIL,
process.env.POCKETBASE_PASSWORD,
);
const client = new S3({});
export const handler: Handler<S3Event> = async (event: S3Event) => {
try {
console.dir(event);
const s3_record = event.Records[0];
const bucket = s3_record.s3.bucket.name;
const filename = decodeURIComponent(
s3_record.s3.object.key.replace(/\+/g, " "),
);
const htmlFile = await getFileFromS3(bucket, filename);
const fullUrl = await parseHTML(htmlFile);
console.log(`New file uploaded: ${filename}`);
const parts = filename.split(".");
const name = parts.slice(0, -1).join(".");
const extension = parts[-1];
console.log(`Name: ${name}, Extension: ${extension}`);
const record = await pb
.collection(process.env.POCKETBASE_COLLECTION)
.create({
Title: name,
URL: fullUrl,
Key: filename,
});
console.log(record.created);
return "ok";
} catch (error) {
console.error("Error processing S3 event:", error);
throw error;
}
};
async function getFileFromS3(bucket: string, key: string): Promise<string> {
try {
const command = new GetObjectCommand({
Bucket: bucket,
Key: key,
});
console.log(`Getting file from S3: ${bucket}/${key}`);
const response = await client.send(command);
return await response.Body.transformToString();
} catch (error) {
console.error("Error getting file from S3:", error);
throw new Error(`Failed to get file from S3: ${error.message}`);
}
}
function parseHTML(htmlFile: string): string {
try {
const $ = cheerio.load(htmlFile);
const url = $(".infobar-link-icon").attr("href");
if (!url) {
throw new Error("URL not found in HTML");
}
return url;
} catch (error) {
console.error("Error parsing HTML:", error);
throw new Error(`Failed to parse HTML: ${error.message}`);
}
}