Skip to content

Commit

Permalink
fix: fetch Medium blogs directly from Medium feed and replace the usa…
Browse files Browse the repository at this point in the history
…ge of `rss2json` API with `feed2json` package to fix errors

Signed-off-by: Amr Elsayyad <[email protected]>
  • Loading branch information
AmrElsayyad committed Aug 23, 2024
1 parent d3fdcee commit 5d9216f
Show file tree
Hide file tree
Showing 7 changed files with 3,467 additions and 5,719 deletions.
Binary file added bun.lockb
Binary file not shown.
28 changes: 16 additions & 12 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fs = require("fs");
const feed2json = require("feed2json");
const fs = require("fs");
const https = require("https");
process = require("process");
const process = require("process");
require("dotenv").config();

const GITHUB_TOKEN = process.env.REACT_APP_GITHUB_TOKEN;
Expand All @@ -16,6 +17,7 @@ const ERR = {
requestFailedMedium:
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
};

if (USE_GITHUB_DATA === "true") {
if (GITHUB_USERNAME === undefined) {
throw new Error(ERR.noUserName);
Expand Down Expand Up @@ -96,28 +98,30 @@ if (USE_GITHUB_DATA === "true") {

if (MEDIUM_USERNAME !== undefined) {
console.log(`Fetching Medium blogs data for ${MEDIUM_USERNAME}`);
const url = `https://medium.com/feed/@${MEDIUM_USERNAME}`;
const options = {
hostname: "api.rss2json.com",
path: `/v1/api.json?rss_url=https://medium.com/feed/@${MEDIUM_USERNAME}`,
hostname: "medium.com",
path: `/feed/@${MEDIUM_USERNAME}`,
port: 443,
method: "GET"
};

const req = https.request(options, res => {
let mediumData = "";

console.log(`statusCode: ${res.statusCode}`);
if (res.statusCode !== 200) {
throw new Error(ERR.requestMediumFailed);
}

res.on("data", d => {
mediumData += d;
});
res.on("end", () => {
feed2json.fromStream(res, url, {}, (err, json) => {
if (err) {
console.error("Error converting feed to JSON:", err);
return;
}

const mediumData = JSON.stringify(json, null, 2);
fs.writeFile("./public/blogs.json", mediumData, function (err) {
if (err) return console.log(err);
console.log("saved file to public/blogs.json");
if (err) return console.error(err);
console.log("Saved file to public/blogs.json");
});
});
});
Expand Down
Loading

0 comments on commit 5d9216f

Please sign in to comment.