Prevent 403 errors from outdated videos

This commit is contained in:
Zed 2019-12-22 01:45:07 +01:00
parent 7ef3a4faba
commit be15f1b001
3 changed files with 13 additions and 17 deletions

View file

@ -47,9 +47,8 @@ proc fetchJson*(url: Uri; headers: HttpHeaders): Future[JsonNode] {.async.} =
headers["accept"] = jsonAccept headers["accept"] = jsonAccept
newClient() newClient()
var resp = ""
try: try:
resp = await client.getContent($url) let resp = await client.getContent($url)
result = parseJson(resp) result = parseJson(resp)
except: except:
return nil return nil

View file

@ -46,16 +46,8 @@ proc createMediaRouter*(cfg: Config) =
let url = decodeUrl(@"url") let url = decodeUrl(@"url")
cond isTwitterUrl(url) == true cond isTwitterUrl(url) == true
let client = newAsyncHttpClient() let content = await safeFetch(url)
var content: string if content.len == 0: resp Http404
try:
content = await client.getContent(url)
client.close
except:
discard
if content.len == 0:
resp Http404
let filename = parseUri(url).path.split(".")[^1] let filename = parseUri(url).path.split(".")[^1]
resp content, settings.mimes.getMimetype(filename) resp content, settings.mimes.getMimetype(filename)
@ -68,18 +60,17 @@ proc createMediaRouter*(cfg: Config) =
if getHmac(url) != @"sig": if getHmac(url) != @"sig":
resp showError("Failed to verify signature", cfg) resp showError("Failed to verify signature", cfg)
let client = newAsyncHttpClient() var content = await safeFetch(url)
var content = await client.getContent(url) if content.len == 0: resp Http404
if ".vmap" in url: if ".vmap" in url:
var m: RegexMatch var m: RegexMatch
discard content.find(re"""url="(.+.m3u8)"""", m) discard content.find(re"""url="(.+.m3u8)"""", m)
url = decodeUrl(content[m.group(0)[0]]) url = decodeUrl(content[m.group(0)[0]])
content = await client.getContent(url) content = await safeFetch(url)
if ".m3u8" in url: if ".m3u8" in url:
content = proxifyVideo(content, prefs.proxyVideos) content = proxifyVideo(content, prefs.proxyVideos)
client.close()
let ext = parseUri(url).path.split(".")[^1] let ext = parseUri(url).path.split(".")[^1]
resp content, settings.mimes.getMimetype(ext) resp content, settings.mimes.getMimetype(ext)

View file

@ -1,4 +1,4 @@
import strutils, sequtils import strutils, sequtils, asyncdispatch, httpclient
import ../utils, ../prefs import ../utils, ../prefs
export utils, prefs export utils, prefs
@ -13,3 +13,9 @@ template refPath*(): untyped {.dirty.} =
proc getNames*(name: string): seq[string] = proc getNames*(name: string): seq[string] =
name.strip(chars={'/'}).split(",").filterIt(it.len > 0) name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
proc safeFetch*(url: string): Future[string] {.async.} =
let client = newAsyncHttpClient()
try: result = await client.getContent(url)
except: discard
client.close()