Skip to content

Commit

Permalink
chore: Refactor fetch.js and add function for fetching Medium blogs data
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrElsayyad committed Aug 2, 2024
1 parent 02a59b2 commit 5bc73ab
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fs = require("fs");
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 +16,52 @@ const ERR = {
requestFailedMedium:
"The request to Medium didn't succeed. Check if Medium username in your .env file is correct."
};

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

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

console.log(`statusCode: ${res.statusCode}`);
if (res.statusCode !== 200) {
res.on("data", d => {
console.error(`Error response: ${d}`);
});
if (retries > 0) {
console.log(`Retrying... (${retries} attempts left)`);
return fetchMediumData(retries - 1);
} else {
throw new Error(ERR.requestFailedMedium);
}
}

res.on("data", d => {
mediumData += d;
});
res.on("end", () => {
fs.writeFile("./public/blogs.json", mediumData, function (err) {
if (err) return console.log(err);
console.log("saved file to public/blogs.json");
});
});
});

req.on("error", error => {
throw error;
});

req.end();
}
}

if (USE_GITHUB_DATA === "true") {
if (GITHUB_USERNAME === undefined) {
throw new Error(ERR.noUserName);
Expand Down Expand Up @@ -94,37 +140,4 @@ if (USE_GITHUB_DATA === "true") {
req.end();
}

if (MEDIUM_USERNAME !== undefined) {
console.log(`Fetching Medium blogs data for ${MEDIUM_USERNAME}`);
const options = {
hostname: "api.rss2json.com",
path: `/v1/api.json?rss_url=https://medium.com/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", () => {
fs.writeFile("./public/blogs.json", mediumData, function (err) {
if (err) return console.log(err);
console.log("saved file to public/blogs.json");
});
});
});

req.on("error", error => {
throw error;
});

req.end();
}
fetchMediumData();

0 comments on commit 5bc73ab

Please sign in to comment.