rss.nimf 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #? stdtmpl(subsChar = '$', metaChad = '#')
  2. #import strutils, xmltree, strformat
  3. #import ../types, ../utils, ../formatters
  4. #const hostname {.strdefine.} = "nitter.net"
  5. #
  6. #proc renderRssTweet(tweet: Tweet; prefs: Prefs): string =
  7. #let text = linkifyText(tweet.text, prefs, rss=true)
  8. #if tweet.quote.isSome and get(tweet.quote).available:
  9. #let quoteLink = hostname & getLink(get(tweet.quote))
  10. <p>${text}<br><a href="https://${quoteLink}">${quoteLink}</a></p>
  11. #else:
  12. <p>${text}</p>
  13. #end if
  14. #if tweet.photos.len > 0:
  15. <img src="https://${hostname}${getPicUrl(tweet.photos[0])}" width="250" />
  16. #elif tweet.video.isSome:
  17. <img src="https://${hostname}${getPicUrl(get(tweet.video).thumb)}" width="250" />
  18. #elif tweet.gif.isSome:
  19. #let thumb = &"https://{hostname}{getPicUrl(get(tweet.gif).thumb)}"
  20. #let url = &"https://{hostname}{getGifUrl(get(tweet.gif).url)}"
  21. <video poster="${thumb}" autoplay muted loop width="250">
  22. <source src="${url}" type="video/mp4"</source></video>
  23. #end if
  24. #end proc
  25. #
  26. #proc getTitle(tweet: Tweet; prefs: Prefs): string =
  27. #if tweet.pinned: result = "Pinned: "
  28. #elif tweet.retweet.isSome: result = "RT: "
  29. #end if
  30. #result &= xmltree.escape(replaceUrl(tweet.text, prefs))
  31. #if result.len > 0: return
  32. #end if
  33. #if tweet.photos.len > 0:
  34. # result &= "Image"
  35. #elif tweet.video.isSome:
  36. # result &= "Video"
  37. #elif tweet.gif.isSome:
  38. # result &= "Gif"
  39. #end if
  40. #end proc
  41. #
  42. #proc renderTimelineRss*(tweets: seq[Tweet]; profile: Profile): string =
  43. #let prefs = Prefs(replaceTwitter: hostname)
  44. #result = ""
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  47. <channel>
  48. <atom:link href="https://${hostname}/${profile.username}/rss" rel="self" type="application/rss+xml" />
  49. <title>${profile.fullname} / @${profile.username}</title>
  50. <link>https://${hostname}/${profile.username}</link>
  51. <description>Twitter feed for: @${profile.username}. Generated by ${hostname}</description>
  52. <language>en-us</language>
  53. <ttl>40</ttl>
  54. <image>
  55. <title>${profile.fullname} / @${profile.username}</title>
  56. <link>https://${hostname}/${profile.username}</link>
  57. <url>https://${hostname}${getPicUrl(profile.getUserPic(style="_400x400"))}</url>
  58. <width>128</width>
  59. <height>128</height>
  60. </image>
  61. #for tweet in tweets:
  62. <item>
  63. <title>${getTitle(tweet, prefs)}</title>
  64. <dc:creator>@${tweet.profile.username}</dc:creator>
  65. <description><![CDATA[${renderRssTweet(tweet, prefs).strip(chars={'\n'})}]]></description>
  66. <pubDate>${getRfc822Time(tweet)}</pubDate>
  67. <guid>https://${hostname}${getLink(tweet)}</guid>
  68. <link>https://${hostname}${getLink(tweet)}</link>
  69. </item>
  70. #end for
  71. </channel>
  72. </rss>
  73. #end proc
  74. #
  75. #proc renderListRss*(tweets: seq[Tweet]; name, list: string): string =
  76. #let prefs = Prefs(replaceTwitter: hostname)
  77. #result = ""
  78. <?xml version="1.0" encoding="UTF-8"?>
  79. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  80. <channel>
  81. <atom:link href="https://${hostname}/${name}/lists/${list}/rss" rel="self" type="application/rss+xml" />
  82. <title>${list} / @${name}</title>
  83. <link>https://${hostname}/${name}/lists/${list}</link>
  84. <description>Twitter feed for: ${list} by @${name}. Generated by ${hostname}</description>
  85. <language>en-us</language>
  86. <ttl>40</ttl>
  87. #for tweet in tweets:
  88. <item>
  89. <title>${getTitle(tweet, prefs)}</title>
  90. <dc:creator>@${tweet.profile.username}</dc:creator>
  91. <description><![CDATA[${renderRssTweet(tweet, prefs).strip(chars={'\n'})}]]></description>
  92. <pubDate>${getRfc822Time(tweet)}</pubDate>
  93. <guid>https://${hostname}${getLink(tweet)}</guid>
  94. <link>https://${hostname}${getLink(tweet)}</link>
  95. </item>
  96. #end for
  97. </channel>
  98. </rss>
  99. #end proc