Show video link in RSS feed

This commit is contained in:
blankie 2023-06-06 17:09:06 +07:00
parent cd1fd532e6
commit 4e0b16d2bc
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 67 additions and 17 deletions

View File

@ -15,7 +15,7 @@ proc redisKey*(page, name, cursor: string): string =
if cursor.len > 0: if cursor.len > 0:
result &= ":" & cursor result &= ":" & cursor
proc timelineRss*(req: Request; cfg: Config; query: Query; tab, param: string): Future[Rss] {.async.} = proc timelineRss*(req: Request; cfg: Config; query: Query; prefs: Prefs; tab, param: string): Future[Rss] {.async.} =
var profile: Profile var profile: Profile
let let
name = req.params.getOrDefault("name") name = req.params.getOrDefault("name")
@ -39,7 +39,7 @@ proc timelineRss*(req: Request; cfg: Config; query: Query; tab, param: string):
return Rss(feed: profile.user.username, cursor: "suspended") return Rss(feed: profile.user.username, cursor: "suspended")
if profile.user.fullname.len > 0: if profile.user.fullname.len > 0:
let rss = renderTimelineRss(profile, cfg, tab, param, multi=(names.len > 1)) let rss = renderTimelineRss(profile, cfg, prefs, tab, param, multi=(names.len > 1))
return Rss(feed: rss, cursor: profile.tweets.bottom) return Rss(feed: rss, cursor: profile.tweets.bottom)
template respRss*(rss, page) = template respRss*(rss, page) =
@ -78,7 +78,7 @@ proc createRssRouter*(cfg: Config) =
let tweets = await getGraphTweetSearch(query, cursor) let tweets = await getGraphTweetSearch(query, cursor)
rss.cursor = tweets.bottom rss.cursor = tweets.bottom
rss.feed = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg) rss.feed = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg, cookiePrefs())
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss, "Search") respRss(rss, "Search")
@ -94,7 +94,7 @@ proc createRssRouter*(cfg: Config) =
if rss.cursor.len > 0: if rss.cursor.len > 0:
respRss(rss, "User") respRss(rss, "User")
rss = await timelineRss(request, cfg, Query(fromUser: @[name]), "", "") rss = await timelineRss(request, cfg, Query(fromUser: @[name]), cookiePrefs(), "", "")
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss, "User") respRss(rss, "User")
@ -124,7 +124,7 @@ proc createRssRouter*(cfg: Config) =
if rss.cursor.len > 0: if rss.cursor.len > 0:
respRss(rss, "User") respRss(rss, "User")
rss = await timelineRss(request, cfg, query, tab, param) rss = await timelineRss(request, cfg, query, cookiePrefs(), tab, param)
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss, "User") respRss(rss, "User")
@ -161,7 +161,7 @@ proc createRssRouter*(cfg: Config) =
list = await getCachedList(id=id) list = await getCachedList(id=id)
timeline = await getGraphListTweets(list.id, cursor) timeline = await getGraphListTweets(list.id, cursor)
rss.cursor = timeline.bottom rss.cursor = timeline.bottom
rss.feed = renderListRss(timeline.content, list, cfg) rss.feed = renderListRss(timeline.content, list, cfg, cookiePrefs())
await cacheRss(key, rss) await cacheRss(key, rss)
respRss(rss, "List") respRss(rss, "List")

View File

@ -1,6 +1,6 @@
#? stdtmpl(subsChar = '$', metaChar = '#') #? stdtmpl(subsChar = '$', metaChar = '#')
## SPDX-License-Identifier: AGPL-3.0-only ## SPDX-License-Identifier: AGPL-3.0-only
#import strutils, xmltree, strformat, options, unicode #import strutils, xmltree, strformat, options, unicode, algorithm
#import ../types, ../utils, ../formatters, ../prefs #import ../types, ../utils, ../formatters, ../prefs
# #
#proc getTitle(tweet: Tweet; retweet: string): string = #proc getTitle(tweet: Tweet; retweet: string): string =
@ -28,7 +28,37 @@
Twitter feed for: ${desc}. Generated by ${cfg.hostname} Twitter feed for: ${desc}. Generated by ${cfg.hostname}
#end proc #end proc
# #
#proc renderRssTweet(tweet: Tweet; cfg: Config): string = ## Copied from views/tweet.nim
#proc isPlaybackEnabled(prefs: Prefs; playbackType: VideoType): bool =
# case playbackType
# of mp4: return prefs.mp4Playback
# of m3u8, vmap: return prefs.hlsPlayback
# end case
#end proc
#
#proc hasMp4Url(video: Video): bool =
# video.variants.anyIt(it.contentType == mp4)
#end proc
#
#proc getVideoDisabledText(playbackType: VideoType): string =
# case playbackType
# of mp4:
mp4 playback disabled in preferences
# of m3u8, vmap:
hls playback disabled in preferences
# end case
#end proc
#
#proc getVideoUnavailableText(video: Video): string =
# case video.reason
# of "dmcaed":
This media has been disabled in response to a report by the copyright owner
# else:
This media is unavailable
# end case
#end proc
#
#proc renderRssTweet(tweet: Tweet; cfg: Config; prefs: Prefs): string =
#let tweet = tweet.retweet.get(tweet) #let tweet = tweet.retweet.get(tweet)
#let urlPrefix = getUrlPrefix(cfg) #let urlPrefix = getUrlPrefix(cfg)
#let text = replaceUrls(tweet.text, defaultPrefs, absolute=urlPrefix) #let text = replaceUrls(tweet.text, defaultPrefs, absolute=urlPrefix)
@ -42,7 +72,27 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
<img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" /> <img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" />
# end for # end for
#elif tweet.video.isSome: #elif tweet.video.isSome:
<img src="${urlPrefix}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" /> # let video = get(tweet.video)
# let thumb = &"{urlPrefix}{getPicUrl(video.thumb)}"
# let playbackType = if not prefs.proxyVideos and video.hasMp4Url: mp4
# else: video.playbackType
# if not video.available:
<img src="${thumb}" style="max-width:250px;" />
<p>${getVideoUnavailableText(video)}</p>
# elif not isPlaybackEnabled(prefs, playbackType):
<img src="${thumb}" style="max-width:250px;" />
<p>${getVideoDisabledText(playbackType)}</p>
# else:
# let vars = video.variants.filterIt(it.contentType == playbackType)
# let vidUrl = vars.sortedByIt(it.resolution)[^1].url
# let source = if prefs.proxyVideos: getVidUrl(vidUrl)
# else: vidUrl
# let mutedAttribute = if prefs.muteVideos: "muted=\"\""
# else: ""
<video poster="${thumb}" controls="" ${mutedAttribute} style="max-width:250px;">
<source src="${source}" type="${playbackType}" />
</video>
# end if
#elif tweet.gif.isSome: #elif tweet.gif.isSome:
# let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}" # let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}"
# let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}" # let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}"
@ -56,7 +106,7 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
#end if #end if
#end proc #end proc
# #
#proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; userId=""): string = #proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; prefs: Prefs; userId=""): string =
#let urlPrefix = getUrlPrefix(cfg) #let urlPrefix = getUrlPrefix(cfg)
#var links: seq[string] #var links: seq[string]
#for thread in tweets: #for thread in tweets:
@ -73,7 +123,7 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
<item> <item>
<title>${getTitle(tweet, retweet)}</title> <title>${getTitle(tweet, retweet)}</title>
<dc:creator>@${tweet.user.username}</dc:creator> <dc:creator>@${tweet.user.username}</dc:creator>
<description><![CDATA[${renderRssTweet(tweet, cfg).strip(chars={'\n'})}]]></description> <description><![CDATA[${renderRssTweet(tweet, cfg, prefs).strip(chars={'\n'})}]]></description>
<pubDate>${getRfc822Time(tweet)}</pubDate> <pubDate>${getRfc822Time(tweet)}</pubDate>
<guid>${urlPrefix & link}</guid> <guid>${urlPrefix & link}</guid>
<link>${urlPrefix & link}</link> <link>${urlPrefix & link}</link>
@ -82,7 +132,7 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
#end for #end for
#end proc #end proc
# #
#proc renderTimelineRss*(profile: Profile; cfg: Config; tab, param: string; multi=false): string = #proc renderTimelineRss*(profile: Profile; cfg: Config; prefs: Prefs; tab, param: string; multi=false): string =
#let urlPrefix = getUrlPrefix(cfg) #let urlPrefix = getUrlPrefix(cfg)
#var atomLink = &"{urlPrefix}/{profile.user.username}" #var atomLink = &"{urlPrefix}/{profile.user.username}"
#var link = &"{urlPrefix}/{profile.user.username}" #var link = &"{urlPrefix}/{profile.user.username}"
@ -119,13 +169,13 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
<height>128</height> <height>128</height>
</image> </image>
#if profile.tweets.content.len > 0: #if profile.tweets.content.len > 0:
${renderRssTweets(profile.tweets.content, cfg, userId=profile.user.id)} ${renderRssTweets(profile.tweets.content, cfg, prefs, userId=profile.user.id)}
#end if #end if
</channel> </channel>
</rss> </rss>
#end proc #end proc
# #
#proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config): string = #proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config; prefs: Prefs): string =
#let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}" #let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}"
#result = "" #result = ""
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
@ -137,12 +187,12 @@ ${renderRssTweets(profile.tweets.content, cfg, userId=profile.user.id)}
<description>${getDescription(&"{list.name} by @{list.username}", cfg)}</description> <description>${getDescription(&"{list.name} by @{list.username}", cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
${renderRssTweets(tweets, cfg)} ${renderRssTweets(tweets, cfg, prefs)}
</channel> </channel>
</rss> </rss>
#end proc #end proc
# #
#proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config): string = #proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config; prefs: Prefs): string =
#let urlPrefix = getUrlPrefix(cfg) #let urlPrefix = getUrlPrefix(cfg)
#let escName = xmltree.escape(name) #let escName = xmltree.escape(name)
#let escParam = xmltree.escape(param) #let escParam = xmltree.escape(param)
@ -156,7 +206,7 @@ ${renderRssTweets(tweets, cfg)}
<description>${getDescription(&"Search \"{escName}\"", cfg)}</description> <description>${getDescription(&"Search \"{escName}\"", cfg)}</description>
<language>en-us</language> <language>en-us</language>
<ttl>40</ttl> <ttl>40</ttl>
${renderRssTweets(tweets, cfg)} ${renderRssTweets(tweets, cfg, prefs)}
</channel> </channel>
</rss> </rss>
#end proc #end proc