-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_blog.ts
More file actions
67 lines (55 loc) · 1.56 KB
/
Copy pathfetch_blog.ts
File metadata and controls
67 lines (55 loc) · 1.56 KB
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
import path from "path";
import axios from "axios";
import * as cheerio from "cheerio";
const BLOG_URL = "https://blog.uint.dev";
const SELECTOR = ".listing .card";
const POST_LIMIT = 5;
const BLOG_TITLE: string = "Recent Posts";
const BLOG_DESCRIPTION: string = `View all <a href="${BLOG_URL}">here</a>.`;
interface Post {
link: string;
title: string;
description: string;
metadata: string;
}
interface BlogEntryMetadata {
title: string;
description: string;
base: string;
}
interface BlogEntry {
metadata: BlogEntryMetadata;
posts: Post[];
}
const blogEntryObject: BlogEntry = {
metadata: {
title: BLOG_TITLE,
description: BLOG_DESCRIPTION,
base: BLOG_URL,
},
posts: [],
};
console.log("Fetching blog post data...");
const { data } = await axios.get(BLOG_URL);
const $: cheerio.CheerioAPI = cheerio.load(data);
const cards = $(SELECTOR);
if (!cards.length) {
console.log(`No elements found with selector '${SELECTOR}'`);
process.exit(1);
}
cards.each((i, el) => {
if (i >= POST_LIMIT) return;
const card = $(el);
blogEntryObject.posts.push({
link: card.attr("href") ?? "#",
title: card.find(".title").eq(0).text(),
description: card.find(".description").eq(0).text(),
metadata: card.find(".metadata").eq(0).html() ?? "",
});
});
console.log(blogEntryObject);
const filePath: string = path.join("./src/data", "blog.json");
console.log(`Writing to ${filePath}...`);
await Bun.write(filePath, JSON.stringify(blogEntryObject, null, 2));
console.log(`Successfully wrote to ${filePath}`);
console.log("Creating build...");