From 1464131707ea7ff497818f0f2f97511a0fee031e Mon Sep 17 00:00:00 2001 From: Zed Date: Thu, 15 Aug 2019 23:17:13 +0200 Subject: [PATCH] Clean up --- src/nitter.nim | 61 ++++++++++++++++++------------------------- src/views/general.nim | 6 ++--- tests/test_card.py | 38 +++++++++++++-------------- 3 files changed, 48 insertions(+), 57 deletions(-) diff --git a/src/nitter.nim b/src/nitter.nim index 36a7448..a985371 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -37,8 +37,7 @@ proc showSingleTimeline(name, after, agent: string; query: Option[Query]; return "" let profileHtml = renderProfile(profile, timeline, await railFut, prefs) - return renderMain(profileHtml, prefs, title=cfg.title, titleText=pageTitle(profile), - desc=pageDesc(profile)) + return renderMain(profileHtml, cfg.title, pageTitle(profile), pageDesc(profile)) proc showMultiTimeline(names: seq[string]; after, agent: string; query: Option[Query]; prefs: Prefs): Future[string] {.async.} = @@ -51,7 +50,7 @@ proc showMultiTimeline(names: seq[string]; after, agent: string; query: Option[Q var timeline = renderMulti(await getTimelineSearch(get(q), after, agent), names.join(","), prefs) - return renderMain(timeline, prefs, title=cfg.title, titleText="Multi") + return renderMain(timeline, cfg.title, "Multi") proc showTimeline(name, after: string; query: Option[Query]; prefs: Prefs): Future[string] {.async.} = @@ -65,10 +64,10 @@ proc showTimeline(name, after: string; query: Option[Query]; template respTimeline(timeline: typed) = if timeline.len == 0: - resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title, prefs) + resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title) resp timeline -proc getCookiePrefs(request: Request): Prefs = +template cookiePrefs(): untyped {.dirty.} = getPrefs(request.cookies.getOrDefault("preferences")) setProfileCacheTime(cfg.profileCacheTime) @@ -80,59 +79,55 @@ settings: routes: get "/": - let prefs = getCookiePrefs(request) - resp renderMain(renderSearch(), prefs, title=cfg.title) + resp renderMain(renderSearch(), cfg.title) post "/search": if @"query".len == 0: - resp Http404, showError("Please enter a username.", cfg.title, - getCookiePrefs(request)) + resp Http404, showError("Please enter a username.", cfg.title) redirect("/" & @"query") post "/saveprefs": - var prefs = getCookiePrefs(request) + var prefs = cookiePrefs() genUpdatePrefs() setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=true) - redirect("/settings") + redirect("/") post "/resetprefs": - var prefs = getCookiePrefs(request) + var prefs = cookiePrefs() resetPrefs(prefs) setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=true) redirect("/settings") get "/settings": - let prefs = getCookiePrefs(request) - resp renderMain(renderPreferences(prefs), prefs, title=cfg.title, titleText="Preferences") + resp renderMain(renderPreferences(cookiePrefs()), cfg.title, "Preferences") get "/@name/?": cond '.' notin @"name" - let prefs = getCookiePrefs(request) - respTimeline(await showTimeline(@"name", @"after", none(Query), prefs)) + respTimeline(await showTimeline(@"name", @"after", none(Query), cookiePrefs())) get "/@name/search": cond '.' notin @"name" - let prefs = getCookiePrefs(request) + let prefs = cookiePrefs() let query = initQuery(@"filter", @"include", @"not", @"sep", @"name") - respTimeline(await showTimeline(@"name", @"after", some(query), prefs)) + respTimeline(await showTimeline(@"name", @"after", some(query), cookiePrefs())) get "/@name/replies": cond '.' notin @"name" - let prefs = getCookiePrefs(request) - respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name")), prefs)) + let prefs = cookiePrefs() + respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name")), cookiePrefs())) get "/@name/media": cond '.' notin @"name" - let prefs = getCookiePrefs(request) - respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name")), prefs)) + let prefs = cookiePrefs() + respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name")), cookiePrefs())) get "/@name/status/@id": cond '.' notin @"name" - let prefs = getCookiePrefs(request) + let prefs = cookiePrefs() let conversation = await getTweet(@"name", @"id", getAgent()) if conversation == nil or conversation.tweet.id.len == 0: - resp Http404, showError("Tweet not found", cfg.title, prefs) + resp Http404, showError("Tweet not found", cfg.title) let title = pageTitle(conversation.tweet.profile) let desc = conversation.tweet.text @@ -141,29 +136,26 @@ routes: if conversation.tweet.video.isSome(): let thumb = get(conversation.tweet.video).thumb let vidUrl = getVideoEmbed(conversation.tweet.id) - resp renderMain(html, prefs, title=cfg.title, titleText=title, desc=desc, - images = @[thumb], `type`="video", video=vidUrl) + resp renderMain(html, cfg.title, title, desc, images = @[thumb], + `type`="video", video=vidUrl) elif conversation.tweet.gif.isSome(): let thumb = get(conversation.tweet.gif).thumb let vidUrl = getVideoEmbed(conversation.tweet.id) - resp renderMain(html, prefs, title=cfg.title, titleText=title, desc=desc, - images = @[thumb], `type`="video", video=vidUrl) + resp renderMain(html, cfg.title, title, desc, images = @[thumb], + `type`="video", video=vidUrl) else: - resp renderMain(html, prefs, title=cfg.title, titleText=title, - desc=desc, images=conversation.tweet.photos) + resp renderMain(html, cfg.title, title, desc, images=conversation.tweet.photos) get "/pic/@sig/@url": cond "http" in @"url" cond "twimg" in @"url" - let prefs = getCookiePrefs(request) - let uri = parseUri(decodeUrl(@"url")) path = uri.path.split("/")[2 .. ^1].join("/") filename = cfg.cacheDir / cleanFilename(path & uri.query) if getHmac($uri) != @"sig": - resp showError("Failed to verify signature", cfg.title, prefs) + resp showError("Failed to verify signature", cfg.title) if not existsDir(cfg.cacheDir): createDir(cfg.cacheDir) @@ -185,11 +177,10 @@ routes: get "/video/@sig/@url": cond "http" in @"url" cond "video.twimg" in @"url" - let prefs = getCookiePrefs(request) let url = decodeUrl(@"url") if getHmac(url) != @"sig": - resp showError("Failed to verify signature", cfg.title, prefs) + resp showError("Failed to verify signature", cfg.title) let client = newAsyncHttpClient() let video = await client.getContent(url) diff --git a/src/views/general.nim b/src/views/general.nim index 2ca43d6..8639053 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -17,7 +17,7 @@ proc renderNavbar*(title: string): VNode = icon "info-circled", title="About", href="/about" icon "cog-2", title="Preferences", href="/settings" -proc renderMain*(body: VNode; prefs: Prefs; title="Nitter"; titleText=""; desc=""; +proc renderMain*(body: VNode; title="Nitter"; titleText=""; desc=""; `type`="article"; video=""; images: seq[string] = @[]): string = let node = buildHtml(html(lang="en")): head: @@ -62,5 +62,5 @@ proc renderError*(error: string): VNode = tdiv(class="error-panel"): span: text error -proc showError*(error: string; title: string; prefs: Prefs): string = - renderMain(renderError(error), prefs, title=title, titleText="Error") +proc showError*(error, title: string): string = + renderMain(renderError(error), title, "Error") diff --git a/tests/test_card.py b/tests/test_card.py index ac7b545..fba1041 100644 --- a/tests/test_card.py +++ b/tests/test_card.py @@ -6,62 +6,62 @@ card = [ ['voidtarget/status/1133028231672582145', 'sinkingsugar/nimqt-example', 'A sample of a Qt app written using mostly nim. Contribute to sinkingsugar/nimqt-example development by creating an account on GitHub.', - 'github.com', '-tb6lD-A', False], + 'github.com', False], ['Bountysource/status/1141879700639215617', '$1,000 Bounty on kivy/plyer', 'Automation and Screen Reader Support', - 'bountysource.com', '1161324818224078848', False], + 'bountysource.com', False], ['lorenlugosch/status/1115440394148487168', 'lorenlugosch/pretrain_speech_model', 'Speech Model Pre-training for End-to-End Spoken Language Understanding - lorenlugosch/pretrain_speech_model', - 'github.com', '1161172194040246272', False], + 'github.com', False], ['PyTorch/status/1123379369672450051', 'PyTorch', 'An open source deep learning platform that provides a seamless path from research prototyping to production deployment.', - 'pytorch.org', 'lAc4aESh', False], + 'pytorch.org', False], ['Thom_Wolf/status/1122466524860702729', 'pytorch/fairseq', 'Facebook AI Research Sequence-to-Sequence Toolkit written in Python. - pytorch/fairseq', - 'github.com', '1SVn24P6', False], + 'github.com', False], ['TheTwoffice/status/558685306090946561', 'Eternity: a moment standing still forever…', '- James Montgomery. | facebook | 500px | ferpectshotz | I dusted off this one from my old archives, it was taken while I was living in mighty new York city working at Wall St. I think this was the 11...', - 'flickr.com', '161236662619389953', True], + 'flickr.com', True], ['nim_lang/status/1136652293510717440', 'Version 0.20.0 released', 'We are very proud to announce Nim version 0.20. This is a massive release, both literally and figuratively. It contains more than 1,000 commits and it marks our release candidate for version 1.0!', - 'nim-lang.org', 'Q0aJrdMZ', True], + 'nim-lang.org', True], ['Tesla/status/1141041022035623936', 'Experience the Tesla Arcade', '', - 'www.tesla.com', '40H36baw', True], + 'www.tesla.com', True], ['mobile_test/status/490378953744318464', 'Nantasket Beach', 'Rocks on the beach.', - '500px.com', 'FVUU4YDwN', True], + '500px.com', True], ['voidtarget/status/1094632512926605312', 'Basic OBS Studio plugin, written in nim, supporting C++ (C fine too)', 'Basic OBS Studio plugin, written in nim, supporting C++ (C fine too) - obsplugin.nim', - 'gist.github.com', '1160647657574076423', True], + 'gist.github.com', True], ['AdsAPI/status/1110272721005367296', 'Conversation Targeting', '', - 'view.highspot.com', 'FrVMLWJH', True], + 'view.highspot.com', True], ['FluentAI/status/1116417904831029248', 'Amazon’s Alexa isn’t just AI — thousands of humans are listening', 'One of the only ways to improve Alexa is to have human beings check it for errors', - 'theverge.com', 'HOW73fOB', True] + 'theverge.com', True] ] no_thumb = [ @@ -80,17 +80,17 @@ playable = [ ['nim_lang/status/1118234460904919042', 'Nim development blog 2019-03', 'Arne (aka Krux02) * debugging: * improved nim-gdb, $ works, framefilter * alias for --debugger:native: -g * bugs: * forwarding of .pure. * sizeof union * fea...', - 'youtube.com', '1161613174514290688'], + 'youtube.com'], ['nim_lang/status/1121090879823986688', 'Nim - First natively compiled language w/ hot code-reloading at...', '#nim #c++ #ACCUConf Nim is a statically typed systems and applications programming language which offers perhaps some of the most powerful metaprogramming ca...', - 'youtube.com', '1161379576087568386'], + 'youtube.com'], ['lele/status/819930645145288704', 'Eurocrash presents Open Decks - emerging dj #4: E-Musik', "OPEN DECKS is Eurocrash's new project about discovering new and emerging dj talents. Every selected dj will have the chance to perform the first dj-set in front of an actual audience. The best dj...", - 'mixcloud.com', '161048988763795457'] + 'mixcloud.com'] ] promo = [ @@ -106,12 +106,12 @@ promo = [ class CardTest(BaseTestCase): @parameterized.expand(card) - def test_card(self, tweet, title, description, destination, image, large): + def test_card(self, tweet, title, description, destination, large): self.open_nitter(tweet) card = Card(Conversation.main + " ") self.assert_text(title, card.title) self.assert_text(destination, card.destination) - self.assertIn(image, self.get_image_url(card.image + ' img')) + self.assertIn('_img', self.get_image_url(card.image + ' img')) if len(description) > 0: self.assert_text(description, card.description) if large: @@ -129,12 +129,12 @@ class CardTest(BaseTestCase): self.assert_text(description, card.description) @parameterized.expand(playable) - def test_card_playable(self, tweet, title, description, destination, image): + def test_card_playable(self, tweet, title, description, destination): self.open_nitter(tweet) card = Card(Conversation.main + " ") self.assert_text(title, card.title) self.assert_text(destination, card.destination) - self.assertIn(image, self.get_image_url(card.image + ' img')) + self.assertIn('_img', self.get_image_url(card.image + ' img')) self.assert_element_visible('.card-overlay') if len(description) > 0: self.assert_text(description, card.description)