From e1b3e9db7612875b9967875be3e3f2aa43a4d237 Mon Sep 17 00:00:00 2001 From: Zed Date: Fri, 8 Jan 2021 02:25:43 +0100 Subject: [PATCH] Add proper http support Fixes #223 --- src/config.nim | 1 - src/formatters.nim | 8 +++- src/nitter.nim | 3 +- src/routes/rss.nim | 14 +++---- src/routes/search.nim | 7 +--- src/types.nim | 1 - src/views/general.nim | 8 +--- src/views/rss.nimf | 97 +++++++++++++++++++++++-------------------- 8 files changed, 70 insertions(+), 69 deletions(-) diff --git a/src/config.nim b/src/config.nim index 89e81e3..f24e9aa 100644 --- a/src/config.nim +++ b/src/config.nim @@ -24,7 +24,6 @@ proc getConfig*(path: string): (Config, parseCfg.Config) = base64Media: cfg.get("Config", "base64Media", false), minTokens: cfg.get("Config", "tokenCount", 10), - cacheDir: cfg.get("Cache", "directory", "/tmp/nitter"), listCacheTime: cfg.get("Cache", "listMinutes", 120), rssCacheTime: cfg.get("Cache", "rssMinutes", 10), diff --git a/src/formatters.nim b/src/formatters.nim index 5707393..5136701 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -18,6 +18,10 @@ const twitter = parseUri("https://twitter.com") +proc getUrlPrefix*(cfg: Config): string = + if cfg.useHttps: "https://" & cfg.hostname + else: "http://" & cfg.hostname + proc stripHtml*(text: string): string = var html = parseHtml(text) for el in html.findAll("a"): @@ -48,7 +52,7 @@ proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string = result = result.replace(cards, prefs.replaceTwitter & "/cards") result = result.replace(twRegex, prefs.replaceTwitter) if absolute.len > 0: - result = result.replace("href=\"/", "href=\"https://" & absolute & "/") + result = result.replace("href=\"/", "href=\"" & absolute & "/") proc getM3u8Url*(content: string): string = var m: RegexMatch @@ -69,7 +73,7 @@ proc getUserpic*(profile: Profile; style=""): string = getUserPic(profile.userpic, style) proc getVideoEmbed*(cfg: Config; id: int64): string = - &"https://{cfg.hostname}/i/videos/{id}" + &"{getUrlPrefix(cfg)}/i/videos/{id}" proc pageTitle*(profile: Profile): string = &"{profile.fullname} (@{profile.username})" diff --git a/src/nitter.nim b/src/nitter.nim index 586cd4e..3eee87b 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -21,8 +21,7 @@ when defined(release): addHandler(newConsoleLogger()) setLogFilter(lvlError) -let http = if cfg.useHttps: "https" else: "http" -stdout.write &"Starting Nitter at {http}://{cfg.hostname}\n" +stdout.write &"Starting Nitter at {getUrlPrefix(cfg)}\n" stdout.flushFile updateDefaultPrefs(fullCfg) diff --git a/src/routes/rss.nim b/src/routes/rss.nim index 2ecef89..69264a6 100644 --- a/src/routes/rss.nim +++ b/src/routes/rss.nim @@ -9,7 +9,7 @@ include "../views/rss.nimf" export times, hashes, supersnappy -proc showRss*(req: Request; hostname: string; query: Query): Future[Rss] {.async.} = +proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} = var profile: Profile var timeline: Timeline let @@ -35,8 +35,7 @@ proc showRss*(req: Request; hostname: string; query: Query): Future[Rss] {.async return Rss(feed: profile.username, cursor: "suspended") if profile.fullname.len > 0: - let rss = compress renderTimelineRss(timeline, profile, hostname, - multi=(names.len > 1)) + let rss = compress renderTimelineRss(timeline, profile, cfg, multi=(names.len > 1)) return Rss(feed: rss, cursor: timeline.bottom) template respRss*(rss) = @@ -44,6 +43,7 @@ template respRss*(rss) = resp Http404, showError("User \"" & @"name" & "\" not found", cfg) elif rss.cursor.len == 9 and rss.cursor == "suspended": resp Http404, showError(getSuspended(rss.feed), cfg) + let headers = {"Content-Type": "application/rss+xml; charset=utf-8", "Min-Id": rss.cursor} resp Http200, headers, uncompress rss.feed @@ -69,7 +69,7 @@ proc createRssRouter*(cfg: Config) = let tweets = await getSearch[Tweet](query, cursor) rss.cursor = tweets.bottom rss.feed = compress renderSearchRss(tweets.content, query.text, - genQueryUrl(query), cfg.hostname) + genQueryUrl(query), cfg) await cacheRss(key, rss) respRss(rss) @@ -85,7 +85,7 @@ proc createRssRouter*(cfg: Config) = if rss.cursor.len > 0: respRss(rss) - rss = await showRss(request, cfg.hostname, Query(fromUser: @[name])) + rss = await timelineRss(request, cfg, Query(fromUser: @[name])) await cacheRss(key, rss) respRss(rss) @@ -110,7 +110,7 @@ proc createRssRouter*(cfg: Config) = if rss.cursor.len > 0: respRss(rss) - rss = await showRss(request, cfg.hostname, query) + rss = await timelineRss(request, cfg, query) await cacheRss(key, rss) respRss(rss) @@ -129,7 +129,7 @@ proc createRssRouter*(cfg: Config) = list = await getCachedList(@"name", @"list") timeline = await getListTimeline(list.id, cursor) rss.cursor = timeline.bottom - rss.feed = compress renderListRss(timeline.content, list, cfg.hostname) + rss.feed = compress renderListRss(timeline.content, list, cfg) await cacheRss(key, rss) respRss(rss) diff --git a/src/routes/search.nim b/src/routes/search.nim index 9eba1ea..329d955 100644 --- a/src/routes/search.nim +++ b/src/routes/search.nim @@ -39,10 +39,7 @@ proc createSearchRouter*(cfg: Config) = redirect("/search?q=" & encodeUrl("#" & @"hash")) get "/opensearch": - var url = "" - if cfg.useHttps: - url = "https://" & cfg.hostname & "/search?q=" - else: - url = "http://" & cfg.hostname & "/search?q=" + var url = if cfg.useHttps: "https://" else: "http://" + url &= cfg.hostname & "/search?q=" resp Http200, {"Content-Type": "application/opensearchdescription+xml"}, generateOpenSearchXML(cfg.title, cfg.hostname, url) diff --git a/src/types.nim b/src/types.nim index 756b277..4c48020 100644 --- a/src/types.nim +++ b/src/types.nim @@ -211,7 +211,6 @@ type base64Media*: bool minTokens*: int - cacheDir*: string rssCacheTime*: int listCacheTime*: int diff --git a/src/views/general.nim b/src/views/general.nim index 26ccf80..341e3c0 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -40,11 +40,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video=""; elif images.len > 0: "photo" else: "article" - var opensearchUrl = "" - if cfg.useHttps: - opensearchUrl = "https://" & cfg.hostname & "/opensearch" - else: - opensearchUrl = "http://" & cfg.hostname & "/opensearch" + let opensearchUrl = getUrlPrefix(cfg) & "/opensearch" buildHtml(head): link(rel="stylesheet", type="text/css", href="/css/style.css?v=3") @@ -93,7 +89,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video=""; let preloadUrl = getPicUrl(url & suffix) link(rel="preload", type="image/png", href=preloadUrl, `as`="image") - let image = "https://" & cfg.hostname & getPicUrl(url) + let image = getUrlPrefix(cfg) & getPicUrl(url) meta(property="og:image", content=image) meta(property="twitter:image:src", content=image) diff --git a/src/views/rss.nimf b/src/views/rss.nimf index f690a46..720a3ed 100644 --- a/src/views/rss.nimf +++ b/src/views/rss.nimf @@ -19,51 +19,58 @@ #end if #end proc # -#proc renderRssTweet(tweet: Tweet; prefs: Prefs; hostname: string): string = +#proc getDescription(desc: string; cfg: Config): string = +Twitter feed for: ${desc}. Generated by ${cfg.hostname} +#end proc +# +#proc renderRssTweet(tweet: Tweet; prefs: Prefs; cfg: Config): string = #let tweet = tweet.retweet.get(tweet) -#let text = replaceUrl(tweet.text, prefs, absolute=hostname) +#let urlPrefix = getUrlPrefix(cfg) +#let text = replaceUrl(tweet.text, prefs, absolute=urlPrefix) #if tweet.quote.isSome and get(tweet.quote).available: -#let quoteLink = hostname & getLink(get(tweet.quote)) -

${text}
${quoteLink}

+# let quoteLink = getLink(get(tweet.quote)) +

${text}
${cfg.hostname}${quoteLink}

#else:

${text}

#end if #if tweet.photos.len > 0: -#for photo in tweet.photos: - -#end for +# for photo in tweet.photos: + +# end for #elif tweet.video.isSome: - + #elif tweet.gif.isSome: -#let thumb = &"https://{hostname}{getPicUrl(get(tweet.gif).thumb)}" -#let url = &"https://{hostname}{getPicUrl(get(tweet.gif).url)}" +# let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}" +# let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}" #end if #end proc # -#proc renderRssTweets(tweets: seq[Tweet]; prefs: Prefs; hostname: string): string = +#proc renderRssTweets(tweets: seq[Tweet]; prefs: Prefs; cfg: Config): string = +#let urlPrefix = getUrlPrefix(cfg) #var links: seq[string] #for t in tweets: -#let retweet = if t.retweet.isSome: t.profile.username else: "" -#let tweet = if retweet.len > 0: t.retweet.get else: t -#let link = getLink(tweet) -#if link in links: continue -#end if -#links.add link - - ${getTitle(tweet, prefs, retweet)} - @${tweet.profile.username} - - ${getRfc822Time(tweet)} - https://${hostname & link} - https://${hostname & link} - +# let retweet = if t.retweet.isSome: t.profile.username else: "" +# let tweet = if retweet.len > 0: t.retweet.get else: t +# let link = getLink(tweet) +# if link in links: continue +# end if +# links.add link + + ${getTitle(tweet, prefs, retweet)} + @${tweet.profile.username} + + ${getRfc822Time(tweet)} + ${urlPrefix & link} + ${urlPrefix & link} + #end for #end proc # -#proc renderTimelineRss*(timeline: Timeline; profile: Profile; hostname: string; multi=false): string = -#let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidious.snopyta.org") +#proc renderTimelineRss*(timeline: Timeline; profile: Profile; cfg: Config; multi=false): string = +#let prefs = Prefs(replaceTwitter: cfg.hostname, replaceYouTube: "invidious.snopyta.org") +#let urlPrefix = getUrlPrefix(cfg) #result = "" #let user = (if multi: "" else: "@") & profile.username #var title = profile.fullname @@ -73,29 +80,29 @@ - + ${title} - https://${hostname}/${profile.username} - Twitter feed for: ${user}. Generated by ${hostname} + ${urlPrefix}/${profile.username} + ${getDescription(user, cfg)} en-us 40 ${title} - https://${hostname}/${profile.username} - https://${hostname}${getPicUrl(profile.getUserPic(style="_400x400"))} + ${urlPrefix}/${profile.username} + ${urlPrefix}${getPicUrl(profile.getUserPic(style="_400x400"))} 128 128 - #if timeline.content.len > 0: - ${renderRssTweets(timeline.content, prefs, hostname)} - #end if +#if timeline.content.len > 0: +${renderRssTweets(timeline.content, prefs, cfg)} +#end if #end proc # -#proc renderListRss*(tweets: seq[Tweet]; list: List; hostname: string): string = -#let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidious.snopyta.org") -#let link = &"https://{hostname}/{list.username}/lists/{list.name}" +#proc renderListRss*(tweets: seq[Tweet]; list: List; cfg: Config): string = +#let prefs = Prefs(replaceTwitter: cfg.hostname, replaceYouTube: "invidious.snopyta.org") +#let link = &"{getUrlPrefix(cfg)}/{list.username}/lists/{list.name}" #result = "" @@ -103,17 +110,17 @@ ${xmltree.escape(list.name)} / @${list.username} ${link} - Twitter feed for: ${list.name} by @${list.username}. Generated by ${hostname} + ${getDescription(list.name & " by @" & list.username, cfg)} en-us 40 - ${renderRssTweets(tweets, prefs, hostname)} +${renderRssTweets(tweets, prefs, cfg)} #end proc # -#proc renderSearchRss*(tweets: seq[Tweet]; name, param, hostname: string): string = -#let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidious.snopyta.org") -#let link = &"https://{hostname}/search" +#proc renderSearchRss*(tweets: seq[Tweet]; name, param: string; cfg: Config): string = +#let prefs = Prefs(replaceTwitter: cfg.hostname, replaceYouTube: "invidious.snopyta.org") +#let link = &"{getUrlPrefix(cfg)}/search" #let escName = xmltree.escape(name) #result = "" @@ -122,10 +129,10 @@ Search results for "${escName}" ${link} - Twitter feed for search "${escName}". Generated by ${hostname} + ${getDescription("Search \"" & escName & "\"", cfg)} en-us 40 - ${renderRssTweets(tweets, prefs, hostname)} +${renderRssTweets(tweets, prefs, cfg)} #end proc