From 5bc73ab13696dc72830ab7a0122989cf34ca5a89 Mon Sep 17 00:00:00 2001 From: Amr Elsayyad Date: Fri, 2 Aug 2024 16:33:41 +0300 Subject: [PATCH] chore: Refactor fetch.js and add function for fetching Medium blogs data --- fetch.js | 85 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/fetch.js b/fetch.js index 415ab0f5e7..aabb484a1f 100644 --- a/fetch.js +++ b/fetch.js @@ -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; @@ -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); @@ -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();