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

Fixed incorrect badge count if there's no gold badge #3

Open
wants to merge 1 commit into
base: main
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
50 changes: 36 additions & 14 deletions services/extract-stats.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import * as cheerio from "cheerio";

function extractStats(html) {
const $ = cheerio.load(html);

const detailsDiv = $("div.d-flex.flex__fl-equal.fw-wrap.gs24").first();
const badgeDivs = detailsDiv.find("div.fs-title");

return {
username: $(".flex--item.mb12.fs-headline2.lh-xs").text().trim(),
reputation: $("div.fs-body3.fc-black-600").eq(0).text(),
peopleReached: $("div.fs-body3.fc-black-600").eq(1).text(),
answers: $("div.fs-body3.fc-black-600").eq(2).text(),
goldBadges: badgeDivs.eq(0).text().trim(),
silverBadges: badgeDivs.eq(1).text().trim(),
bronzeBadges: badgeDivs.eq(2).text().trim(),
};
const $ = cheerio.load(html);

const detailsDiv = $("div.d-flex.flex__fl-equal.fw-wrap.gs24").first();
const badgeDivs = detailsDiv.find("div.fs-title"); 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this 1? compilation isn't failing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a typo, I had it fixed in the immediate next commit


// Extract badge types and counts using class names (if available)
const goldBadges = detailsDiv.find("div.badge-gold").text().trim();
const silverBadges = detailsDiv.find("div.badge-silver").text().trim();
const bronzeBadges = detailsDiv.find("div.badge-bronze").text().trim();

// If class names are not found or not reliable, use text content as a fallback
if (!goldBadges || !silverBadges || !bronzeBadges) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If any one is not available why are we processing for all ? any reason.
Let's say goldbadges is not there silverBadges and bronzeBadges exists.
Then we are recalculating for silverbadges and bronzebadges even. can we try to optimize this , or any particular reason this is written this way?

const badges = badgeDivs.map(i => ({
type: $(badgeDivs[i]).text().trim().toLowerCase().split(" ")[0], // Extract badge type (gold, silver, bronze)
count: $(badgeDivs[i]).text().trim().split(" ")[1] // Extract badge count
}));

const userBadges = badges.reduce((acc, badge) => {
acc[badge.type] = badge.count;
return acc;
}, {}); // Create an object with badge types as keys and counts as values

goldBadges = userBadges.gold;
silverBadges = userBadges.silver;
bronzeBadges = userBadges.bronze;
}

return {
username: $(".flex--item.mb12.fs-headline2.lh-xs").text().trim(),
reputation: $("div.fs-body3.fc-black-600").eq(0).text(),
peopleReached: $("div.fs-body3.fc-black-600").eq(1).text(),
answers: $("div.fs-body3.fc-black-600").eq(2).text(),
goldBadges,
silverBadges,
bronzeBadges,
};
}

export default extractStats;