nitter/src/routes/rss.nim

84 lines
2.8 KiB
Nim
Raw Normal View History

import asyncdispatch, strutils
import jester
import router_utils, timeline
2019-09-13 21:28:20 +00:00
import ".."/[cache, agents, query]
import ../views/general
include "../views/rss.nimf"
2019-12-08 10:56:20 +00:00
proc showRss*(req: Request; hostname: string; query: Query): Future[(string, string)] {.async.} =
2019-12-04 04:58:18 +00:00
var profile: Profile
var timeline: Timeline
2019-12-08 10:56:20 +00:00
let
name = req.params.getOrDefault("name")
after = req.params.getOrDefault("max_position")
names = getNames(name)
2019-12-04 04:58:18 +00:00
if names.len == 1:
(profile, timeline) =
2019-12-08 10:56:20 +00:00
await fetchSingleTimeline(names[0], after, getAgent(), query, media=false)
2019-12-04 04:58:18 +00:00
else:
let multiQuery = query.getMultiQuery(names)
timeline = await getSearch[Tweet](multiQuery, after, getAgent(), media=false)
2019-12-04 04:58:18 +00:00
# this is kinda dumb
profile = Profile(
username: name,
fullname: names.join(" | "),
userpic: "https://abs.twimg.com/sticky/default_profile_images/default_profile.png"
)
2020-04-14 21:56:31 +00:00
if profile.suspended:
return (profile.username, "suspended")
if timeline != nil:
2019-12-08 10:56:20 +00:00
let rss = renderTimelineRss(timeline, profile, hostname, multi=(names.len > 1))
return (rss, timeline.minId)
2019-12-08 10:56:20 +00:00
template respRss*(rss, minId) =
if rss.len == 0:
2019-10-21 05:59:22 +00:00
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
2020-04-14 21:56:31 +00:00
elif minId == "suspended":
resp Http404, showError(getSuspended(rss), cfg)
2019-12-08 10:56:20 +00:00
let headers = {"Content-Type": "application/rss+xml;charset=utf-8", "Min-Id": minId}
resp Http200, headers, rss
proc createRssRouter*(cfg: Config) =
router rss:
get "/search/rss":
if @"q".len > 200:
resp Http400, showError("Search input too long.", cfg)
let query = initQuery(params(request))
if query.kind != tweets:
resp Http400, showError("Only Tweet searches are allowed for RSS feeds.", cfg)
2019-12-08 10:56:20 +00:00
let tweets = await getSearch[Tweet](query, @"max_position", getAgent(), media=false)
let rss = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg.hostname)
respRss(rss, tweets.minId)
get "/@name/rss":
cond '.' notin @"name"
2019-12-08 10:56:20 +00:00
let (rss, minId) = await showRss(request, cfg.hostname, Query())
respRss(rss, minId)
2019-12-08 10:56:20 +00:00
get "/@name/@tab/rss":
cond '.' notin @"name"
2019-12-08 11:38:55 +00:00
cond @"tab" in ["with_replies", "media", "search"]
2019-12-08 10:56:20 +00:00
let query =
case @"tab"
of "with_replies": getReplyQuery(@"name")
of "media": getMediaQuery(@"name")
of "search": initQuery(params(request), name=(@"name"))
else: Query()
2019-09-20 01:35:27 +00:00
2019-12-08 10:56:20 +00:00
let (rss, minId) = await showRss(request, cfg.hostname, query)
respRss(rss, minId)
2019-09-20 23:08:30 +00:00
get "/@name/lists/@list/rss":
cond '.' notin @"name"
2019-12-08 10:56:20 +00:00
let list = await getListTimeline(@"name", @"list", @"max_position", getAgent(), media=false)
let rss = renderListRss(list.content, @"name", @"list", cfg.hostname)
respRss(rss, list.minId)