formatters.nim 4.9 KB

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