-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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