2019-10-08 13:07:10 +00:00
|
|
|
import strutils, strformat, sequtils, htmlgen, xmltree, times, uri, tables
|
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
|
|
|
|
2019-06-25 00:38:18 +00:00
|
|
|
from unicode import Rune, `$`
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
const
|
2019-06-27 20:30:00 +00:00
|
|
|
urlRegex = re"((https?|ftp)://(-\.)?([^\s/?\.#]+\.?)+([/\?][^\s\)]*)?)"
|
2019-06-20 14:16:20 +00:00
|
|
|
emailRegex = re"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
|
2019-09-19 23:46:20 +00:00
|
|
|
usernameRegex = re"(^|[^A-z0-9_?\/])@([A-z0-9_]+)"
|
2019-06-20 14:16:20 +00:00
|
|
|
picRegex = re"pic.twitter.com/[^ ]+"
|
|
|
|
ellipsisRegex = re" ?…"
|
2019-09-26 22:53:11 +00:00
|
|
|
hashtagRegex = re"([^\S]|^)([#$]\w+)"
|
2019-09-14 06:56:46 +00:00
|
|
|
ytRegex = re"(www.|m.)?youtu(be.com|.be)"
|
|
|
|
twRegex = re"(www.|mobile.)?twitter.com"
|
2019-06-25 00:38:18 +00:00
|
|
|
nbsp = $Rune(0x000A0)
|
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
const hostname {.strdefine.} = "nitter.net"
|
|
|
|
|
2019-06-25 00:38:18 +00:00
|
|
|
proc stripText*(text: string): string =
|
|
|
|
text.replace(nbsp, " ").strip()
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
proc shortLink*(text: string; length=28): string =
|
|
|
|
result = text.replace(re"https?://(www.)?", "")
|
|
|
|
if result.len > length:
|
|
|
|
result = result[0 ..< length] & "…"
|
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
proc toLink*(url, text: string): string =
|
|
|
|
a(text, href=url)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
proc reUrlToShortLink*(m: RegexMatch; s: string): string =
|
2019-06-20 14:16:20 +00:00
|
|
|
let url = s[m.group(0)[0]]
|
2019-06-24 06:07:36 +00:00
|
|
|
toLink(url, shortLink(url))
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
proc reUrlToLink*(m: RegexMatch; s: string): string =
|
|
|
|
let url = s[m.group(0)[0]]
|
|
|
|
toLink(url, url.replace(re"https?://(www.)?", ""))
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
proc reEmailToLink*(m: RegexMatch; s: string): string =
|
|
|
|
let url = s[m.group(0)[0]]
|
|
|
|
toLink("mailto://" & url, url)
|
|
|
|
|
2019-09-19 03:22:45 +00:00
|
|
|
proc reHashtagToLink*(m: RegexMatch; s: string): string =
|
|
|
|
result = if m.group(0).len > 0: s[m.group(0)[0]] else: ""
|
|
|
|
let hash = s[m.group(1)[0]]
|
2019-10-08 11:16:48 +00:00
|
|
|
let link = toLink("/search?q=" & encodeUrl(hash), hash)
|
2019-09-19 03:22:45 +00:00
|
|
|
if hash.any(isAlphaAscii):
|
|
|
|
result &= link
|
|
|
|
else:
|
|
|
|
result &= hash
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
proc reUsernameToLink*(m: RegexMatch; s: string): string =
|
2019-06-25 01:48:57 +00:00
|
|
|
var username = ""
|
|
|
|
var pretext = ""
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-06-25 01:48:57 +00:00
|
|
|
let pre = m.group(0)
|
|
|
|
let match = m.group(1)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
|
|
|
username = s[match[0]]
|
|
|
|
|
|
|
|
if pre.len > 0:
|
|
|
|
pretext = s[pre[0]]
|
|
|
|
|
|
|
|
pretext & toLink("/" & username, "@" & username)
|
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
proc reUsernameToFullLink*(m: RegexMatch; s: string): string =
|
|
|
|
result = reUsernameToLink(m, s)
|
|
|
|
result = result.replace("href=\"/", &"href=\"https://{hostname}/")
|
2019-08-15 13:51:20 +00:00
|
|
|
|
|
|
|
proc replaceUrl*(url: string; prefs: Prefs): string =
|
2019-08-15 16:45:56 +00:00
|
|
|
result = url
|
2019-08-15 13:51:20 +00:00
|
|
|
if prefs.replaceYouTube.len > 0:
|
2019-08-15 17:19:21 +00:00
|
|
|
result = result.replace(ytRegex, prefs.replaceYouTube)
|
2019-08-15 13:51:20 +00:00
|
|
|
if prefs.replaceTwitter.len > 0:
|
2019-08-15 17:19:21 +00:00
|
|
|
result = result.replace(twRegex, prefs.replaceTwitter)
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
proc linkifyText*(text: string; prefs: Prefs; rss=false): string =
|
|
|
|
result = xmltree.escape(stripText(text))
|
2019-09-19 23:46:20 +00:00
|
|
|
result = result.replace(ellipsisRegex, " ")
|
2019-09-15 07:57:45 +00:00
|
|
|
result = result.replace(emailRegex, reEmailToLink)
|
|
|
|
if rss:
|
|
|
|
result = result.replace(urlRegex, reUrlToLink)
|
|
|
|
result = result.replace(usernameRegex, reUsernameToFullLink)
|
|
|
|
else:
|
|
|
|
result = result.replace(urlRegex, reUrlToShortLink)
|
|
|
|
result = result.replace(usernameRegex, reUsernameToLink)
|
2019-09-19 23:46:20 +00:00
|
|
|
result = result.replace(hashtagRegex, reHashtagToLink)
|
2019-09-15 07:57:45 +00:00
|
|
|
result = result.replace(re"([^\s\(\n%])<a", "$1 <a")
|
|
|
|
result = result.replace(re"</a>\s+([;.,!\)'%]|')", "</a>$1")
|
|
|
|
result = result.replace(re"^\. <a", ".<a")
|
|
|
|
result = result.replaceUrl(prefs)
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
proc stripTwitterUrls*(text: string): string =
|
|
|
|
result = text
|
|
|
|
result = result.replace(picRegex, "")
|
|
|
|
result = result.replace(ellipsisRegex, "")
|
|
|
|
|
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]]
|
2019-09-13 10:27:04 +00:00
|
|
|
if proxy: result = getVidUrl(result)
|
2019-08-19 18:53:47 +00:00
|
|
|
result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb)
|
|
|
|
|
2019-06-20 14:16:20 +00:00
|
|
|
proc getUserpic*(userpic: string; style=""): string =
|
2019-08-11 19:26:55 +00:00
|
|
|
let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
|
2019-06-20 14:16:20 +00:00
|
|
|
pic.replace(re"(\.[A-z]+)$", style & "$1")
|
|
|
|
|
|
|
|
proc getUserpic*(profile: Profile; style=""): string =
|
|
|
|
getUserPic(profile.userpic, style)
|
|
|
|
|
2019-08-07 20:27:24 +00:00
|
|
|
proc getVideoEmbed*(id: string): string =
|
|
|
|
&"https://twitter.com/i/videos/{id}?embed_source=facebook"
|
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
|
|
|
|
2019-08-07 20:02:19 +00:00
|
|
|
proc pageDesc*(profile: Profile): string =
|
|
|
|
"The latest tweets from " & profile.fullname
|
|
|
|
|
2019-08-11 19:26:55 +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', 'd MMM yyyy HH:mm:ss 'GMT'")
|
2019-07-01 21:14:36 +00:00
|
|
|
|
2019-10-08 11:28:57 +00:00
|
|
|
proc getTweetTime*(tweet: Tweet): string =
|
|
|
|
tweet.time.format("h:mm tt' · 'MMM d', 'YYYY")
|
|
|
|
|
2019-07-01 21:14:36 +00:00
|
|
|
proc getLink*(tweet: Tweet | Quote): string =
|
2019-09-24 14:01:09 +00:00
|
|
|
if tweet.id.len == 0: return
|
2019-07-01 21:55:19 +00:00
|
|
|
&"/{tweet.profile.username}/status/{tweet.id}"
|
2019-09-08 12:34:26 +00:00
|
|
|
|
|
|
|
proc getTombstone*(text: string): string =
|
|
|
|
text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'})
|
2019-10-08 13:07:10 +00:00
|
|
|
|
|
|
|
proc getTwitterLink*(path: string; params: Table[string, string]): string =
|
|
|
|
let
|
|
|
|
twitter = parseUri("https://twitter.com")
|
|
|
|
username = params.getOrDefault("name")
|
|
|
|
query = initQuery(params, username)
|
|
|
|
|
|
|
|
var after = params.getOrDefault("after", "0")
|
|
|
|
if query.kind notin {userList, users} and "/members" notin path:
|
|
|
|
after = after.genPos()
|
|
|
|
|
|
|
|
var paramList = filterParams(params).mapIt(
|
|
|
|
if it[0] == "after": ("max_position", after) else: it)
|
|
|
|
|
|
|
|
if "/search" notin path:
|
|
|
|
return $(twitter / path ? paramList)
|
|
|
|
|
|
|
|
let p = {
|
|
|
|
"f": $query.kind,
|
|
|
|
"q": genQueryParam(query),
|
|
|
|
"src": "typd",
|
|
|
|
"max_position": after
|
|
|
|
}
|
|
|
|
|
|
|
|
result = $(parseUri("https://twitter.com") / path ? p)
|
|
|
|
if username.len > 0:
|
|
|
|
result = result.replace("/" & username, "")
|