nitter/src/routes/rss.nim

136 lines
3.8 KiB
Nim
Raw Normal View History

2020-11-15 00:21:56 +00:00
import asyncdispatch, strutils, tables, times, sequtils, hashes, supersnappy
import jester
import router_utils, timeline
2020-06-09 14:45:21 +00:00
import ../query
include "../views/rss.nimf"
2020-11-15 00:21:56 +00:00
export times, hashes, supersnappy
2020-06-01 00:22:56 +00:00
proc showRss*(req: Request; hostname: string; query: Query): Future[Rss] {.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")
2020-06-01 00:22:56 +00:00
after = getCursor(req)
2019-12-08 10:56:20 +00:00
names = getNames(name)
2019-12-04 04:58:18 +00:00
if names.len == 1:
(profile, timeline) =
2020-06-05 14:49:10 +00:00
await fetchSingleTimeline(after, query, skipRail=true)
2019-12-04 04:58:18 +00:00
else:
var q = query
q.fromUser = names
timeline = await getSearch[Tweet](q, after)
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 Rss(feed: profile.username, cursor: "suspended")
2020-04-14 21:56:31 +00:00
if profile.fullname.len > 0:
let rss = compress renderTimelineRss(timeline, profile, hostname,
multi=(names.len > 1))
return Rss(feed: rss, cursor: timeline.bottom)
template respRss*(rss) =
if rss.cursor.len == 0:
2019-10-21 05:59:22 +00:00
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
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)
2020-06-01 00:22:56 +00:00
let
cursor = getCursor()
2020-06-05 14:27:48 +00:00
key = $hash(genQueryUrl(query)) & cursor
2020-06-01 00:22:56 +00:00
var rss = await getCachedRss(key)
if rss.cursor.len > 0:
respRss(rss)
2020-06-01 00:22:56 +00:00
let tweets = await getSearch[Tweet](query, cursor)
rss.cursor = tweets.bottom
rss.feed = compress renderSearchRss(tweets.content, query.text,
genQueryUrl(query), cfg.hostname)
2020-06-01 00:22:56 +00:00
await cacheRss(key, rss)
respRss(rss)
get "/@name/rss":
cond '.' notin @"name"
2020-06-01 00:22:56 +00:00
let
cursor = getCursor()
name = @"name"
key = name & cursor
2020-06-01 00:22:56 +00:00
var rss = await getCachedRss(key)
if rss.cursor.len > 0:
respRss(rss)
2020-06-01 00:22:56 +00:00
rss = await showRss(request, cfg.hostname, Query(fromUser: @[name]))
2020-06-01 00:22:56 +00:00
await cacheRss(key, rss)
respRss(rss)
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"]
let name = @"name"
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(fromUser: @[name])
2019-09-20 01:35:27 +00:00
2020-06-05 14:27:48 +00:00
var key = @"name" & "/" & @"tab"
if @"tab" == "search":
2020-06-22 01:50:16 +00:00
key &= $hash(genQueryUrl(query))
2020-06-05 14:27:48 +00:00
key &= getCursor()
2020-06-01 00:22:56 +00:00
var rss = await getCachedRss(key)
if rss.cursor.len > 0:
respRss(rss)
rss = await showRss(request, cfg.hostname, query)
2020-06-01 00:22:56 +00:00
await cacheRss(key, rss)
respRss(rss)
2019-09-20 23:08:30 +00:00
get "/@name/lists/@list/rss":
cond '.' notin @"name"
2020-06-01 00:22:56 +00:00
let
cursor = getCursor()
key = @"name" & "/" & @"list" & cursor
var rss = await getCachedRss(key)
if rss.cursor.len > 0:
respRss(rss)
2020-06-01 00:22:56 +00:00
let
list = await getCachedList(@"name", @"list")
timeline = await getListTimeline(list.id, cursor)
rss.cursor = timeline.bottom
rss.feed = compress renderListRss(timeline.content, list, cfg.hostname)
2020-06-01 00:22:56 +00:00
await cacheRss(key, rss)
respRss(rss)