nitter/src/formatters.nim

166 lines
5.1 KiB
Nim
Raw Normal View History

import strutils, strformat, times, uri, tables, xmltree, htmlparser
2019-06-20 14:16:20 +00:00
import regex
2019-10-08 13:07:10 +00:00
import types, utils, query
2019-06-20 14:16:20 +00:00
const
2020-01-19 07:49:20 +00:00
ytRegex = re"([A-z.]+\.)?youtu(be\.com|\.be)"
twRegex = re"(?<![^\/> ])(?<![^\/]\/)(www\.|mobile\.)?twitter\.com"
igRegex = re"(www\.)?instagram\.com"
2020-03-08 23:33:52 +00:00
cards = "cards.twitter.com/cards"
tco = "https://t.co"
2019-06-25 00:38:18 +00:00
2020-01-22 12:04:35 +00:00
wwwRegex = re"https?://(www[0-9]?\.)?"
2020-06-09 13:04:38 +00:00
m3u8Regex = re"""url="(.+.m3u8)""""
manifestRegex = re"\/(.+(.ts|.m4s|.m3u8|.vmap|.mp4))"
2020-01-22 12:04:35 +00:00
userpicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$"
extRegex = re"(\.[A-z]+)$"
illegalXmlRegex = re"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]"
2019-06-20 14:16:20 +00:00
twitter = parseUri("https://twitter.com")
2021-01-08 01:25:43 +00:00
proc getUrlPrefix*(cfg: Config): string =
if cfg.useHttps: "https://" & cfg.hostname
else: "http://" & cfg.hostname
proc stripHtml*(text: string): string =
2019-10-11 17:20:40 +00:00
var html = parseHtml(text)
for el in html.findAll("a"):
let link = el.attr("href")
if "http" in link:
2020-06-01 00:25:39 +00:00
if el.len == 0: continue
2019-10-11 17:20:40 +00:00
el[0].text = link
html.innerText()
proc sanitizeXml*(text: string): string =
text.replace(illegalXmlRegex, "")
2019-06-20 14:16:20 +00:00
proc shortLink*(text: string; length=28): string =
2020-01-22 12:04:35 +00:00
result = text.replace(wwwRegex, "")
2019-06-20 14:16:20 +00:00
if result.len > length:
result = result[0 ..< length] & ""
proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
2019-08-15 16:45:56 +00:00
result = url
if prefs.replaceYouTube.len > 0 and ytRegex in result:
result = result.replace(ytRegex, prefs.replaceYouTube)
2020-03-08 23:47:00 +00:00
if prefs.replaceYouTube in result:
result = result.replace("/c/", "/")
if prefs.replaceTwitter.len > 0 and
(twRegex in result or tco in result):
result = result.replace(tco, "https://" & prefs.replaceTwitter & "/t.co")
2020-03-08 23:33:52 +00:00
result = result.replace(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter)
if prefs.replaceInstagram.len > 0 and igRegex in result:
result = result.replace(igRegex, prefs.replaceInstagram)
if absolute.len > 0 and "href" in result:
2021-01-08 01:25:43 +00:00
result = result.replace("href=\"/", "href=\"" & absolute & "/")
2019-06-20 14:16:20 +00:00
2020-06-09 13:04:38 +00:00
proc getM3u8Url*(content: string): string =
var m: RegexMatch
if content.find(m3u8Regex, m):
2020-06-09 13:04:38 +00:00
result = content[m.group(0)[0]]
2019-08-19 18:53:47 +00:00
proc proxifyVideo*(manifest: string; proxy: bool): string =
proc cb(m: RegexMatch; s: string): string =
result = "https://video.twimg.com/" & s[m.group(0)[0]]
if proxy: result = getVidUrl(result)
2020-01-22 12:04:35 +00:00
result = manifest.replace(manifestRegex, cb)
2019-08-19 18:53:47 +00:00
2019-06-20 14:16:20 +00:00
proc getUserpic*(userpic: string; style=""): string =
2020-01-22 12:04:35 +00:00
let pic = userpic.replace(userpicRegex, "$2")
pic.replace(extRegex, style & "$1")
2019-06-20 14:16:20 +00:00
proc getUserpic*(profile: Profile; style=""): string =
getUserPic(profile.userpic, style)
2019-12-09 23:39:12 +00:00
proc getVideoEmbed*(cfg: Config; id: int64): string =
2021-01-08 01:25:43 +00:00
&"{getUrlPrefix(cfg)}/i/videos/{id}"
2019-08-07 20:02:19 +00:00
2019-06-24 20:40:48 +00:00
proc pageTitle*(profile: Profile): string =
2019-07-31 00:15:43 +00:00
&"{profile.fullname} (@{profile.username})"
2019-06-25 02:52:38 +00:00
2020-03-29 07:15:05 +00:00
proc pageTitle*(tweet: Tweet): string =
&"{pageTitle(tweet.profile)}: \"{stripHtml(tweet.text)}\""
2019-08-07 20:02:19 +00:00
proc pageDesc*(profile: Profile): string =
2019-10-11 16:43:47 +00:00
if profile.bio.len > 0:
stripHtml(profile.bio)
else:
"The latest tweets from " & profile.fullname
2019-08-07 20:02:19 +00:00
proc getJoinDate*(profile: Profile): string =
profile.joinDate.format("'Joined' MMMM YYYY")
proc getJoinDateFull*(profile: Profile): string =
profile.joinDate.format("h:mm tt - d MMM YYYY")
2019-06-25 02:52:38 +00:00
proc getTime*(tweet: Tweet): string =
2019-09-15 09:14:03 +00:00
tweet.time.format("d/M/yyyy', 'HH:mm:ss")
proc getRfc822Time*(tweet: Tweet): string =
tweet.time.format("ddd', 'dd MMM yyyy HH:mm:ss 'GMT'")
2019-07-01 21:14:36 +00:00
proc getTweetTime*(tweet: Tweet): string =
tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
2020-06-01 00:25:39 +00:00
proc getShortTime*(tweet: Tweet): string =
2020-06-02 19:06:44 +00:00
let now = now()
2021-12-20 02:11:12 +00:00
let since = now - tweet.time
2020-06-02 19:06:44 +00:00
2021-12-20 02:11:12 +00:00
if now.year != tweet.time.year:
2020-06-01 00:25:39 +00:00
result = tweet.time.format("d MMM yyyy")
elif since.inDays >= 1:
result = tweet.time.format("MMM d")
elif since.inHours >= 1:
result = $since.inHours & "h"
elif since.inMinutes >= 1:
result = $since.inMinutes & "m"
elif since.inSeconds > 1:
result = $since.inSeconds & "s"
else:
result = "now"
proc getLink*(tweet: Tweet; focus=true): string =
if tweet.id == 0: return
2020-06-01 00:25:39 +00:00
var username = tweet.profile.username
if username.len == 0:
username = "i"
result = &"/{username}/status/{tweet.id}"
2019-10-22 07:17:58 +00:00
if focus: result &= "#m"
2019-10-08 13:07:10 +00:00
proc getTwitterLink*(path: string; params: Table[string, string]): string =
var
2019-10-08 13:07:10 +00:00
username = params.getOrDefault("name")
query = initQuery(params, username)
path = path
if "," in username:
query.fromUser = username.split(",")
path = "/search"
2019-10-08 13:07:10 +00:00
if "/search" notin path and query.fromUser.len < 2:
return $(twitter / path ? filterParams(params))
2019-10-08 13:07:10 +00:00
let p = {
2020-06-02 20:31:46 +00:00
"f": if query.kind == users: "user" else: "live",
2019-10-08 13:07:10 +00:00
"q": genQueryParam(query),
2020-06-01 00:25:39 +00:00
"src": "typed_query"
2019-10-08 13:07:10 +00:00
}
result = $(twitter / path ? p)
2019-10-08 13:07:10 +00:00
if username.len > 0:
result = result.replace("/" & username, "")
2019-12-21 04:44:58 +00:00
proc getLocation*(u: Profile | Tweet): (string, string) =
2020-03-09 00:03:24 +00:00
if "://" in u.location: return (u.location, "")
2019-12-21 04:44:58 +00:00
let loc = u.location.split(":")
let url = if loc.len > 1: "/search?q=place:" & loc[1] else: ""
(loc[0], url)
2020-04-14 21:56:31 +00:00
proc getSuspended*(username: string): string =
&"User \"{username}\" has been suspended"