nitter/src/routes/media.nim

87 lines
2.1 KiB
Nim
Raw Normal View History

2019-09-06 02:42:35 +02:00
import asyncfile, uri, strutils, httpclient, os
import jester, regex
import router_utils
2019-09-21 01:08:30 +02:00
import ".."/[types, formatters]
2019-09-06 02:42:35 +02:00
import ../views/general
export asyncfile, httpclient, os, strutils
export regex
proc createMediaRouter*(cfg: Config) =
router media:
get "/pic/@url":
2019-09-06 02:42:35 +02:00
cond "http" in @"url"
cond "twimg" in @"url"
let uri = parseUri(decodeUrl(@"url"))
cond isTwitterUrl($uri) == true
let path = uri.path.split("/")[2 .. ^1].join("/")
let filename = cfg.cacheDir / cleanFilename(path & uri.query)
2019-09-06 02:42:35 +02:00
if not existsDir(cfg.cacheDir):
createDir(cfg.cacheDir)
if not existsFile(filename):
let client = newAsyncHttpClient()
try:
await client.downloadFile($uri, filename)
client.close()
except:
discard
2019-09-06 02:42:35 +02:00
if not existsFile(filename):
halt Http404
2019-09-06 02:42:35 +02:00
let file = openAsync(filename)
let buf = await readAll(file)
file.close()
resp buf, mimetype(filename)
get "/gif/@url":
cond "http" in @"url"
cond "twimg" in @"url"
cond "mp4" in @"url" or "gif" in @"url"
let url = decodeUrl(@"url")
cond isTwitterUrl(url) == true
let client = newAsyncHttpClient()
var content: string
try:
content = await client.getContent(url)
client.close
except:
discard
if content.len == 0:
halt Http404
resp content, mimetype(url)
2019-09-06 02:42:35 +02:00
get "/video/@sig/@url":
cond "http" in @"url"
var url = decodeUrl(@"url")
let prefs = cookiePrefs()
if getHmac(url) != @"sig":
resp showError("Failed to verify signature", cfg)
2019-09-06 02:42:35 +02:00
let client = newAsyncHttpClient()
var content = await client.getContent(url)
if ".vmap" in url:
var m: RegexMatch
discard content.find(re"""url="(.+.m3u8)"""", m)
url = decodeUrl(content[m.group(0)[0]])
content = await client.getContent(url)
if ".m3u8" in url:
content = proxifyVideo(content, prefs.proxyVideos)
client.close()
resp content, mimetype(url)