Support RSS feeds for /media and /replies

This commit is contained in:
Zed 2019-09-15 12:10:43 +02:00
parent e72b48defc
commit 36484c73fd
3 changed files with 64 additions and 53 deletions

View File

@ -5,7 +5,7 @@ import jester
import types, config, prefs import types, config, prefs
import views/[general, about] import views/[general, about]
import routes/[preferences, timeline, media] import routes/[preferences, timeline, media, rss]
const configPath {.strdefine.} = "./nitter.conf" const configPath {.strdefine.} = "./nitter.conf"
let cfg = getConfig(configPath) let cfg = getConfig(configPath)
@ -13,6 +13,7 @@ let cfg = getConfig(configPath)
createPrefRouter(cfg) createPrefRouter(cfg)
createTimelineRouter(cfg) createTimelineRouter(cfg)
createMediaRouter(cfg) createMediaRouter(cfg)
createRssRouter(cfg)
settings: settings:
port = Port(cfg.port) port = Port(cfg.port)
@ -32,6 +33,7 @@ routes:
redirect("/" & @"query") redirect("/" & @"query")
extend preferences, "" extend preferences, ""
extend rss, ""
extend timeline, "" extend timeline, ""
extend media, "" extend media, ""

35
src/routes/rss.nim Normal file
View File

@ -0,0 +1,35 @@
import asyncdispatch, strutils
import jester
import router_utils, timeline
import ".."/[types, utils, cache, agents, search]
import ../views/general
include "../views/rss.nimf"
export uri
export cache, search, agents
proc showRss*(name: string; query: Option[Query]): Future[string] {.async.} =
let (profile, timeline, _) = await fetchSingleTimeline(name, "", getAgent(), query)
return renderTimelineRss(timeline.content, profile)
template respRss*(rss: typed) =
if rss.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
resp rss, "application/rss+xml;charset=utf-8"
proc createRssRouter*(cfg: Config) =
router rss:
get "/@name/rss":
cond '.' notin @"name"
respRss(await showRss(@"name", none(Query)))
get "/@name/replies/rss":
cond '.' notin @"name"
respRss(await showRss(@"name", some(getReplyQuery(@"name"))))
get "/@name/media/rss":
cond '.' notin @"name"
respRss(await showRss(@"name", some(getMediaQuery(@"name"))))

View File

@ -13,8 +13,10 @@ export router_utils
export api, cache, formatters, search, agents export api, cache, formatters, search, agents
export profile, timeline, status export profile, timeline, status
proc showSingleTimeline(name, after, agent: string; query: Option[Query]; type ProfileTimeline = (Profile, Timeline, seq[GalleryPhoto])
prefs: Prefs; path, title: string): Future[string] {.async.} =
proc fetchSingleTimeline*(name, after, agent: string;
query: Option[Query]): Future[ProfileTimeline] {.async.} =
let railFut = getPhotoRail(name, agent) let railFut = getPhotoRail(name, agent)
var timeline: Timeline var timeline: Timeline
@ -36,95 +38,67 @@ proc showSingleTimeline(name, after, agent: string; query: Option[Query];
profile = await getCachedProfile(name, agent) profile = await getCachedProfile(name, agent)
timeline = await timelineFut timeline = await timelineFut
if profile.username.len == 0: if profile.username.len == 0: return
return "" return (profile, timeline, await railFut)
let rssUrl = profile.username & "/rss" proc fetchMultiTimeline*(names: seq[string]; after, agent: string;
let profileHtml = renderProfile(profile, timeline, await railFut, prefs, path) query: Option[Query]): Future[Timeline] {.async.} =
return renderMain(profileHtml, prefs, title, pageTitle(profile),
pageDesc(profile), path, rss=rssUrl)
proc showMultiTimeline(names: seq[string]; after, agent: string; query: Option[Query];
prefs: Prefs; path, title: string): Future[string] {.async.} =
var q = query var q = query
if q.isSome: if q.isSome:
get(q).fromUser = names get(q).fromUser = names
else: else:
q = some(Query(kind: multi, fromUser: names, excludes: @["replies"])) q = some(Query(kind: multi, fromUser: names, excludes: @["replies"]))
var timeline = renderMulti(await getTimelineSearch(get(q), after, agent), return await getTimelineSearch(get(q), after, agent)
names.join(","), prefs, path)
return renderMain(timeline, prefs, title, "Multi")
proc showTimeline*(name, after: string; query: Option[Query]; proc showTimeline*(name, after: string; query: Option[Query];
prefs: Prefs; path, title: string): Future[string] {.async.} = prefs: Prefs; path, title, rss: string): Future[string] {.async.} =
let agent = getAgent() let agent = getAgent()
let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0) let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
if names.len == 1: if names.len == 1:
return await showSingleTimeline(names[0], after, agent, query, prefs, path, title) let (p, t, r) = await fetchSingleTimeline(names[0], after, agent, query)
if p.username.len == 0: return
let pHtml = renderProfile(p, t, r, prefs, path)
return renderMain(pHtml, prefs, title, pageTitle(p), pageDesc(p), path, rss=rss)
else: else:
return await showMultiTimeline(names, after, agent, query, prefs, path, title) let
timeline = await fetchMultiTimeline(names, after, agent, query)
html = renderMulti(timeline, names.join(","), prefs, path)
return renderMain(html, prefs, title, "Multi")
template respTimeline*(timeline: typed) = template respTimeline*(timeline: typed) =
if timeline.len == 0: if timeline.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title) resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
resp timeline resp timeline
proc showRssTimeline*(name: string): Future[string] {.async.} =
var timeline: Timeline
var profile: Profile
var cachedProfile = hasCachedProfile(name)
let agent = getAgent()
if cachedProfile.isSome:
profile = get(cachedProfile)
if cachedProfile.isSome:
timeline = await getTimeline(name, "", agent)
else:
(profile, timeline) = await getProfileAndTimeline(name, agent, "")
cache(profile)
if profile.username.len == 0:
return ""
return renderTimelineRss(timeline.content, profile)
proc createTimelineRouter*(cfg: Config) = proc createTimelineRouter*(cfg: Config) =
setProfileCacheTime(cfg.profileCacheTime) setProfileCacheTime(cfg.profileCacheTime)
router timeline: router timeline:
get "/@name/?": get "/@name/?":
cond '.' notin @"name" cond '.' notin @"name"
respTimeline(await showTimeline(@"name", @"after", none(Query), let rss = "/$1/rss" % @"name"
cookiePrefs(), getPath(), cfg.title)) respTimeline(await showTimeline(@"name", @"after", none(Query), cookiePrefs(),
getPath(), cfg.title, rss))
get "/@name/search": get "/@name/search":
cond '.' notin @"name" cond '.' notin @"name"
let query = initQuery(@"filter", @"include", @"not", @"sep", @"name") let query = initQuery(@"filter", @"include", @"not", @"sep", @"name")
respTimeline(await showTimeline(@"name", @"after", some(query), respTimeline(await showTimeline(@"name", @"after", some(query),
cookiePrefs(), getPath(), cfg.title)) cookiePrefs(), getPath(), cfg.title, ""))
get "/@name/replies": get "/@name/replies":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/replies/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name")), respTimeline(await showTimeline(@"name", @"after", some(getReplyQuery(@"name")),
cookiePrefs(), getPath(), cfg.title)) cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/media": get "/@name/media":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/media/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name")), respTimeline(await showTimeline(@"name", @"after", some(getMediaQuery(@"name")),
cookiePrefs(), getPath(), cfg.title)) cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/rss":
cond '.' notin @"name"
let rss = await showRssTimeline(@"name")
if rss.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
else:
resp rss, "application/rss+xml;charset=utf-8"
get "/@name/status/@id": get "/@name/status/@id":
cond '.' notin @"name" cond '.' notin @"name"