mirror of
https://github.com/zedeus/nitter.git
synced 2024-12-23 23:35:37 +00:00
Turn regex patterns into consts
This commit is contained in:
parent
bddb6df567
commit
b03faccd45
|
@ -13,6 +13,12 @@ const
|
||||||
tco = "https://t.co"
|
tco = "https://t.co"
|
||||||
nbsp = $Rune(0x000A0)
|
nbsp = $Rune(0x000A0)
|
||||||
|
|
||||||
|
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"
|
||||||
|
|
||||||
proc stripText*(text: string): string =
|
proc stripText*(text: string): string =
|
||||||
text.replace(nbsp, " ").strip()
|
text.replace(nbsp, " ").strip()
|
||||||
|
|
||||||
|
@ -25,7 +31,7 @@ proc stripHtml*(text: string): string =
|
||||||
html.innerText()
|
html.innerText()
|
||||||
|
|
||||||
proc shortLink*(text: string; length=28): string =
|
proc shortLink*(text: string; length=28): string =
|
||||||
result = text.replace(re"https?://(www[0-9]?\.)?", "")
|
result = text.replace(wwwRegex, "")
|
||||||
if result.len > length:
|
if result.len > length:
|
||||||
result = result[0 ..< length] & "…"
|
result = result[0 ..< length] & "…"
|
||||||
|
|
||||||
|
@ -44,11 +50,11 @@ proc proxifyVideo*(manifest: string; proxy: bool): string =
|
||||||
proc cb(m: RegexMatch; s: string): string =
|
proc cb(m: RegexMatch; s: string): string =
|
||||||
result = "https://video.twimg.com" & s[m.group(0)[0]]
|
result = "https://video.twimg.com" & s[m.group(0)[0]]
|
||||||
if proxy: result = getVidUrl(result)
|
if proxy: result = getVidUrl(result)
|
||||||
result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb)
|
result = manifest.replace(manifestRegex, cb)
|
||||||
|
|
||||||
proc getUserpic*(userpic: string; style=""): string =
|
proc getUserpic*(userpic: string; style=""): string =
|
||||||
let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
|
let pic = userpic.replace(userpicRegex, "$2")
|
||||||
pic.replace(re"(\.[A-z]+)$", style & "$1")
|
pic.replace(extRegex, style & "$1")
|
||||||
|
|
||||||
proc getUserpic*(profile: Profile; style=""): string =
|
proc getUserpic*(profile: Profile; style=""): string =
|
||||||
getUserPic(profile.userpic, style)
|
getUserPic(profile.userpic, style)
|
||||||
|
@ -86,7 +92,7 @@ proc getLink*(tweet: Tweet | Quote; focus=true): string =
|
||||||
if focus: result &= "#m"
|
if focus: result &= "#m"
|
||||||
|
|
||||||
proc getTombstone*(text: string): string =
|
proc getTombstone*(text: string): string =
|
||||||
text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'})
|
text.replace(tombstoneRegex, "").stripText().strip(chars={' ', '\n'})
|
||||||
|
|
||||||
proc getTwitterLink*(path: string; params: Table[string, string]): string =
|
proc getTwitterLink*(path: string; params: Table[string, string]): string =
|
||||||
let
|
let
|
||||||
|
|
|
@ -9,6 +9,7 @@ from htmlgen import a
|
||||||
const
|
const
|
||||||
thumbRegex = re".+:url\('([^']+)'\)"
|
thumbRegex = re".+:url\('([^']+)'\)"
|
||||||
gifRegex = re".+thumb/([^\.']+)\.[jpng].*"
|
gifRegex = re".+thumb/([^\.']+)\.[jpng].*"
|
||||||
|
reColor = re"a:active \{\n +color: (#[A-Z0-9]+)"
|
||||||
|
|
||||||
proc selectAll*(node: XmlNode; selector: string): seq[XmlNode] =
|
proc selectAll*(node: XmlNode; selector: string): seq[XmlNode] =
|
||||||
if node == nil: return
|
if node == nil: return
|
||||||
|
@ -134,7 +135,7 @@ proc getTimelineBanner*(node: XmlNode): string =
|
||||||
|
|
||||||
let style = node.select("style").innerText()
|
let style = node.select("style").innerText()
|
||||||
var m: RegexMatch
|
var m: RegexMatch
|
||||||
if style.find(re"a:active \{\n +color: (#[A-Z0-9]+)", m):
|
if style.find(reColor, m):
|
||||||
return style[m.group(0)[0]]
|
return style[m.group(0)[0]]
|
||||||
|
|
||||||
proc getMediaCount*(node: XmlNode): string =
|
proc getMediaCount*(node: XmlNode): string =
|
||||||
|
|
|
@ -9,6 +9,8 @@ import ../views/general
|
||||||
export asyncfile, httpclient, os, strutils
|
export asyncfile, httpclient, os, strutils
|
||||||
export regex
|
export regex
|
||||||
|
|
||||||
|
const m3u8Regex* = re"""url="(.+.m3u8)""""
|
||||||
|
|
||||||
proc createMediaRouter*(cfg: Config) =
|
proc createMediaRouter*(cfg: Config) =
|
||||||
router media:
|
router media:
|
||||||
get "/pic/@url":
|
get "/pic/@url":
|
||||||
|
@ -67,7 +69,7 @@ proc createMediaRouter*(cfg: Config) =
|
||||||
|
|
||||||
if ".vmap" in url:
|
if ".vmap" in url:
|
||||||
var m: RegexMatch
|
var m: RegexMatch
|
||||||
discard content.find(re"""url="(.+.m3u8)"""", m)
|
discard content.find(m3u8Regex, m)
|
||||||
url = decodeUrl(content[m.group(0)[0]])
|
url = decodeUrl(content[m.group(0)[0]])
|
||||||
content = await safeFetch(url)
|
content = await safeFetch(url)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue