formatters.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. el[0].text = link
  26. html.innerText()
  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 proxifyVideo*(manifest: string; proxy: bool): string =
  46. proc cb(m: RegexMatch; s: string): string =
  47. result = "https://video.twimg.com" & s[m.group(0)[0]]
  48. if proxy: result = getVidUrl(result)
  49. result = manifest.replace(manifestRegex, cb)
  50. proc getUserpic*(userpic: string; style=""): string =
  51. let pic = userpic.replace(userpicRegex, "$2")
  52. pic.replace(extRegex, style & "$1")
  53. proc getUserpic*(profile: Profile; style=""): string =
  54. getUserPic(profile.userpic, style)
  55. proc getVideoEmbed*(cfg: Config; id: int64): string =
  56. &"https://{cfg.hostname}/i/videos/{id}"
  57. proc pageTitle*(profile: Profile): string =
  58. &"{profile.fullname} (@{profile.username})"
  59. proc pageDesc*(profile: Profile): string =
  60. if profile.bio.len > 0:
  61. stripHtml(profile.bio)
  62. else:
  63. "The latest tweets from " & profile.fullname
  64. proc getJoinDate*(profile: Profile): string =
  65. profile.joinDate.format("'Joined' MMMM YYYY")
  66. proc getJoinDateFull*(profile: Profile): string =
  67. profile.joinDate.format("h:mm tt - d MMM YYYY")
  68. proc getTime*(tweet: Tweet): string =
  69. tweet.time.format("d/M/yyyy', 'HH:mm:ss")
  70. proc getRfc822Time*(tweet: Tweet): string =
  71. tweet.time.format("ddd', 'd MMM yyyy HH:mm:ss 'GMT'")
  72. proc getTweetTime*(tweet: Tweet): string =
  73. tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
  74. proc getLink*(tweet: Tweet | Quote; focus=true): string =
  75. if tweet.id == 0: return
  76. result = &"/{tweet.profile.username}/status/{tweet.id}"
  77. if focus: result &= "#m"
  78. proc getTombstone*(text: string): string =
  79. text.replace(tombstoneRegex, "").stripText().strip(chars={' ', '\n'})
  80. proc getTwitterLink*(path: string; params: Table[string, string]): string =
  81. let
  82. twitter = parseUri("https://twitter.com")
  83. username = params.getOrDefault("name")
  84. query = initQuery(params, username)
  85. if "/search" notin path:
  86. return $(twitter / path ? filterParams(params))
  87. let p = {
  88. "f": $query.kind,
  89. "q": genQueryParam(query),
  90. "src": "typd",
  91. "max_position": params.getOrDefault("max_position", "0")
  92. }
  93. result = $(parseUri("https://twitter.com") / path ? p)
  94. if username.len > 0:
  95. result = result.replace("/" & username, "")
  96. proc getLocation*(u: Profile | Tweet): (string, string) =
  97. if "://" in u.location: return (u.location, "")
  98. let loc = u.location.split(":")
  99. let url = if loc.len > 1: "/search?q=place:" & loc[1] else: ""
  100. (loc[0], url)