nitter/src/utils.nim

55 lines
1.2 KiB
Nim
Raw Normal View History

2021-12-27 01:37:38 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
2022-01-02 10:21:03 +00:00
import strutils, strformat, uri, tables, base64
import nimcrypto
2019-06-20 14:16:20 +00:00
2020-06-09 13:04:38 +00:00
var
2022-01-02 10:21:03 +00:00
hmacKey: string
2020-06-09 13:04:38 +00:00
base64Media = false
const
https* = "https://"
twimg* = "pbs.twimg.com/"
2022-01-02 10:21:03 +00:00
nitterParams = ["name", "tab", "id", "list", "referer", "scroll"]
twitterDomains = @[
"twitter.com",
2020-06-01 00:25:39 +00:00
"pic.twitter.com",
"twimg.com",
"abs.twimg.com",
"pbs.twimg.com",
"video.twimg.com"
]
proc setHmacKey*(key: string) =
hmacKey = key
2019-06-20 14:16:20 +00:00
2020-06-09 13:04:38 +00:00
proc setProxyEncoding*(state: bool) =
base64Media = state
2019-06-20 14:16:20 +00:00
proc getHmac*(data: string): string =
($hmac(sha256, hmacKey, data))[0 .. 12]
2019-06-20 14:16:20 +00:00
proc getVidUrl*(link: string): string =
2020-06-01 19:53:21 +00:00
if link.len == 0: return
2020-06-09 13:04:38 +00:00
let sig = getHmac(link)
if base64Media:
&"/video/enc/{sig}/{encode(link, safe=true)}"
else:
&"/video/{sig}/{encodeUrl(link)}"
proc getPicUrl*(link: string): string =
2020-06-09 13:04:38 +00:00
if base64Media:
&"/pic/enc/{encode(link, safe=true)}"
else:
&"/pic/{encodeUrl(link)}"
proc filterParams*(params: Table): seq[(string, string)] =
for p in params.pairs():
2022-01-02 06:00:44 +00:00
if p[1].len > 0 and p[0] notin nitterParams:
result.add p
proc isTwitterUrl*(uri: Uri): bool =
uri.hostname in twitterDomains
proc isTwitterUrl*(url: string): bool =
parseUri(url).hostname in twitterDomains