rss.nimf 4.6 KB

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