nitter/src/formatters.nim

133 lines
4.2 KiB
Nim
Raw Normal View History

2019-12-06 04:37:38 +01:00
import strutils, strformat, times, uri, tables
import xmltree, htmlparser
2019-06-20 16:16:20 +02:00
import regex
2019-10-08 15:07:10 +02:00
import types, utils, query
2019-06-20 16:16:20 +02:00
2019-06-25 02:38:18 +02:00
from unicode import Rune, `$`
2019-06-20 16:16:20 +02:00
const
2020-01-19 08:49:20 +01:00
ytRegex = re"([A-z.]+\.)?youtu(be\.com|\.be)"
twRegex = re"(www\.|mobile\.)?twitter\.com"
igRegex = re"(www\.)?instagram.com"
2020-03-09 00:33:52 +01:00
cards = "cards.twitter.com/cards"
tco = "https://t.co"
2019-06-25 02:38:18 +02:00
nbsp = $Rune(0x000A0)
2020-01-22 13:04:35 +01:00
wwwRegex = re"https?://(www[0-9]?\.)?"
manifestRegex = re"(.+(.ts|.m3u8|.vmap))"
userpicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$"
extRegex = re"(\.[A-z]+)$"
tombstoneRegex = re"\n* *Learn more"
2019-06-25 02:38:18 +02:00
proc stripText*(text: string): string =
text.replace(nbsp, " ").strip()
2019-06-20 16:16:20 +02:00
proc stripHtml*(text: string): string =
2019-10-11 19:20:40 +02:00
var html = parseHtml(text)
for el in html.findAll("a"):
let link = el.attr("href")
if "http" in link:
el[0].text = link
html.innerText()
2019-06-20 16:16:20 +02:00
proc shortLink*(text: string; length=28): string =
2020-01-22 13:04:35 +01:00
result = text.replace(wwwRegex, "")
2019-06-20 16:16:20 +02:00
if result.len > length:
result = result[0 ..< length] & ""
proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
2019-08-15 18:45:56 +02:00
result = url
if prefs.replaceYouTube.len > 0:
result = result.replace(ytRegex, prefs.replaceYouTube)
2020-03-09 00:47:00 +01:00
if prefs.replaceYouTube in result:
result = result.replace("/c/", "/")
if prefs.replaceInstagram.len > 0:
result = result.replace(igRegex, prefs.replaceInstagram)
if prefs.replaceTwitter.len > 0:
result = result.replace(tco, "https://" & prefs.replaceTwitter & "/t.co")
2020-03-09 00:33:52 +01:00
result = result.replace(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter)
if absolute.len > 0:
result = result.replace("href=\"/", "href=\"https://" & absolute & "/")
2019-06-20 16:16:20 +02:00
2019-08-19 20:53:47 +02: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 13:04:35 +01:00
result = manifest.replace(manifestRegex, cb)
2019-08-19 20:53:47 +02:00
2019-06-20 16:16:20 +02:00
proc getUserpic*(userpic: string; style=""): string =
2020-01-22 13:04:35 +01:00
let pic = userpic.replace(userpicRegex, "$2")
pic.replace(extRegex, style & "$1")
2019-06-20 16:16:20 +02:00
proc getUserpic*(profile: Profile; style=""): string =
getUserPic(profile.userpic, style)
2019-12-10 00:39:12 +01:00
proc getVideoEmbed*(cfg: Config; id: int64): string =
2019-12-06 15:15:56 +01:00
&"https://{cfg.hostname}/i/videos/{id}"
2019-08-07 22:02:19 +02:00
2019-06-24 22:40:48 +02:00
proc pageTitle*(profile: Profile): string =
2019-07-31 02:15:43 +02:00
&"{profile.fullname} (@{profile.username})"
2019-06-25 04:52:38 +02:00
2020-03-29 09:15:05 +02:00
proc pageTitle*(tweet: Tweet): string =
&"{pageTitle(tweet.profile)}: \"{stripHtml(tweet.text)}\""
2019-08-07 22:02:19 +02:00
proc pageDesc*(profile: Profile): string =
2019-10-11 18:43:47 +02:00
if profile.bio.len > 0:
stripHtml(profile.bio)
else:
"The latest tweets from " & profile.fullname
2019-08-07 22:02:19 +02: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 04:52:38 +02:00
proc getTime*(tweet: Tweet): string =
2019-09-15 11:14:03 +02:00
tweet.time.format("d/M/yyyy', 'HH:mm:ss")
proc getRfc822Time*(tweet: Tweet): string =
tweet.time.format("ddd', 'd MMM yyyy HH:mm:ss 'GMT'")
2019-07-01 23:14:36 +02:00
proc getTweetTime*(tweet: Tweet): string =
tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
2019-10-22 09:17:58 +02:00
proc getLink*(tweet: Tweet | Quote; focus=true): string =
if tweet.id == 0: return
2019-10-22 09:17:58 +02:00
result = &"/{tweet.profile.username}/status/{tweet.id}"
if focus: result &= "#m"
proc getTombstone*(text: string): string =
2020-01-22 13:04:35 +01:00
text.replace(tombstoneRegex, "").stripText().strip(chars={' ', '\n'})
2019-10-08 15:07:10 +02:00
proc getTwitterLink*(path: string; params: Table[string, string]): string =
let
twitter = parseUri("https://twitter.com")
username = params.getOrDefault("name")
query = initQuery(params, username)
if "/search" notin path:
return $(twitter / path ? filterParams(params))
2019-10-08 15:07:10 +02:00
let p = {
"f": $query.kind,
"q": genQueryParam(query),
"src": "typd",
"max_position": params.getOrDefault("max_position", "0")
2019-10-08 15:07:10 +02:00
}
result = $(parseUri("https://twitter.com") / path ? p)
if username.len > 0:
result = result.replace("/" & username, "")
2019-12-21 05:44:58 +01:00
proc getLocation*(u: Profile | Tweet): (string, string) =
2020-03-09 01:03:24 +01:00
if "://" in u.location: return (u.location, "")
2019-12-21 05:44:58 +01:00
let loc = u.location.split(":")
let url = if loc.len > 1: "/search?q=place:" & loc[1] else: ""
(loc[0], url)
2020-04-14 23:56:31 +02:00
proc getSuspended*(username: string): string =
&"User \"{username}\" has been suspended"