formatters.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. const hostname {.strdefine.} = "nitter.net"
  11. proc stripText*(text: string): string =
  12. text.replace(nbsp, " ").strip()
  13. proc stripHtml*(text: string): string =
  14. let html = parseHtml(text)
  15. html.innerText()
  16. proc shortLink*(text: string; length=28): string =
  17. result = text.replace(re"https?://(www.)?", "")
  18. if result.len > length:
  19. result = result[0 ..< length] & "…"
  20. proc replaceUrl*(url: string; prefs: Prefs; rss=false): string =
  21. result = url
  22. if prefs.replaceYouTube.len > 0:
  23. result = result.replace(ytRegex, prefs.replaceYouTube)
  24. if prefs.replaceTwitter.len > 0:
  25. result = result.replace(twRegex, prefs.replaceTwitter)
  26. if rss:
  27. result = result.replace("href=\"/", "href=\"https://" & hostname & "/")
  28. proc proxifyVideo*(manifest: string; proxy: bool): string =
  29. proc cb(m: RegexMatch; s: string): string =
  30. result = "https://video.twimg.com" & s[m.group(0)[0]]
  31. if proxy: result = getVidUrl(result)
  32. result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb)
  33. proc getUserpic*(userpic: string; style=""): string =
  34. let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
  35. pic.replace(re"(\.[A-z]+)$", style & "$1")
  36. proc getUserpic*(profile: Profile; style=""): string =
  37. getUserPic(profile.userpic, style)
  38. proc getVideoEmbed*(id: string): string =
  39. &"https://twitter.com/i/videos/{id}?embed_source=facebook"
  40. proc pageTitle*(profile: Profile): string =
  41. &"{profile.fullname} (@{profile.username})"
  42. proc pageDesc*(profile: Profile): string =
  43. "The latest tweets from " & profile.fullname
  44. proc getJoinDate*(profile: Profile): string =
  45. profile.joinDate.format("'Joined' MMMM YYYY")
  46. proc getJoinDateFull*(profile: Profile): string =
  47. profile.joinDate.format("h:mm tt - d MMM YYYY")
  48. proc getTime*(tweet: Tweet): string =
  49. tweet.time.format("d/M/yyyy', 'HH:mm:ss")
  50. proc getRfc822Time*(tweet: Tweet): string =
  51. tweet.time.format("ddd', 'd MMM yyyy HH:mm:ss 'GMT'")
  52. proc getTweetTime*(tweet: Tweet): string =
  53. tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
  54. proc getLink*(tweet: Tweet | Quote): string =
  55. if tweet.id.len == 0: return
  56. &"/{tweet.profile.username}/status/{tweet.id}"
  57. proc getTombstone*(text: string): string =
  58. text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'})
  59. proc getTwitterLink*(path: string; params: Table[string, string]): string =
  60. let
  61. twitter = parseUri("https://twitter.com")
  62. username = params.getOrDefault("name")
  63. query = initQuery(params, username)
  64. if "/search" notin path:
  65. return $(twitter / path ? filterParams(params))
  66. let p = {
  67. "f": $query.kind,
  68. "q": genQueryParam(query),
  69. "src": "typd",
  70. "max_position": params.getOrDefault("max_position", "0")
  71. }
  72. result = $(parseUri("https://twitter.com") / path ? p)
  73. if username.len > 0:
  74. result = result.replace("/" & username, "")