formatters.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import strutils, strformat, sequtils, times, uri, tables
  2. import xmltree, htmlparser, htmlgen
  3. import regex
  4. import types, utils, query
  5. from unicode import Rune, `$`
  6. const
  7. ytRegex = re"(www.|m.)?youtu(be.com|.be)"
  8. twRegex = re"(www.|mobile.)?twitter.com"
  9. nbsp = $Rune(0x000A0)
  10. proc stripText*(text: string): string =
  11. text.replace(nbsp, " ").strip()
  12. proc stripHtml*(text: string): string =
  13. var html = parseHtml(text)
  14. for el in html.findAll("a"):
  15. let link = el.attr("href")
  16. if "http" in link:
  17. el[0].text = link
  18. html.innerText()
  19. proc shortLink*(text: string; length=28): string =
  20. result = text.replace(re"https?://(www[0-9]?\.)?", "")
  21. if result.len > length:
  22. result = result[0 ..< length] & "…"
  23. proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
  24. result = url
  25. if prefs.replaceYouTube.len > 0:
  26. result = result.replace(ytRegex, prefs.replaceYouTube)
  27. if prefs.replaceTwitter.len > 0:
  28. result = result.replace(twRegex, prefs.replaceTwitter)
  29. if absolute.len > 0:
  30. result = result.replace("href=\"/", "href=\"https://" & absolute & "/")
  31. proc proxifyVideo*(manifest: string; proxy: bool): string =
  32. proc cb(m: RegexMatch; s: string): string =
  33. result = "https://video.twimg.com" & s[m.group(0)[0]]
  34. if proxy: result = getVidUrl(result)
  35. result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb)
  36. proc getUserpic*(userpic: string; style=""): string =
  37. let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
  38. pic.replace(re"(\.[A-z]+)$", style & "$1")
  39. proc getUserpic*(profile: Profile; style=""): string =
  40. getUserPic(profile.userpic, style)
  41. proc getVideoEmbed*(id: int): string =
  42. &"https://twitter.com/i/videos/{id}?embed_source=facebook"
  43. proc pageTitle*(profile: Profile): string =
  44. &"{profile.fullname} (@{profile.username})"
  45. proc pageDesc*(profile: Profile): string =
  46. if profile.bio.len > 0:
  47. stripHtml(profile.bio)
  48. else:
  49. "The latest tweets from " & profile.fullname
  50. proc getJoinDate*(profile: Profile): string =
  51. profile.joinDate.format("'Joined' MMMM YYYY")
  52. proc getJoinDateFull*(profile: Profile): string =
  53. profile.joinDate.format("h:mm tt - d MMM YYYY")
  54. proc getTime*(tweet: Tweet): string =
  55. tweet.time.format("d/M/yyyy', 'HH:mm:ss")
  56. proc getRfc822Time*(tweet: Tweet): string =
  57. tweet.time.format("ddd', 'd MMM yyyy HH:mm:ss 'GMT'")
  58. proc getTweetTime*(tweet: Tweet): string =
  59. tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
  60. proc getLink*(tweet: Tweet | Quote; focus=true): string =
  61. if tweet.id == 0: return
  62. result = &"/{tweet.profile.username}/status/{tweet.id}"
  63. if focus: result &= "#m"
  64. proc getTombstone*(text: string): string =
  65. text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'})
  66. proc getTwitterLink*(path: string; params: Table[string, string]): string =
  67. let
  68. twitter = parseUri("https://twitter.com")
  69. username = params.getOrDefault("name")
  70. query = initQuery(params, username)
  71. if "/search" notin path:
  72. return $(twitter / path ? filterParams(params))
  73. let p = {
  74. "f": $query.kind,
  75. "q": genQueryParam(query),
  76. "src": "typd",
  77. "max_position": params.getOrDefault("max_position", "0")
  78. }
  79. result = $(parseUri("https://twitter.com") / path ? p)
  80. if username.len > 0:
  81. result = result.replace("/" & username, "")