rss.nimf 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #? stdtmpl(subsChar = '$', metaChad = '#')
  2. #import strutils, xmltree, strformat, options
  3. #import ../types, ../utils, ../formatters
  4. #
  5. #proc getTitle(tweet: Tweet; prefs: Prefs; retweet: string): string =
  6. #if tweet.pinned: result = "Pinned: "
  7. #elif retweet.len > 0: result = &"RT by @{retweet}: "
  8. #elif tweet.reply.len > 0: result = &"R to @{tweet.reply[0]}: "
  9. #end if
  10. #result &= xmltree.escape(stripHtml(tweet.text))
  11. #if result.len > 0: return
  12. #end if
  13. #if tweet.photos.len > 0:
  14. # result &= "Image"
  15. #elif tweet.video.isSome:
  16. # result &= "Video"
  17. #elif tweet.gif.isSome:
  18. # result &= "Gif"
  19. #end if
  20. #end proc
  21. #
  22. #proc renderRssTweet(tweet: Tweet; prefs: Prefs; hostname: string): string =
  23. #let tweet = tweet.retweet.get(tweet)
  24. #let text = replaceUrl(tweet.text, prefs, absolute=hostname)
  25. #if tweet.quote.isSome and get(tweet.quote).available:
  26. #let quoteLink = hostname & getLink(get(tweet.quote))
  27. <p>${text}<br><a href="https://${quoteLink}">${quoteLink}</a></p>
  28. #else:
  29. <p>${text}</p>
  30. #end if
  31. #if tweet.photos.len > 0:
  32. #for photo in tweet.photos:
  33. <img src="https://${hostname}${getPicUrl(photo)}" style="max-width:250px;" />
  34. #end for
  35. #elif tweet.video.isSome:
  36. <img src="https://${hostname}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" />
  37. #elif tweet.gif.isSome:
  38. #let thumb = &"https://{hostname}{getPicUrl(get(tweet.gif).thumb)}"
  39. #let url = &"https://{hostname}{getPicUrl(get(tweet.gif).url)}"
  40. <video poster="${thumb}" autoplay muted loop style="max-width:250px;">
  41. <source src="${url}" type="video/mp4"</source></video>
  42. #end if
  43. #end proc
  44. #
  45. #proc renderRssTweets(tweets: seq[Tweet]; prefs: Prefs; hostname: string): string =
  46. #var links: seq[string]
  47. #for t in tweets:
  48. #let retweet = if t.retweet.isSome: t.profile.username else: ""
  49. #let tweet = if retweet.len > 0: t.retweet.get else: t
  50. #let link = getLink(tweet)
  51. #if link in links: continue
  52. #end if
  53. #links.add link
  54. <item>
  55. <title>${getTitle(tweet, prefs, retweet)}</title>
  56. <dc:creator>@${tweet.profile.username}</dc:creator>
  57. <description><![CDATA[${renderRssTweet(tweet, prefs, hostname).strip(chars={'\n'})}]]></description>
  58. <pubDate>${getRfc822Time(tweet)}</pubDate>
  59. <guid>https://${hostname & link}</guid>
  60. <link>https://${hostname & link}</link>
  61. </item>
  62. #end for
  63. #end proc
  64. #
  65. #proc renderTimelineRss*(timeline: Timeline; profile: Profile; hostname: string; multi=false): string =
  66. #let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidio.us")
  67. #result = ""
  68. #let user = (if multi: "" else: "@") & profile.username
  69. #var title = profile.fullname
  70. #if not multi: title &= " / " & user
  71. #end if
  72. #title = xmltree.escape(title)
  73. <?xml version="1.0" encoding="UTF-8"?>
  74. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  75. <channel>
  76. <atom:link href="https://${hostname}/${profile.username}/rss" rel="self" type="application/rss+xml" />
  77. <title>${title}</title>
  78. <link>https://${hostname}/${profile.username}</link>
  79. <description>Twitter feed for: ${user}. Generated by ${hostname}</description>
  80. <language>en-us</language>
  81. <ttl>40</ttl>
  82. <image>
  83. <title>${title}</title>
  84. <link>https://${hostname}/${profile.username}</link>
  85. <url>https://${hostname}${getPicUrl(profile.getUserPic(style="_400x400"))}</url>
  86. <width>128</width>
  87. <height>128</height>
  88. </image>
  89. # if timeline.content.len > 0:
  90. ${renderRssTweets(timeline.content, prefs, hostname)}
  91. #end if
  92. </channel>
  93. </rss>
  94. #end proc
  95. #
  96. #proc renderListRss*(tweets: seq[Tweet]; list: List; hostname: string): string =
  97. #let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidio.us")
  98. #let link = &"https://{hostname}/{list.username}/lists/{list.name}"
  99. #result = ""
  100. <?xml version="1.0" encoding="UTF-8"?>
  101. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  102. <channel>
  103. <atom:link href="${link}" rel="self" type="application/rss+xml" />
  104. <title>${xmltree.escape(list.name)} / @${list.username}</title>
  105. <link>${link}</link>
  106. <description>Twitter feed for: ${list.name} by @${list.username}. Generated by ${hostname}</description>
  107. <language>en-us</language>
  108. <ttl>40</ttl>
  109. ${renderRssTweets(tweets, prefs, hostname)}
  110. </channel>
  111. </rss>
  112. #end proc
  113. #
  114. #proc renderSearchRss*(tweets: seq[Tweet]; name, param, hostname: string): string =
  115. #let prefs = Prefs(replaceTwitter: hostname, replaceYouTube: "invidio.us")
  116. #let link = &"https://{hostname}/search"
  117. #let escName = xmltree.escape(name)
  118. #result = ""
  119. <?xml version="1.0" encoding="UTF-8"?>
  120. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  121. <channel>
  122. <atom:link href="${link}" rel="self" type="application/rss+xml" />
  123. <title>Search results for "${escName}"</title>
  124. <link>${link}</link>
  125. <description>Twitter feed for search "${escName}". Generated by ${hostname}</description>
  126. <language>en-us</language>
  127. <ttl>40</ttl>
  128. ${renderRssTweets(tweets, prefs, hostname)}
  129. </channel>
  130. </rss>
  131. #end proc