-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchPosts.ts
More file actions
51 lines (40 loc) · 1.56 KB
/
Copy pathfetchPosts.ts
File metadata and controls
51 lines (40 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
import fs from "fs";
import Parser from "rss-parser";
import { extractGameImage, extractGameInfo, getDifference } from "./util";
import { FormattedPost, Post } from "./types";
const LOCAL_FEED_FILE = "localFeed.json";
const parser = new Parser();
const getDifferenceAndFormat = (localFeed: Post[], latestFeed: Post[]) => {
const newPosts = [
...getDifference(localFeed, latestFeed),
...getDifference(latestFeed, localFeed),
];
const formattedPosts: FormattedPost[] = newPosts.flatMap((item) => {
if (item.title === "Upcoming Repacks" || !item.contentSnippet.includes("Download Mirrors"))
return [];
let formattedPost: FormattedPost = {
title: item.title,
link: item.link,
companies: [],
genres: [],
imageUrl: "",
};
const { companies, genres } = extractGameInfo(item.contentSnippet);
const imageUrl = extractGameImage(item["content:encoded"]);
formattedPost = { ...formattedPost, companies, genres, imageUrl };
return formattedPost;
});
return formattedPosts;
};
export const fetchPosts = async () => {
const feed = await parser.parseURL("https://fitgirl-repacks.site/feed/");
if (!feed) throw new Error("Failed to fetch RSS feed");
const latestFeed = feed.items as Post[];
let localFeed = [] as Post[];
if (fs.existsSync(LOCAL_FEED_FILE)) {
localFeed = JSON.parse(fs.readFileSync(LOCAL_FEED_FILE).toString()).items;
}
// Save latest feed to local
fs.writeFileSync("./localFeed.json", JSON.stringify(feed, null, 2), "utf-8");
return getDifferenceAndFormat(localFeed, latestFeed);
};