Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fetch.js to limit medium post #685

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ if (USE_GITHUB_DATA === "true") {
}

if (MEDIUM_USERNAME !== undefined) {
// Define the maximum number of items to fetch
const MAX_ITEMS_TO_FETCH = 4; // Change this number to limit the number of blogs fetched

console.log(`Fetching Medium blogs data for ${MEDIUM_USERNAME}`);
const options = {
hostname: "api.rss2json.com",
Expand All @@ -115,9 +118,24 @@ if (MEDIUM_USERNAME !== undefined) {
mediumData += d;
});
res.on("end", () => {
fs.writeFile("./public/blogs.json", mediumData, function (err) {
// Parse the JSON data
const jsonData = JSON.parse(mediumData);

// Limit the items fetched
const limitedData = {
...jsonData,
items: jsonData.items.slice(0, MAX_ITEMS_TO_FETCH)
};

// Convert back to JSON
const limitedDataJSON = JSON.stringify(limitedData);

// Write to file
fs.writeFile("./public/blogs.json", limitedDataJSON, function (err) {
if (err) return console.log(err);
console.log("saved file to public/blogs.json");
console.log(
`Saved ${limitedData.items.length} items to public/blogs.json`
);
});
});
});
Expand Down
Loading