From 8000a814df336c69285b170d40c8ce8dbb5dad4e Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 25 Jun 2019 13:07:49 +0200 Subject: [PATCH] Fix crash on invalid id and non-existent profiles --- src/api.nim | 17 +++++++++++------ src/nitter.nim | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/api.nim b/src/api.nim index 31af03d..52aec12 100644 --- a/src/api.nim +++ b/src/api.nim @@ -161,21 +161,23 @@ proc getTimeline*(username: string; after=""): Future[Timeline] {.async.} = }) var url = timelineUrl % username - if after.len > 0: - url &= "&max_position=" & after + let cleanAfter = after.replace(re"[^\d]*(\d+)[^\d]*", "$1") + if cleanAfter.len > 0: + url &= "&max_position=" & cleanAfter let json = await fetchJson(base / url, headers) let html = parseHtml(json["items_html"].to(string)) result = Timeline( - tweets: parseTweets(html), - minId: json["min_position"].to(string), hasMore: json["has_more_items"].to(bool), + maxId: json.getOrDefault("max_position").getStr(""), + minId: json.getOrDefault("min_position").getStr(""), ) - if json.hasKey("max_position"): - result.maxId = json["max_position"].to(string) + if json["new_latent_count"].to(int) == 0: + return + result.tweets = parseTweets(html) await getVideos(result.tweets) proc getTweet*(id: string): Future[Conversation] {.async.} = @@ -194,5 +196,8 @@ proc getTweet*(id: string): Future[Conversation] {.async.} = url = base / tweetUrl / id html = await fetchHtml(url, headers) + if html.isNil: + return + result = parseConversation(html) await getConversationVideos(result) diff --git a/src/nitter.nim b/src/nitter.nim index 2bed905..e306cc7 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -44,7 +44,7 @@ routes: cond '.' notin @"name" let conversation = await getTweet(@"id") - if conversation.tweet.id.len == 0: + if conversation.isNil or conversation.tweet.id.len == 0: resp Http404, showError("Tweet not found") let title = pageTitle(conversation.tweet.profile)