2020-06-09 13:04:38 +00:00
|
|
|
import strutils, strformat, sequtils, uri, tables, base64
|
2019-06-23 00:46:46 +00:00
|
|
|
import nimcrypto, regex
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2020-06-09 13:04:38 +00:00
|
|
|
var
|
2020-06-22 01:50:16 +00:00
|
|
|
hmacKey {.threadvar.}: string
|
2020-06-09 13:04:38 +00:00
|
|
|
base64Media = false
|
2019-10-23 22:17:38 +00:00
|
|
|
|
2019-09-13 10:27:04 +00:00
|
|
|
const
|
2020-06-07 05:55:57 +00:00
|
|
|
https* = "https://"
|
|
|
|
twimg* = "pbs.twimg.com/"
|
2020-01-19 08:01:11 +00:00
|
|
|
badJpgExts = @["1500x500", "jpgn", "jpg:", "jpg_", "_jpg"]
|
|
|
|
badPngExts = @["pngn", "png:", "png_", "_png"]
|
2019-09-13 10:27:04 +00:00
|
|
|
twitterDomains = @[
|
|
|
|
"twitter.com",
|
2020-06-01 00:25:39 +00:00
|
|
|
"pic.twitter.com",
|
2019-09-13 10:27:04 +00:00
|
|
|
"twimg.com",
|
|
|
|
"abs.twimg.com",
|
|
|
|
"pbs.twimg.com",
|
|
|
|
"video.twimg.com"
|
|
|
|
]
|
2019-10-23 22:17:38 +00:00
|
|
|
|
|
|
|
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 =
|
2019-10-23 22:17:38 +00:00
|
|
|
($hmac(sha256, hmacKey, data))[0 .. 12]
|
2019-06-20 14:16:20 +00:00
|
|
|
|
2019-09-13 10:27:04 +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)}"
|
2019-09-13 10:27:04 +00:00
|
|
|
|
|
|
|
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)}"
|
2019-06-23 00:46:46 +00:00
|
|
|
|
|
|
|
proc cleanFilename*(filename: string): string =
|
|
|
|
const reg = re"[^A-Za-z0-9._-]"
|
2019-10-21 05:22:24 +00:00
|
|
|
result = filename.replace(reg, "_")
|
2019-10-21 06:31:02 +00:00
|
|
|
if badJpgExts.anyIt(it in result):
|
2019-10-21 05:22:24 +00:00
|
|
|
result &= ".jpg"
|
2019-10-21 06:31:02 +00:00
|
|
|
elif badPngExts.anyIt(it in result):
|
|
|
|
result &= ".png"
|
2019-09-05 20:40:36 +00:00
|
|
|
|
|
|
|
proc filterParams*(params: Table): seq[(string, string)] =
|
2020-06-22 01:50:16 +00:00
|
|
|
const filter = ["name", "id", "list", "referer", "scroll"]
|
2019-09-19 23:52:08 +00:00
|
|
|
toSeq(params.pairs()).filterIt(it[0] notin filter and it[1].len > 0)
|
2019-09-13 10:27:04 +00:00
|
|
|
|
2020-06-07 05:55:57 +00:00
|
|
|
proc isTwitterUrl*(uri: Uri): bool =
|
|
|
|
uri.hostname in twitterDomains
|
|
|
|
|
2019-09-13 10:27:04 +00:00
|
|
|
proc isTwitterUrl*(url: string): bool =
|
|
|
|
parseUri(url).hostname in twitterDomains
|