Skip to content

Commit

Permalink
Add a feature checking if there's any changed handle
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzoolee committed Dec 31, 2022
1 parent adafc75 commit 86ec876
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion utils/cofo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,44 @@ const axios = require("axios");
const logger = require("./logger");

cofo.getTier = async (id) => {
const url = "https://codeforces.com/api/user.info?handles=" + id;

try {
const url = "https://codeforces.com/api/user.info?handles=" + id;
const { data } = await axios.get(url);
return data;
} catch (error) {
console.error(error);

const { ok, changedHandle } = await checkForChangedHandle(id);
if (ok) {
return await cofo.getTier(changedHandle);
}

logger.exception(error.response.data.comment);

return { status: "FAILED", result: [] };
}
};

const checkForChangedHandle = async (handle) => {
try {
await axios.head(`https://codeforces.com/profile/${handle}`, {
maxRedirects: 0,
});
} catch (e) {
const response = e.response;

if (response.status === 302) {
const movedURL = response.headers.location;
return {
ok: true,
changedHandle: movedURL.replace("https://codeforces.com/profile/", ""),
};
}
}

return {
ok: false,
changedHandle: null,
};
};

0 comments on commit 86ec876

Please sign in to comment.