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