Skip to content

Commit bdb7009

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 bdb7009

File tree

3 files changed

+1833
-897
lines changed

3 files changed

+1833
-897
lines changed

fetch.js

Lines changed: 71 additions & 47 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,49 @@ const ERR = {
1617
requestFailedMedium:
1718
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
1819
};
20+
21+
async function fetchWithRetry(options, data, retries = 5, delay = 5000) {
22+
for (let i = 0; i < retries; i++) {
23+
try {
24+
return await new Promise((resolve, reject) => {
25+
const req = https.request(options, res => {
26+
let responseData = "";
27+
28+
res.on("data", d => {
29+
responseData += d;
30+
});
31+
32+
res.on("end", () => {
33+
if (res.statusCode === 200) {
34+
resolve(responseData);
35+
} else if (res.statusCode === 429 && i < retries - 1) {
36+
console.log(`Rate limited. Retrying in ${delay}ms...`);
37+
setTimeout(() => resolve(fetchWithRetry(options, data, retries - 1, delay)), delay);
38+
} else {
39+
reject(new Error(`${options.path} request failed with status code: ${res.statusCode}`));
40+
}
41+
});
42+
});
43+
44+
req.on("error", error => {
45+
reject(error);
46+
});
47+
48+
if (data) {
49+
req.write(data);
50+
}
51+
req.end();
52+
});
53+
} catch (error) {
54+
if (i === retries - 1) {
55+
throw error;
56+
}
57+
console.log(`Error fetching data. Retrying in ${delay}ms...`);
58+
await new Promise(resolve => setTimeout(resolve, delay));
59+
}
60+
}
61+
}
62+
1963
if (USE_GITHUB_DATA === "true") {
2064
if (GITHUB_USERNAME === undefined) {
2165
throw new Error(ERR.noUserName);
@@ -67,64 +111,44 @@ if (USE_GITHUB_DATA === "true") {
67111
}
68112
};
69113

70-
const req = https.request(default_options, res => {
71-
let data = "";
72-
73-
console.log(`statusCode: ${res.statusCode}`);
74-
if (res.statusCode !== 200) {
75-
throw new Error(ERR.requestFailed);
76-
}
77-
78-
res.on("data", d => {
79-
data += d;
80-
});
81-
res.on("end", () => {
82-
fs.writeFile("./public/profile.json", data, function (err) {
114+
fetchWithRetry(default_options, data)
115+
.then(responseData => {
116+
fs.writeFile("./public/profile.json", responseData, function (err) {
83117
if (err) return console.log(err);
84118
console.log("saved file to public/profile.json");
85119
});
120+
})
121+
.catch(error => {
122+
console.error("Error:", error);
86123
});
87-
});
88-
89-
req.on("error", error => {
90-
throw error;
91-
});
92-
93-
req.write(data);
94-
req.end();
95124
}
96125

97126
if (MEDIUM_USERNAME !== undefined) {
98127
console.log(`Fetching Medium blogs data for ${MEDIUM_USERNAME}`);
128+
const url = `https://medium.com/feed/@${MEDIUM_USERNAME}`;
99129
const options = {
100-
hostname: "api.rss2json.com",
101-
path: `/v1/api.json?rss_url=https://medium.com/feed/@${MEDIUM_USERNAME}`,
130+
hostname: "medium.com",
131+
path: `/feed/@${MEDIUM_USERNAME}`,
102132
port: 443,
103133
method: "GET"
104134
};
105135

106-
const req = https.request(options, res => {
107-
let mediumData = "";
108-
109-
console.log(`statusCode: ${res.statusCode}`);
110-
if (res.statusCode !== 200) {
111-
throw new Error(ERR.requestMediumFailed);
112-
}
136+
fetchWithRetry(options)
137+
.then(responseData => {
138+
feed2json.fromString(responseData, url, {}, (err, json) => {
139+
if (err) {
140+
console.error("Error converting feed to JSON:", err);
141+
return;
142+
}
113143

114-
res.on("data", d => {
115-
mediumData += d;
116-
});
117-
res.on("end", () => {
118-
fs.writeFile("./public/blogs.json", mediumData, function (err) {
119-
if (err) return console.log(err);
120-
console.log("saved file to public/blogs.json");
144+
const mediumData = JSON.stringify(json, null, 2);
145+
fs.writeFile("./public/blogs.json", mediumData, function (err) {
146+
if (err) return console.error(err);
147+
console.log("Saved file to public/blogs.json");
148+
});
121149
});
150+
})
151+
.catch(error => {
152+
console.error("Error:", error);
122153
});
123-
});
124-
125-
req.on("error", error => {
126-
throw error;
127-
});
128-
129-
req.end();
130-
}
154+
}

0 commit comments

Comments
 (0)