From fcfc1ef497ad48d3c63fb6527c6932616de0f29d Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 16 Jan 2022 03:32:18 +0100 Subject: [PATCH] Parse user stats as ints, not strings, cleanup --- src/parser.nim | 10 +++++----- src/redis_cache.nim | 1 + src/routes/rss.nim | 3 +-- src/routes/timeline.nim | 8 ++++---- src/types.nim | 11 +++++------ src/views/profile.nim | 4 ++-- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/parser.nim b/src/parser.nim index 6aa6a7a..011798f 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -14,11 +14,11 @@ proc parseProfile(js: JsonNode; id=""): Profile = bio: js{"description"}.getStr, userPic: js{"profile_image_url_https"}.getImageStr.replace("_normal", ""), banner: js.getBanner, - following: $js{"friends_count"}.getInt, - followers: $js{"followers_count"}.getInt, - tweets: $js{"statuses_count"}.getInt, - likes: $js{"favourites_count"}.getInt, - media: $js{"media_count"}.getInt, + following: js{"friends_count"}.getInt, + followers: js{"followers_count"}.getInt, + tweets: js{"statuses_count"}.getInt, + likes: js{"favourites_count"}.getInt, + media: js{"media_count"}.getInt, verified: js{"verified"}.getBool, protected: js{"protected"}.getBool, joinDate: js{"created_at"}.getTime diff --git a/src/redis_cache.nim b/src/redis_cache.nim index dbe917c..7a921c8 100644 --- a/src/redis_cache.nim +++ b/src/redis_cache.nim @@ -47,6 +47,7 @@ proc initRedisPool*(cfg: Config) {.async.} = await migrate("snappyRss", "rss:*") await migrate("userBuckets", "p:*") await migrate("profileDates", "p:*") + await migrate("profileStats", "p:*") pool.withAcquire(r): # optimize memory usage for profile ID buckets diff --git a/src/routes/rss.nim b/src/routes/rss.nim index 5deb1cf..771a3ad 100644 --- a/src/routes/rss.nim +++ b/src/routes/rss.nim @@ -19,8 +19,7 @@ proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async. names = getNames(name) if names.len == 1: - (profile, timeline) = - await fetchSingleTimeline(after, query, skipRail=true) + (profile, timeline) = await fetchTimeline(after, query, skipRail=true) else: var q = query q.fromUser = names diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index 2992888..12c2baa 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -19,8 +19,8 @@ proc getQuery*(request: Request; tab, name: string): Query = of "search": initQuery(params(request), name=name) else: Query(fromUser: @[name]) -proc fetchSingleTimeline*(after: string; query: Query; skipRail=false): - Future[(Profile, Timeline, PhotoRail)] {.async.} = +proc fetchTimeline*(after: string; query: Query; skipRail=false): + Future[(Profile, Timeline, PhotoRail)] {.async.} = let name = query.fromUser[0] var @@ -86,7 +86,7 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs; html = renderTweetSearch(timeline, prefs, getPath()) return renderMain(html, request, cfg, prefs, "Multi", rss=rss) - var (p, t, r) = await fetchSingleTimeline(after, query) + var (p, t, r) = await fetchTimeline(after, query) if p.suspended: return showError(getSuspended(p.username), cfg) if p.id.len == 0: return @@ -139,7 +139,7 @@ proc createTimelineRouter*(cfg: Config) = timeline.beginning = true resp $renderTweetSearch(timeline, prefs, getPath()) else: - var (_, timeline, _) = await fetchSingleTimeline(after, query, skipRail=true) + var (_, timeline, _) = await fetchTimeline(after, query, skipRail=true) if timeline.content.len == 0: resp Http404 timeline.beginning = true resp $renderTimelineTweets(timeline, prefs, getPath()) diff --git a/src/types.nim b/src/types.nim index 8bb1956..ce3446b 100644 --- a/src/types.nim +++ b/src/types.nim @@ -48,17 +48,16 @@ type id*: string username*: string fullname*: string - lowername*: string location*: string website*: string bio*: string userPic*: string banner*: string - following*: string - followers*: string - tweets*: string - likes*: string - media*: string + following*: int + followers*: int + tweets*: int + likes*: int + media*: int verified*: bool protected*: bool suspended*: bool diff --git a/src/views/profile.nim b/src/views/profile.nim index 9481faf..a0ef40f 100644 --- a/src/views/profile.nim +++ b/src/views/profile.nim @@ -5,12 +5,12 @@ import karax/[karaxdsl, vdom, vstyles] import renderutils, search import ".."/[types, utils, formatters] -proc renderStat(num, class: string; text=""): VNode = +proc renderStat(num: int; class: string; text=""): VNode = let t = if text.len > 0: text else: class buildHtml(li(class=class)): span(class="profile-stat-header"): text capitalizeAscii(t) span(class="profile-stat-num"): - text if num.len == 0: "?" else: insertSep(num, ',') + text insertSep($num, ',') proc renderProfileCard*(profile: Profile; prefs: Prefs): VNode = buildHtml(tdiv(class="profile-card")):