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),
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),

View File

@ -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})"

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

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

View File

@ -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)

View File

@ -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))
<p>${text}<br><a href="https://${quoteLink}">${quoteLink}</a></p>
# let quoteLink = getLink(get(tweet.quote))
<p>${text}<br><a href="${urlPrefix}${quoteLink}">${cfg.hostname}${quoteLink}</a></p>
#else:
<p>${text}</p>
#end if
#if tweet.photos.len > 0:
#for photo in tweet.photos:
<img src="https://${hostname}${getPicUrl(photo)}" style="max-width:250px;" />
#end for
# for photo in tweet.photos:
<img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" />
# end for
#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:
#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)}"
<video poster="${thumb}" autoplay muted loop style="max-width:250px;">
<source src="${url}" type="video/mp4"</source></video>
#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
<item>
<title>${getTitle(tweet, prefs, retweet)}</title>
<dc:creator>@${tweet.profile.username}</dc:creator>
<description><![CDATA[${renderRssTweet(tweet, prefs, hostname).strip(chars={'\n'})}]]></description>
<pubDate>${getRfc822Time(tweet)}</pubDate>
<guid>https://${hostname & link}</guid>
<link>https://${hostname & link}</link>
</item>
# 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
<item>
<title>${getTitle(tweet, prefs, retweet)}</title>
<dc:creator>@${tweet.profile.username}</dc:creator>
<description><![CDATA[${renderRssTweet(tweet, prefs, cfg).strip(chars={'\n'})}]]></description>
<pubDate>${getRfc822Time(tweet)}</pubDate>
<guid>${urlPrefix & link}</guid>
<link>${urlPrefix & link}</link>
</item>
#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 @@
<?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">
<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>
<link>https://${hostname}/${profile.username}</link>
<description>Twitter feed for: ${user}. Generated by ${hostname}</description>
<link>${urlPrefix}/${profile.username}</link>
<description>${getDescription(user, cfg)}</description>
<language>en-us</language>
<ttl>40</ttl>
<image>
<title>${title}</title>
<link>https://${hostname}/${profile.username}</link>
<url>https://${hostname}${getPicUrl(profile.getUserPic(style="_400x400"))}</url>
<link>${urlPrefix}/${profile.username}</link>
<url>${urlPrefix}${getPicUrl(profile.getUserPic(style="_400x400"))}</url>
<width>128</width>
<height>128</height>
</image>
#if timeline.content.len > 0:
${renderRssTweets(timeline.content, prefs, hostname)}
#end if
#if timeline.content.len > 0:
${renderRssTweets(timeline.content, prefs, cfg)}
#end if
</channel>
</rss>
#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 = ""
<?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">
@ -103,17 +110,17 @@
<atom:link href="${link}" rel="self" type="application/rss+xml" />
<title>${xmltree.escape(list.name)} / @${list.username}</title>
<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>
<ttl>40</ttl>
${renderRssTweets(tweets, prefs, hostname)}
${renderRssTweets(tweets, prefs, cfg)}
</channel>
</rss>
#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 = ""
<?xml version="1.0" encoding="UTF-8"?>
@ -122,10 +129,10 @@
<atom:link href="${link}" rel="self" type="application/rss+xml" />
<title>Search results for "${escName}"</title>
<link>${link}</link>
<description>Twitter feed for search "${escName}". Generated by ${hostname}</description>
<description>${getDescription("Search \"" & escName & "\"", cfg)}</description>
<language>en-us</language>
<ttl>40</ttl>
${renderRssTweets(tweets, prefs, hostname)}
${renderRssTweets(tweets, prefs, cfg)}
</channel>
</rss>
#end proc