formatters.nim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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|.m4s|.m3u8|.vmap|.mp4))"
  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 getUrlPrefix*(cfg: Config): string =
  18. if cfg.useHttps: "https://" & cfg.hostname
  19. else: "http://" & cfg.hostname
  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 sanitizeXml*(text: string): string =
  29. text.replace(illegalXmlRegex, "")
  30. proc shortLink*(text: string; length=28): string =
  31. result = text.replace(wwwRegex, "")
  32. if result.len > length:
  33. result = result[0 ..< length] & "…"
  34. proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
  35. result = url
  36. if prefs.replaceYouTube.len > 0:
  37. result = result.replace(ytRegex, prefs.replaceYouTube)
  38. if prefs.replaceYouTube in result:
  39. result = result.replace("/c/", "/")
  40. if prefs.replaceInstagram.len > 0:
  41. result = result.replace(igRegex, prefs.replaceInstagram)
  42. if prefs.replaceTwitter.len > 0:
  43. result = result.replace(tco, "https://" & prefs.replaceTwitter & "/t.co")
  44. result = result.replace(cards, prefs.replaceTwitter & "/cards")
  45. result = result.replace(twRegex, prefs.replaceTwitter)
  46. if absolute.len > 0:
  47. result = result.replace("href=\"/", "href=\"" & absolute & "/")
  48. proc getM3u8Url*(content: string): string =
  49. var m: RegexMatch
  50. if content.find(m3u8Regex, m):
  51. result = content[m.group(0)[0]]
  52. proc proxifyVideo*(manifest: string; proxy: bool): string =
  53. proc cb(m: RegexMatch; s: string): string =
  54. result = "https://video.twimg.com/" & s[m.group(0)[0]]
  55. if proxy: result = getVidUrl(result)
  56. result = manifest.replace(manifestRegex, cb)
  57. proc getUserpic*(userpic: string; style=""): string =
  58. let pic = userpic.replace(userpicRegex, "$2")
  59. pic.replace(extRegex, style & "$1")
  60. proc getUserpic*(profile: Profile; style=""): string =
  61. getUserPic(profile.userpic, style)
  62. proc getVideoEmbed*(cfg: Config; id: int64): string =
  63. &"{getUrlPrefix(cfg)}/i/videos/{id}"
  64. proc pageTitle*(profile: Profile): string =
  65. &"{profile.fullname} (@{profile.username})"
  66. proc pageTitle*(tweet: Tweet): string =
  67. &"{pageTitle(tweet.profile)}: \"{stripHtml(tweet.text)}\""
  68. proc pageDesc*(profile: Profile): string =
  69. if profile.bio.len > 0:
  70. stripHtml(profile.bio)
  71. else:
  72. "The latest tweets from " & profile.fullname
  73. proc getJoinDate*(profile: Profile): string =
  74. profile.joinDate.format("'Joined' MMMM YYYY")
  75. proc getJoinDateFull*(profile: Profile): string =
  76. profile.joinDate.format("h:mm tt - d MMM YYYY")
  77. proc getTime*(tweet: Tweet): string =
  78. tweet.time.format("d/M/yyyy', 'HH:mm:ss")
  79. proc getRfc822Time*(tweet: Tweet): string =
  80. tweet.time.format("ddd', 'dd MMM yyyy HH:mm:ss 'GMT'")
  81. proc getTweetTime*(tweet: Tweet): string =
  82. tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
  83. proc getShortTime*(tweet: Tweet): string =
  84. let now = now()
  85. let since = now - tweet.time
  86. if now.year != tweet.time.year:
  87. result = tweet.time.format("d MMM yyyy")
  88. elif since.inDays >= 1:
  89. result = tweet.time.format("MMM d")
  90. elif since.inHours >= 1:
  91. result = $since.inHours & "h"
  92. elif since.inMinutes >= 1:
  93. result = $since.inMinutes & "m"
  94. elif since.inSeconds > 1:
  95. result = $since.inSeconds & "s"
  96. else:
  97. result = "now"
  98. proc getLink*(tweet: Tweet; focus=true): string =
  99. if tweet.id == 0: return
  100. var username = tweet.profile.username
  101. if username.len == 0:
  102. username = "i"
  103. result = &"/{username}/status/{tweet.id}"
  104. if focus: result &= "#m"
  105. proc getTwitterLink*(path: string; params: Table[string, string]): string =
  106. var
  107. username = params.getOrDefault("name")
  108. query = initQuery(params, username)
  109. path = path
  110. if "," in username:
  111. query.fromUser = username.split(",")
  112. path = "/search"
  113. if "/search" notin path and query.fromUser.len < 2:
  114. return $(twitter / path ? filterParams(params))
  115. let p = {
  116. "f": if query.kind == users: "user" else: "live",
  117. "q": genQueryParam(query),
  118. "src": "typed_query"
  119. }
  120. result = $(twitter / path ? p)
  121. if username.len > 0:
  122. result = result.replace("/" & username, "")
  123. proc getLocation*(u: Profile | Tweet): (string, string) =
  124. if "://" in u.location: return (u.location, "")
  125. let loc = u.location.split(":")
  126. let url = if loc.len > 1: "/search?q=place:" & loc[1] else: ""
  127. (loc[0], url)
  128. proc getSuspended*(username: string): string =
  129. &"User \"{username}\" has been suspended"