nitter/src/utils.nim

42 lines
1 KiB
Nim
Raw Normal View History

import strutils, strformat, sequtils, uri, tables
import nimcrypto, regex
2019-06-20 14:16:20 +00:00
const
key = "supersecretkey"
twitterDomains = @[
"twitter.com",
"twimg.com",
"abs.twimg.com",
"pbs.twimg.com",
"video.twimg.com"
]
2019-10-21 05:49:43 +00:00
badExts = @["1500x500", "jpgn", "jpg:", "jpg_"]
2019-06-20 14:16:20 +00:00
proc getHmac*(data: string): string =
($hmac(sha256, key, data))[0 .. 12]
proc getVidUrl*(link: string): string =
2019-06-20 14:16:20 +00:00
let
sig = getHmac(link)
url = encodeUrl(link)
&"/video/{sig}/{url}"
proc getGifUrl*(link: string): string =
&"/gif/{encodeUrl(link)}"
proc getPicUrl*(link: string): string =
&"/pic/{encodeUrl(link)}"
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 05:49:43 +00:00
if badExts.anyIt(it in result):
2019-10-21 05:22:24 +00:00
result &= ".jpg"
proc filterParams*(params: Table): seq[(string, string)] =
let filter = ["name", "id", "list", "referer"]
2019-09-19 23:52:08 +00:00
toSeq(params.pairs()).filterIt(it[0] notin filter and it[1].len > 0)
proc isTwitterUrl*(url: string): bool =
parseUri(url).hostname in twitterDomains