formatters.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. var html = parseHtml(text)
  15. for el in html.findAll("a"):
  16. let link = el.attr("href")
  17. if "http" in link:
  18. el[0].text = link
  19. html.innerText()
  20. proc shortLink*(text: string; length=28): string =
  21. result = text.replace(re"https?://(www.)?", "")
  22. if result.len > length:
  23. result = result[0 ..< length] & "…"
  24. proc replaceUrl*(url: string; prefs: Prefs; rss=false): string =
  25. result = url
  26. if prefs.replaceYouTube.len > 0:
  27. result = result.replace(ytRegex, prefs.replaceYouTube)
  28. if prefs.replaceTwitter.len > 0:
  29. result = result.replace(twRegex, prefs.replaceTwitter)
  30. if rss:
  31. result = result.replace("href=\"/", "href=\"https://" & hostname & "/")
  32. proc proxifyVideo*(manifest: string; proxy: bool): string =
  33. proc cb(m: RegexMatch; s: string): string =
  34. result = "https://video.twimg.com" & s[m.group(0)[0]]
  35. if proxy: result = getVidUrl(result)
  36. result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb)
  37. proc getUserpic*(userpic: string; style=""): string =
  38. let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
  39. pic.replace(re"(\.[A-z]+)$", style & "$1")
  40. proc getUserpic*(profile: Profile; style=""): string =
  41. getUserPic(profile.userpic, style)
  42. proc getVideoEmbed*(id: int): string =
  43. &"https://twitter.com/i/videos/{id}?embed_source=facebook"
  44. proc pageTitle*(profile: Profile): string =
  45. &"{profile.fullname} (@{profile.username})"
  46. proc pageDesc*(profile: Profile): string =
  47. if profile.bio.len > 0:
  48. stripHtml(profile.bio)
  49. else:
  50. "The latest tweets from " & profile.fullname
  51. proc getJoinDate*(profile: Profile): string =
  52. profile.joinDate.format("'Joined' MMMM YYYY")
  53. proc getJoinDateFull*(profile: Profile): string =
  54. profile.joinDate.format("h:mm tt - d MMM YYYY")
  55. proc getTime*(tweet: Tweet): string =
  56. tweet.time.format("d/M/yyyy', 'HH:mm:ss")
  57. proc getRfc822Time*(tweet: Tweet): string =
  58. tweet.time.format("ddd', 'd MMM yyyy HH:mm:ss 'GMT'")
  59. proc getTweetTime*(tweet: Tweet): string =
  60. tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
  61. proc getLink*(tweet: Tweet | Quote): string =
  62. if tweet.id == 0: return
  63. &"/{tweet.profile.username}/status/{tweet.id}"
  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, "")