Skip to content

Commit 5d9216f

Browse files
committed
fix: fetch Medium blogs directly from Medium feed and replace the usage of rss2json API with feed2json package to fix errors
Signed-off-by: Amr Elsayyad <[email protected]>
1 parent d3fdcee commit 5d9216f

File tree

7 files changed

+3467
-5719
lines changed

7 files changed

+3467
-5719
lines changed

bun.lockb

612 KB
Binary file not shown.

fetch.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
fs = require("fs");
1+
const feed2json = require("feed2json");
2+
const fs = require("fs");
23
const https = require("https");
3-
process = require("process");
4+
const process = require("process");
45
require("dotenv").config();
56

67
const GITHUB_TOKEN = process.env.REACT_APP_GITHUB_TOKEN;
@@ -16,6 +17,7 @@ const ERR = {
1617
requestFailedMedium:
1718
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
1819
};
20+
1921
if (USE_GITHUB_DATA === "true") {
2022
if (GITHUB_USERNAME === undefined) {
2123
throw new Error(ERR.noUserName);
@@ -96,28 +98,30 @@ if (USE_GITHUB_DATA === "true") {
9698

9799
if (MEDIUM_USERNAME !== undefined) {
98100
console.log(`Fetching Medium blogs data for ${MEDIUM_USERNAME}`);
101+
const url = `https://medium.com/feed/@${MEDIUM_USERNAME}`;
99102
const options = {
100-
hostname: "api.rss2json.com",
101-
path: `/v1/api.json?rss_url=https://medium.com/feed/@${MEDIUM_USERNAME}`,
103+
hostname: "medium.com",
104+
path: `/feed/@${MEDIUM_USERNAME}`,
102105
port: 443,
103106
method: "GET"
104107
};
105108

106109
const req = https.request(options, res => {
107-
let mediumData = "";
108-
109110
console.log(`statusCode: ${res.statusCode}`);
110111
if (res.statusCode !== 200) {
111112
throw new Error(ERR.requestMediumFailed);
112113
}
113114

114-
res.on("data", d => {
115-
mediumData += d;
116-
});
117-
res.on("end", () => {
115+
feed2json.fromStream(res, url, {}, (err, json) => {
116+
if (err) {
117+
console.error("Error converting feed to JSON:", err);
118+
return;
119+
}
120+
121+
const mediumData = JSON.stringify(json, null, 2);
118122
fs.writeFile("./public/blogs.json", mediumData, function (err) {
119-
if (err) return console.log(err);
120-
console.log("saved file to public/blogs.json");
123+
if (err) return console.error(err);
124+
console.log("Saved file to public/blogs.json");
121125
});
122126
});
123127
});

0 commit comments

Comments
 (0)