Skip to content

Commit 57c7d87

Browse files
feat: enhance follower growth calculation with snapshot timestamp and add growth indicator styling
1 parent ba39f3e commit 57c7d87

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

docs/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ function buildCardElement(user) {
697697

698698
card.appendChild(link);
699699
card.appendChild(box);
700+
user.card = card;
700701
return card;
701702
}
702703

docs/styles.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,17 @@ h1 {
211211
box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
212212
}
213213

214+
.growth-indicator {
215+
margin-top: 8px;
216+
padding: 6px;
217+
font-size: 0.8rem;
218+
font-weight: 600;
219+
color: #27ae60; /* Green for positive growth */
220+
background-color: #e9f7ef;
221+
border-radius: 6px;
222+
border: 1px solid #a7d7c5;
223+
}
224+
214225
.filters-buttons {
215226
display: flex;
216227
gap: 10px;

fetch.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,37 +232,42 @@ def fetch_user_detail_with_retry(login: str, max_retries: int = 5) -> Dict[str,
232232

233233
def compute_follower_growth(login: str, current_followers: Any, previous_users: Dict[str, Dict[str, Any]]) -> Dict[str, Any]:
234234
"""
235-
Trending feature: compute the previous follower count and growth percentage
236-
returns { followers_previous: int | None, followers_growth_pct: float | None }
235+
Trending feature: compute follower growth data, including snapshot timestamp.
237236
"""
238-
user = previous_users.get(login, {})
239-
prev_followers = user.get("followers")
240-
prev_snapshot_at = user.get("followers_snapshot_at")
237+
prev_user_data = previous_users.get(login, {})
238+
prev_followers = prev_user_data.get("followers")
239+
prev_snapshot_at = prev_user_data.get("followers_snapshot_at")
241240

241+
# If no valid previous data, initialize snapshot time and return no growth.
242242
if not isinstance(prev_followers, int) or not isinstance(prev_snapshot_at, int):
243243
return {
244244
"followers_previous": None,
245245
"followers_growth_pct": None,
246+
"followers_snapshot_at": int(time.time()),
246247
}
247248

248-
# weekly followers threshold
249+
# If it's not yet time to calculate new growth, return old data to preserve it.
249250
if time.time() - prev_snapshot_at < WEEK_SECONDS:
250251
return {
251-
"followers_previous": None,
252-
"followers_growth_pct": None,
252+
"followers_previous": prev_followers,
253+
"followers_growth_pct": prev_user_data.get("followers_growth_pct"),
254+
"followers_snapshot_at": prev_snapshot_at,
253255
}
254256

257+
# Time to calculate new growth.
255258
if not isinstance(current_followers, int) or prev_followers <= 0:
256259
return {
257260
"followers_previous": prev_followers,
258261
"followers_growth_pct": None,
262+
"followers_snapshot_at": int(time.time()),
259263
}
260264

261265
growth_pct = ((current_followers - prev_followers) / prev_followers) * 100
262266

263267
return {
264268
"followers_previous": prev_followers,
265269
"followers_growth_pct": round(growth_pct, 2),
270+
"followers_snapshot_at": int(time.time()),
266271
}
267272

268273

@@ -276,7 +281,6 @@ def enrich_user_with_details(user: Dict[str, Any], idx: int, total: int, previou
276281
sponsorship = fetch_sponsorship_info(user["login"])
277282

278283
user["followers"] = detail.get("followers", "N/A")
279-
user["followers_snapshot_at"] = int(time.time())
280284
user["following"] = detail.get("following", "N/A")
281285
user["location"] = detail.get("location", "")
282286
user["name"] = detail.get("name")

0 commit comments

Comments
 (0)