Skip to content

Commit 33649fe

Browse files
fix: improve follower growth calculation and error handling in user data loading
1 parent 214c503 commit 33649fe

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

fetch.py

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

3030
def load_previous_users(path: str = "./docs/users.json") -> Dict[str, Dict[str, Any]]:
31-
"""
32-
trending feature -> load previous users.json and index by login
33-
34-
to calcute the follower growth
31+
"""Load previous user data from a JSON file and index it by login.
32+
This is used to calculate follower growth for the trending feature.
3533
"""
3634
if not os.path.exists(path):
3735
return {}
3836
try:
3937
with open(path, "r", encoding="utf-8") as f:
4038
users = json.load(f)
41-
return {u.get("login"): u for u in users if "login" in u}
42-
except Exception:
39+
if not isinstance(users, list):
40+
logger.warning(f"Data in {path} is not a list, returning empty dict.")
41+
return {}
42+
return {u["login"]: u for u in users if isinstance(u, dict) and "login" in u}
43+
except (IOError, json.JSONDecodeError) as e:
44+
logger.warning(f"Failed to load or parse previous users from {path}: {e}")
4345
return {}
4446

4547

0 commit comments

Comments
 (0)