Add user agent for media requests, improve error

This commit is contained in:
Zed 2020-06-02 20:27:43 +02:00
parent 9b34123573
commit 7acea6b9e0
2 changed files with 18 additions and 13 deletions

View File

@ -3,13 +3,14 @@ import asyncfile, uri, strutils, httpclient, os, mimetypes
import jester, regex import jester, regex
import router_utils import router_utils
import ".."/[types, formatters] import ".."/[types, formatters, agents]
import ../views/general import ../views/general
export asyncfile, httpclient, os, strutils export asyncfile, httpclient, os, strutils
export regex export regex
const m3u8Regex* = re"""url="(.+.m3u8)"""" const m3u8Regex* = re"""url="(.+.m3u8)""""
let mediaAgent* = getAgent()
proc createMediaRouter*(cfg: Config) = proc createMediaRouter*(cfg: Config) =
router media: router media:
@ -33,19 +34,23 @@ proc createMediaRouter*(cfg: Config) =
createDir(cfg.cacheDir) createDir(cfg.cacheDir)
if not existsFile(filename): if not existsFile(filename):
let client = newAsyncHttpClient() let client = newAsyncHttpClient(userAgent=mediaAgent)
var failed = false
try: try:
await client.downloadFile($uri, filename) await client.downloadFile($uri, filename)
except HttpRequestError: except HttpRequestError:
client.safeClose() failed = true
removeFile(filename) removeFile(filename)
resp Http404 except OSError as e:
except OSError: echo "Disk full, or network error: ", e.msg
echo "Disk full" failed = true
resp Http404
finally: finally:
client.safeClose() client.safeClose()
if failed:
resp Http404
sendFile(filename) sendFile(filename)
get "/gif/@url": get "/gif/@url":
@ -56,7 +61,7 @@ proc createMediaRouter*(cfg: Config) =
let url = decodeUrl(@"url") let url = decodeUrl(@"url")
cond isTwitterUrl(url) == true cond isTwitterUrl(url) == true
let content = await safeFetch(url) let content = await safeFetch(url, mediaAgent)
if content.len == 0: resp Http404 if content.len == 0: resp Http404
let filename = parseUri(url).path.split(".")[^1] let filename = parseUri(url).path.split(".")[^1]
@ -70,14 +75,14 @@ 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)
var content = await safeFetch(url) var content = await safeFetch(url, mediaAgent)
if content.len == 0: resp Http404 if content.len == 0: resp Http404
if ".vmap" in url: if ".vmap" in url:
var m: RegexMatch var m: RegexMatch
discard content.find(m3u8Regex, m) discard content.find(m3u8Regex, m)
url = decodeUrl(content[m.group(0)[0]]) url = decodeUrl(content[m.group(0)[0]])
content = await safeFetch(url) content = await safeFetch(url, mediaAgent)
if ".m3u8" in url: if ".m3u8" in url:
content = proxifyVideo(content, prefs.proxyVideos) content = proxifyVideo(content, prefs.proxyVideos)

View File

@ -1,6 +1,6 @@
import strutils, sequtils, asyncdispatch, httpclient, uri import strutils, sequtils, asyncdispatch, httpclient, uri
from jester import Request from jester import Request
import ../utils, ../prefs import ".."/[utils, prefs]
export utils, prefs export utils, prefs
template savePref*(pref, value: string; req: Request; expire=false) = template savePref*(pref, value: string; req: Request; expire=false) =
@ -33,8 +33,8 @@ proc safeClose*(client: AsyncHttpClient) =
try: client.close() try: client.close()
except: discard except: discard
proc safeFetch*(url: string): Future[string] {.async.} = proc safeFetch*(url, agent: string): Future[string] {.async.} =
let client = newAsyncHttpClient() let client = newAsyncHttpClient(userAgent=agent)
try: result = await client.getContent(url) try: result = await client.getContent(url)
except: discard except: discard
finally: client.safeClose() finally: client.safeClose()