Add proper http support

Fixes #223
This commit is contained in:
Zed 2021-01-08 02:25:43 +01:00
parent ecc8bc5a95
commit e1b3e9db76
8 changed files with 70 additions and 69 deletions

View File

@ -24,7 +24,6 @@ proc getConfig*(path: string): (Config, parseCfg.Config) =
base64Media: cfg.get("Config", "base64Media", false), base64Media: cfg.get("Config", "base64Media", false),
minTokens: cfg.get("Config", "tokenCount", 10), minTokens: cfg.get("Config", "tokenCount", 10),
cacheDir: cfg.get("Cache", "directory", "/tmp/nitter"),
listCacheTime: cfg.get("Cache", "listMinutes", 120), listCacheTime: cfg.get("Cache", "listMinutes", 120),
rssCacheTime: cfg.get("Cache", "rssMinutes", 10), rssCacheTime: cfg.get("Cache", "rssMinutes", 10),

View File

@ -18,6 +18,10 @@ const
twitter = parseUri("https://twitter.com") 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 = proc stripHtml*(text: string): string =
var html = parseHtml(text) var html = parseHtml(text)
for el in html.findAll("a"): 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(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter) result = result.replace(twRegex, prefs.replaceTwitter)
if absolute.len > 0: if absolute.len > 0:
result = result.replace("href=\"/", "href=\"https://" & absolute & "/") result = result.replace("href=\"/", "href=\"" & absolute & "/")
proc getM3u8Url*(content: string): string = proc getM3u8Url*(content: string): string =
var m: RegexMatch var m: RegexMatch
@ -69,7 +73,7 @@ proc getUserpic*(profile: Profile; style=""): string =
getUserPic(profile.userpic, style) getUserPic(profile.userpic, style)
proc getVideoEmbed*(cfg: Config; id: int64): string = proc getVideoEmbed*(cfg: Config; id: int64): string =
&"https://{cfg.hostname}/i/videos/{id}" &"{getUrlPrefix(cfg)}/i/videos/{id}"
proc pageTitle*(profile: Profile): string = proc pageTitle*(profile: Profile): string =
&"{profile.fullname} (@{profile.username})" &"{profile.fullname} (@{profile.username})"

View File

@ -21,8 +21,7 @@ when defined(release):
addHandler(newConsoleLogger()) addHandler(newConsoleLogger())
setLogFilter(lvlError) setLogFilter(lvlError)
let http = if cfg.useHttps: "https" else: "http" stdout.write &"Starting Nitter at {getUrlPrefix(cfg)}\n"
stdout.write &"Starting Nitter at {http}://{cfg.hostname}\n"
stdout.flushFile stdout.flushFile
updateDefaultPrefs(fullCfg) updateDefaultPrefs(fullCfg)

View File

@ -9,7 +9,7 @@ include "../views/rss.nimf"
export times, hashes, supersnappy 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 profile: Profile
var timeline: Timeline var timeline: Timeline
let let
@ -35,8 +35,7 @@ proc showRss*(req: Request; hostname: string; query: Query): Future[Rss] {.async
return Rss(feed: profile.username, cursor: "suspended") return Rss(feed: profile.username, cursor: "suspended")
if profile.fullname.len > 0: if profile.fullname.len > 0:
let rss = compress renderTimelineRss(timeline, profile, hostname, let rss = compress renderTimelineRss(timeline, profile, cfg, multi=(names.len > 1))
multi=(names.len > 1))
return Rss(feed: rss, cursor: timeline.bottom) return Rss(feed: rss, cursor: timeline.bottom)
template respRss*(rss) = template respRss*(rss) =
@ -44,6 +43,7 @@ template respRss*(rss) =
resp Http404, showError("User \"" & @"name" & "\" not found", cfg) resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
elif rss.cursor.len == 9 and rss.cursor == "suspended": elif rss.cursor.len == 9 and rss.cursor == "suspended":
resp Http404, showError(getSuspended(rss.feed), cfg) resp Http404, showError(getSuspended(rss.feed), cfg)
let headers = {"Content-Type": "application/rss+xml; charset=utf-8", let headers = {"Content-Type": "application/rss+xml; charset=utf-8",
"Min-Id": rss.cursor} "Min-Id": rss.cursor}
resp Http200, headers, uncompress rss.feed resp Http200, headers, uncompress rss.feed
@ -69,7 +69,7 @@ proc createRssRouter*(cfg: Config) =
let tweets = await getSearch[Tweet](query, cursor) let tweets = await getSearch[Tweet](query, cursor)
rss.cursor = tweets.bottom rss.cursor = tweets.bottom
rss.feed = compress renderSearchRss(tweets.content, query.text, rss.feed = compress renderSearchRss(tweets.content, query.text,
genQueryUrl(query), cfg.hostname) genQueryUrl(query), cfg)
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss) respRss(rss)
@ -85,7 +85,7 @@ proc createRssRouter*(cfg: Config) =
if rss.cursor.len > 0: if rss.cursor.len > 0:
respRss(rss) respRss(rss)
rss = await showRss(request, cfg.hostname, Query(fromUser: @[name])) rss = await timelineRss(request, cfg, Query(fromUser: @[name]))
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss) respRss(rss)
@ -110,7 +110,7 @@ proc createRssRouter*(cfg: Config) =
if rss.cursor.len > 0: if rss.cursor.len > 0:
respRss(rss) respRss(rss)
rss = await showRss(request, cfg.hostname, query) rss = await timelineRss(request, cfg, query)
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss) respRss(rss)
@ -129,7 +129,7 @@ proc createRssRouter*(cfg: Config) =
list = await getCachedList(@"name", @"list") list = await getCachedList(@"name", @"list")
timeline = await getListTimeline(list.id, cursor) timeline = await getListTimeline(list.id, cursor)
rss.cursor = timeline.bottom 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) await cacheRss(key, rss)
respRss(rss) respRss(rss)

View File

@ -39,10 +39,7 @@ proc createSearchRouter*(cfg: Config) =
redirect("/search?q=" & encodeUrl("#" & @"hash")) redirect("/search?q=" & encodeUrl("#" & @"hash"))
get "/opensearch": get "/opensearch":
var url = "" var url = if cfg.useHttps: "https://" else: "http://"
if cfg.useHttps: url &= cfg.hostname & "/search?q="
url = "https://" & cfg.hostname & "/search?q="
else:
url = "http://" & cfg.hostname & "/search?q="
resp Http200, {"Content-Type": "application/opensearchdescription+xml"}, resp Http200, {"Content-Type": "application/opensearchdescription+xml"},
generateOpenSearchXML(cfg.title, cfg.hostname, url) generateOpenSearchXML(cfg.title, cfg.hostname, url)

View File

@ -211,7 +211,6 @@ type
base64Media*: bool base64Media*: bool
minTokens*: int minTokens*: int
cacheDir*: string
rssCacheTime*: int rssCacheTime*: int
listCacheTime*: int listCacheTime*: int

View File

@ -40,11 +40,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
elif images.len > 0: "photo" elif images.len > 0: "photo"
else: "article" else: "article"
var opensearchUrl = "" let opensearchUrl = getUrlPrefix(cfg) & "/opensearch"
if cfg.useHttps:
opensearchUrl = "https://" & cfg.hostname & "/opensearch"
else:
opensearchUrl = "http://" & cfg.hostname & "/opensearch"
buildHtml(head): buildHtml(head):
link(rel="stylesheet", type="text/css", href="/css/style.css?v=3") 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) let preloadUrl = getPicUrl(url & suffix)
link(rel="preload", type="image/png", href=preloadUrl, `as`="image") 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="og:image", content=image)
meta(property="twitter:image:src", content=image) meta(property="twitter:image:src", content=image)

View File

@ -19,51 +19,58 @@
#end if #end if
#end proc #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 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: #if tweet.quote.isSome and get(tweet.quote).available:
#let quoteLink = hostname & getLink(get(tweet.quote)) # let quoteLink = getLink(get(tweet.quote))
<p>${text}<br><a href="https://${quoteLink}">${quoteLink}</a></p> <p>${text}<br><a href="${urlPrefix}${quoteLink}">${cfg.hostname}${quoteLink}</a></p>
#else: #else:
<p>${text}</p> <p>${text}</p>
#end if #end if
#if tweet.photos.len > 0: #if tweet.photos.len > 0:
#for photo in tweet.photos: # for photo in tweet.photos:
<img src="https://${hostname}${getPicUrl(photo)}" style="max-width:250px;" /> <img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" />
#end for # end for
#elif tweet.video.isSome: #elif tweet.video.isSome:
<img src="https://${hostname}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" /> <img src="${urlPrefix}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" />
#elif tweet.gif.isSome: #elif tweet.gif.isSome:
#let thumb = &"https://{hostname}{getPicUrl(get(tweet.gif).thumb)}" # let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}"
#let url = &"https://{hostname}{getPicUrl(get(tweet.gif).url)}" # let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}"
<video poster="${thumb}" autoplay muted loop style="max-width:250px;"> <video poster="${thumb}" autoplay muted loop style="max-width:250px;">
<source src="${url}" type="video/mp4"</source></video> <source src="${url}" type="video/mp4"</source></video>
#end if #end if
#end proc #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] #var links: seq[string]
#for t in tweets: #for t in tweets:
#let retweet = if t.retweet.isSome: t.profile.username else: "" # let retweet = if t.retweet.isSome: t.profile.username else: ""
#let tweet = if retweet.len > 0: t.retweet.get else: t # let tweet = if retweet.len > 0: t.retweet.get else: t
#let link = getLink(tweet) # let link = getLink(tweet)
#if link in links: continue # if link in links: continue
#end if # end if
#links.add link # links.add link
<item> <item>
<title>${getTitle(tweet, prefs, retweet)}</title> <title>${getTitle(tweet, prefs, retweet)}</title>
<dc:creator>@${tweet.profile.username}</dc:creator> <dc:creator>@${tweet.profile.username}</dc:creator>
<description><![CDATA[${renderRssTweet(tweet, prefs, hostname).strip(chars={'\n'})}]]></description> <description><![CDATA[${renderRssTweet(tweet, prefs, cfg).strip(chars={'\n'})}]]></description>
<pubDate>${getRfc822Time(tweet)}</pubDate> <pubDate>${getRfc822Time(tweet)}</pubDate>
<guid>https://${hostname & link}</guid> <guid>${urlPrefix & link}</guid>
<link>https://${hostname & link}</link> <link>${urlPrefix & link}</link>
</item> </item>
#end for #end for
#end proc #end proc
# #
#proc renderTimelineRss*(timeline: Timeline; profile: Profile; hostname: string; multi=false): string = #proc renderTimelineRss*(timeline: Timeline; profile: Profile; cfg: Config; multi=false): string =
#let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidious.snopyta.org") #let prefs = Prefs(replaceTwitter: cfg.hostname, replaceYouTube: "invidious.snopyta.org")
#let urlPrefix = getUrlPrefix(cfg)
#result = "" #result = ""
#let user = (if multi: "" else: "@") & profile.username #let user = (if multi: "" else: "@") & profile.username
#var title = profile.fullname #var title = profile.fullname
@ -73,29 +80,29 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel> <channel>
<atom:link href="https://${hostname}/${profile.username}/rss" rel="self" type="application/rss+xml" /> <atom:link href="${urlPrefix}/${profile.username}/rss" rel="self" type="application/rss+xml" />
<title>${title}</title> <title>${title}</title>
<link>https://${hostname}/${profile.username}</link> <link>${urlPrefix}/${profile.username}</link>
<description>Twitter feed for: ${user}. Generated by ${hostname}</description> <description>${getDescription(user, cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
<image> <image>
<title>${title}</title> <title>${title}</title>
<link>https://${hostname}/${profile.username}</link> <link>${urlPrefix}/${profile.username}</link>
<url>https://${hostname}${getPicUrl(profile.getUserPic(style="_400x400"))}</url> <url>${urlPrefix}${getPicUrl(profile.getUserPic(style="_400x400"))}</url>
<width>128</width> <width>128</width>
<height>128</height> <height>128</height>
</image> </image>
#if timeline.content.len > 0: #if timeline.content.len > 0:
${renderRssTweets(timeline.content, prefs, hostname)} ${renderRssTweets(timeline.content, prefs, cfg)}
#end if #end if
</channel> </channel>
</rss> </rss>
#end proc #end proc
# #
#proc renderListRss*(tweets: seq[Tweet]; list: List; hostname: string): string = #proc renderListRss*(tweets: seq[Tweet]; list: List; cfg: Config): string =
#let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidious.snopyta.org") #let prefs = Prefs(replaceTwitter: cfg.hostname, replaceYouTube: "invidious.snopyta.org")
#let link = &"https://{hostname}/{list.username}/lists/{list.name}" #let link = &"{getUrlPrefix(cfg)}/{list.username}/lists/{list.name}"
#result = "" #result = ""
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
@ -103,17 +110,17 @@
<atom:link href="${link}" rel="self" type="application/rss+xml" /> <atom:link href="${link}" rel="self" type="application/rss+xml" />
<title>${xmltree.escape(list.name)} / @${list.username}</title> <title>${xmltree.escape(list.name)} / @${list.username}</title>
<link>${link}</link> <link>${link}</link>
<description>Twitter feed for: ${list.name} by @${list.username}. Generated by ${hostname}</description> <description>${getDescription(list.name & " by @" & list.username, cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
${renderRssTweets(tweets, prefs, hostname)} ${renderRssTweets(tweets, prefs, cfg)}
</channel> </channel>
</rss> </rss>
#end proc #end proc
# #
#proc renderSearchRss*(tweets: seq[Tweet]; name, param, hostname: string): string = #proc renderSearchRss*(tweets: seq[Tweet]; name, param: string; cfg: Config): string =
#let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidious.snopyta.org") #let prefs = Prefs(replaceTwitter: cfg.hostname, replaceYouTube: "invidious.snopyta.org")
#let link = &"https://{hostname}/search" #let link = &"{getUrlPrefix(cfg)}/search"
#let escName = xmltree.escape(name) #let escName = xmltree.escape(name)
#result = "" #result = ""
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
@ -122,10 +129,10 @@
<atom:link href="${link}" rel="self" type="application/rss+xml" /> <atom:link href="${link}" rel="self" type="application/rss+xml" />
<title>Search results for "${escName}"</title> <title>Search results for "${escName}"</title>
<link>${link}</link> <link>${link}</link>
<description>Twitter feed for search "${escName}". Generated by ${hostname}</description> <description>${getDescription("Search \"" & escName & "\"", cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
${renderRssTweets(tweets, prefs, hostname)} ${renderRssTweets(tweets, prefs, cfg)}
</channel> </channel>
</rss> </rss>
#end proc #end proc