Skip to content
Open
Show file tree
Hide file tree
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
105 changes: 64 additions & 41 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,27 @@ 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);
}

console.log(`Fetching profile data for ${GITHUB_USERNAME}`);
var data = JSON.stringify({
query: `
const isPlaceholder = value => {
if (!value) return true;
const trimmed = String(value).trim();
const placeholders = [
"YOUR GITHUB USERNAME HERE",
"YOUR GITHUB TOKEN HERE",
"YOU MEDIUM USERNAME HERE"
];
return trimmed.length === 0 || placeholders.includes(trimmed);
};

if (USE_GITHUB_DATA === "true") {
if (isPlaceholder(GITHUB_USERNAME) || isPlaceholder(GITHUB_TOKEN)) {
console.warn(
"Skipping GitHub fetch: set GITHUB_USERNAME and REACT_APP_GITHUB_TOKEN in .env"
);
} else {
console.log(`Fetching profile data for ${GITHUB_USERNAME}`);
var data = JSON.stringify({
query: `
{
user(login:"${GITHUB_USERNAME}") {
name
Expand Down Expand Up @@ -55,50 +68,54 @@ if (USE_GITHUB_DATA === "true") {
}
}
`
});
const default_options = {
hostname: "api.github.com",
path: "/graphql",
port: 443,
method: "POST",
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
"User-Agent": "Node"
}
};
});
const default_options = {
hostname: "api.github.com",
path: "/graphql",
port: 443,
method: "POST",
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
"User-Agent": "Node"
}
};

const req = https.request(default_options, res => {
let data = "";
const req = https.request(default_options, res => {
let data = "";

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

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

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

req.write(data);
req.end();
req.write(data);
req.end();
}
}

if (MEDIUM_USERNAME !== undefined) {
if (!isPlaceholder(MEDIUM_USERNAME)) {
const encodedMediumUsername = encodeURIComponent(MEDIUM_USERNAME.trim());
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}`,
path: `/v1/api.json?rss_url=https://medium.com/feed/@${encodedMediumUsername}`,
port: 443,
method: "GET"
};
Expand All @@ -108,7 +125,9 @@ if (MEDIUM_USERNAME !== undefined) {

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

res.on("data", d => {
Expand All @@ -123,8 +142,12 @@ if (MEDIUM_USERNAME !== undefined) {
});

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

req.end();
} else {
console.warn(
"Skipping Medium fetch: set MEDIUM_USERNAME in .env (or leave empty to disable)"
);
}
Loading