From 61991a9b562a72f90d26ef38156947e83245a9c3 Mon Sep 17 00:00:00 2001 From: Zed Date: Sat, 28 Sep 2019 03:22:46 +0200 Subject: [PATCH] Support RSS feeds for custom searches Fixes #45 --- src/routes/rss.nim | 11 ++++++ src/routes/search.nim | 6 ++- src/views/rss.nimf | 89 ++++++++++++++++++++++++++----------------- 3 files changed, 69 insertions(+), 37 deletions(-) diff --git a/src/routes/rss.nim b/src/routes/rss.nim index f40d939..c35a633 100644 --- a/src/routes/rss.nim +++ b/src/routes/rss.nim @@ -19,6 +19,17 @@ template respRss*(rss: typed) = proc createRssRouter*(cfg: Config) = router rss: + get "/search/rss": + if @"text".len > 200: + resp Http400, showError("Search input too long.", cfg.title) + + let query = initQuery(params(request)) + if query.kind != custom: + resp Http400, showError("Only Tweet searches are allowed for RSS feeds.", cfg.title) + + let tweets = await getSearch[Tweet](query, "", getAgent()) + respRss(renderSearchRss(tweets.content, query.text, genQueryUrl(query))) + get "/@name/rss": cond '.' notin @"name" respRss(await showRss(@"name", Query())) diff --git a/src/routes/search.nim b/src/routes/search.nim index 5606f3d..7f42ddc 100644 --- a/src/routes/search.nim +++ b/src/routes/search.nim @@ -10,7 +10,7 @@ export search proc createSearchRouter*(cfg: Config) = router search: - get "/search": + get "/search/?": if @"text".len > 200: resp Http400, showError("Search input too long.", cfg.title) @@ -25,7 +25,9 @@ proc createSearchRouter*(cfg: Config) = resp renderMain(renderUserSearch(users, prefs), request, cfg.title) of custom: let tweets = await getSearch[Tweet](query, @"after", getAgent()) - resp renderMain(renderTweetSearch(tweets, prefs, getPath()), request, cfg.title) + let rss = "/search/rss?" & genQueryUrl(query) + resp renderMain(renderTweetSearch(tweets, prefs, getPath()), request, + cfg.title, rss=rss) else: resp Http404, showError("Invalid search.", cfg.title) diff --git a/src/views/rss.nimf b/src/views/rss.nimf index ac8bfc4..717dcef 100644 --- a/src/views/rss.nimf +++ b/src/views/rss.nimf @@ -3,6 +3,22 @@ #import ../types, ../utils, ../formatters #const hostname {.strdefine.} = "nitter.net" # +#proc getTitle(tweet: Tweet; prefs: Prefs): string = +#if tweet.pinned: result = "Pinned: " +#elif tweet.retweet.isSome: result = "RT: " +#end if +#result &= xmltree.escape(replaceUrl(tweet.text, prefs)) +#if result.len > 0: return +#end if +#if tweet.photos.len > 0: +# result &= "Image" +#elif tweet.video.isSome: +# result &= "Video" +#elif tweet.gif.isSome: +# result &= "Gif" +#end if +#end proc +# #proc renderRssTweet(tweet: Tweet; prefs: Prefs): string = #let text = linkifyText(tweet.text, prefs, rss=true) #if tweet.quote.isSome and get(tweet.quote).available: @@ -23,20 +39,22 @@ #end if #end proc # -#proc getTitle(tweet: Tweet; prefs: Prefs): string = -#if tweet.pinned: result = "Pinned: " -#elif tweet.retweet.isSome: result = "RT: " -#end if -#result &= xmltree.escape(replaceUrl(tweet.text, prefs)) -#if result.len > 0: return -#end if -#if tweet.photos.len > 0: -# result &= "Image" -#elif tweet.video.isSome: -# result &= "Video" -#elif tweet.gif.isSome: -# result &= "Gif" +#proc renderRssTweets(tweets: seq[Tweet]; prefs: Prefs): string = +#var links: seq[string] +#for tweet in tweets: +#let link = getLink(tweet) +#if link in links: continue #end if +#links.add link + + ${getTitle(tweet, prefs)} + @${tweet.profile.username} + + ${getRfc822Time(tweet)} + https://${hostname & link} + https://${hostname & link} + +#end for #end proc # #proc renderTimelineRss*(tweets: seq[Tweet]; profile: Profile): string = @@ -58,42 +76,43 @@ 128 128 - #for tweet in tweets: - - ${getTitle(tweet, prefs)} - @${tweet.profile.username} - - ${getRfc822Time(tweet)} - https://${hostname}${getLink(tweet)} - https://${hostname}${getLink(tweet)} - - #end for + ${renderRssTweets(tweets, prefs)} #end proc # #proc renderListRss*(tweets: seq[Tweet]; name, list: string): string = #let prefs = Prefs(replaceTwitter: hostname) +#let link = &"https://{hostname}/{name}/lists/{list}" #result = "" - + ${list} / @${name} - https://${hostname}/${name}/lists/${list} + ${link} Twitter feed for: ${list} by @${name}. Generated by ${hostname} en-us 40 - #for tweet in tweets: - - ${getTitle(tweet, prefs)} - @${tweet.profile.username} - - ${getRfc822Time(tweet)} - https://${hostname}${getLink(tweet)} - https://${hostname}${getLink(tweet)} - - #end for + ${renderRssTweets(tweets, prefs)} + + +#end proc +# +#proc renderSearchRss*(tweets: seq[Tweet]; name, param: string): string = +#let prefs = Prefs(replaceTwitter: hostname) +#let link = &"https://{hostname}/search" +#result = "" + + + + + Search results for "${name}" + ${link} + Twitter feed for search "${name}". Generated by ${hostname} + en-us + 40 + ${renderRssTweets(tweets, prefs)} #end proc