Skip to content

Commit c994b2c

Browse files
fix: improve follower growth calculation and error handling in user data loading
1 parent 3774f81 commit c994b2c

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

docs/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ function getSortedUsers(sortBy) {
561561
'followers-desc': (a, b) => b.followers - a.followers,
562562
'followers-asc': (a, b) => a.followers - b.followers,
563563
'followers-growth-desc': (a, b) => (b.followers_growth_pct ?? -Infinity) - (a.followers_growth_pct ?? -Infinity),
564-
'followers-growth-asc': (a, b) => (a.followers_growth_pct ?? -Infinity) - (b.followers_growth_pct ?? -Infinity),
564+
'followers-growth-asc': (a, b) => (a.followers_growth_pct ?? Infinity) - (b.followers_growth_pct ?? Infinity),
565565
'following-desc': (a, b) => b.following - a.following,
566566
'following-asc': (a, b) => a.following - b.following,
567567
'repos-desc': (a, b) => b.repos - a.repos,
@@ -688,9 +688,9 @@ function buildCardElement(user) {
688688

689689
// Display followers growth
690690
if (user.followers_growth_pct > 0) {
691-
const growth = document.createElement('span');
691+
const growth = document.createElement('div');
692692
growth.className = 'growth-indicator';
693-
growth.textContent = ` Followers Growth (since last week): (+${user.followers_growth_pct}%)`;
693+
growth.textContent = `Followers Growth (since last week): (+${user.followers_growth_pct}%)`;
694694
box.appendChild(growth);
695695

696696
}

fetch.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@
2525
WEEK_SECONDS = 7 * 24 * 60 * 60 # for trending feature
2626

2727
def load_previous_users(path: str = "./docs/users.json") -> Dict[str, Dict[str, Any]]:
28-
"""
29-
trending feature -> load previous users.json and index by login
30-
31-
to calcute the follower growth
28+
"""Load previous user data from a JSON file and index it by login.
29+
This is used to calculate follower growth for the trending feature.
3230
"""
3331
if not os.path.exists(path):
3432
return {}
3533
try:
3634
with open(path, "r", encoding="utf-8") as f:
3735
users = json.load(f)
38-
return {u.get("login"): u for u in users if "login" in u}
39-
except Exception:
36+
if not isinstance(users, list):
37+
logger.warning(f"Data in {path} is not a list, returning empty dict.")
38+
return {}
39+
return {u["login"]: u for u in users if isinstance(u, dict) and "login" in u}
40+
except (IOError, json.JSONDecodeError) as e:
41+
logger.warning(f"Failed to load or parse previous users from {path}: {e}")
4042
return {}
4143

4244

0 commit comments

Comments
 (0)