formatters.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import strutils, strformat, times, uri, tables
  2. import xmltree, htmlparser
  3. import regex
  4. import types, utils, query
  5. from unicode import Rune, `$`
  6. const
  7. ytRegex = re"([A-z.]+\.)?youtu(be\.com|\.be)"
  8. twRegex = re"(www\.|mobile\.)?twitter\.com"
  9. igRegex = re"(www\.)?instagram.com"
  10. cards = "cards.twitter.com/cards"
  11. tco = "https://t.co"
  12. nbsp = $Rune(0x000A0)
  13. wwwRegex = re"https?://(www[0-9]?\.)?"
  14. manifestRegex = re"(.+(.ts|.m3u8|.vmap))"
  15. userpicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$"
  16. extRegex = re"(\.[A-z]+)$"
  17. tombstoneRegex = re"\n* *Learn more"
  18. proc stripText*(text: string): string =
  19. text.replace(nbsp, " ").strip()
  20. proc stripHtml*(text: string): string =
  21. var html = parseHtml(text)
  22. for el in html.findAll("a"):
  23. let link = el.attr("href")
  24. if "http" in link:
  25. if el.len == 0: continue
  26. el[0].text = link
  27. html.innerText()
  28. proc shortLink*(text: string; length=28): string =
  29. result = text.replace(wwwRegex, "")
  30. if result.len > length:
  31. result = result[0 ..< length] & "…"
  32. proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
  33. result = url
  34. if prefs.replaceYouTube.len > 0:
  35. result = result.replace(ytRegex, prefs.replaceYouTube)
  36. if prefs.replaceYouTube in result:
  37. result = result.replace("/c/", "/")
  38. if prefs.replaceInstagram.len > 0:
  39. result = result.replace(igRegex, prefs.replaceInstagram)
  40. if prefs.replaceTwitter.len > 0:
  41. result = result.replace(tco, "https://" & prefs.replaceTwitter & "/t.co")
  42. result = result.replace(cards, prefs.replaceTwitter & "/cards")
  43. result = result.replace(twRegex, prefs.replaceTwitter)
  44. if absolute.len > 0:
  45. result = result.replace("href=\"/", "href=\"https://" & absolute & "/")
  46. proc proxifyVideo*(manifest: string; proxy: bool): string =
  47. proc cb(m: RegexMatch; s: string): string =
  48. result = "https://video.twimg.com" & s[m.group(0)[0]]
  49. if proxy: result = getVidUrl(result)
  50. result = manifest.replace(manifestRegex, cb)
  51. proc getUserpic*(userpic: string; style=""): string =
  52. let pic = userpic.replace(userpicRegex, "$2")
  53. pic.replace(extRegex, style & "$1")
  54. proc getUserpic*(profile: Profile; style=""): string =
  55. getUserPic(profile.userpic, style)
  56. proc getVideoEmbed*(cfg: Config; id: int64): string =
  57. &"https://{cfg.hostname}/i/videos/{id}"
  58. proc pageTitle*(profile: Profile): string =
  59. &"{profile.fullname} (@{profile.username})"
  60. proc pageTitle*(tweet: Tweet): string =
  61. &"{pageTitle(tweet.profile)}: \"{stripHtml(tweet.text)}\""
  62. proc pageDesc*(profile: Profile): string =
  63. if profile.bio.len > 0:
  64. stripHtml(profile.bio)
  65. else:
  66. "The latest tweets from " & profile.fullname
  67. proc getJoinDate*(profile: Profile): string =
  68. profile.joinDate.format("'Joined' MMMM YYYY")
  69. proc getJoinDateFull*(profile: Profile): string =
  70. profile.joinDate.format("h:mm tt - d MMM YYYY")
  71. proc getTime*(tweet: Tweet): string =
  72. tweet.time.format("d/M/yyyy', 'HH:mm:ss")
  73. proc getRfc822Time*(tweet: Tweet): string =
  74. tweet.time.format("ddd', 'd MMM yyyy HH:mm:ss 'GMT'")
  75. proc getTweetTime*(tweet: Tweet): string =
  76. tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
  77. proc getShortTime*(tweet: Tweet): string =
  78. let
  79. now = now().utc
  80. then = tweet.time.utc
  81. since = now - then
  82. if now.year != then.year:
  83. result = tweet.time.format("d MMM yyyy")
  84. elif since.inDays >= 1:
  85. result = tweet.time.format("MMM d")
  86. elif since.inHours >= 1:
  87. result = $since.inHours & "h"
  88. elif since.inMinutes >= 1:
  89. result = $since.inMinutes & "m"
  90. elif since.inSeconds > 1:
  91. result = $since.inSeconds & "s"
  92. else:
  93. # this shouldn't happen, but just in case
  94. result = "now"
  95. proc getLink*(tweet: Tweet; focus=true): string =
  96. if tweet.id == 0: return
  97. var username = tweet.profile.username
  98. if username.len == 0:
  99. username = "i"
  100. result = &"/{username}/status/{tweet.id}"
  101. if focus: result &= "#m"
  102. proc getTombstone*(text: string): string =
  103. text.replace(tombstoneRegex, "").stripText().strip(chars={' ', '\n'})
  104. proc getTwitterLink*(path: string; params: Table[string, string]): string =
  105. let
  106. twitter = parseUri("https://twitter.com")
  107. username = params.getOrDefault("name")
  108. query = initQuery(params, username)
  109. if "/search" notin path:
  110. return $(twitter / path ? filterParams(params))
  111. let p = {
  112. "f": $query.kind,
  113. "q": genQueryParam(query),
  114. "src": "typed_query"
  115. }
  116. result = $(parseUri("https://twitter.com") / path ? p)
  117. if username.len > 0:
  118. result = result.replace("/" & username, "")
  119. proc getLocation*(u: Profile | Tweet): (string, string) =
  120. if "://" in u.location: return (u.location, "")
  121. let loc = u.location.split(":")
  122. let url = if loc.len > 1: "/search?q=place:" & loc[1] else: ""
  123. (loc[0], url)
  124. proc getSuspended*(username: string): string =
  125. &"User \"{username}\" has been suspended"