mirror of
https://github.com/zedeus/nitter.git
synced 2024-12-12 11:06:30 +00:00
Add user agent for media requests, improve error
This commit is contained in:
parent
9b34123573
commit
7acea6b9e0
2 changed files with 18 additions and 13 deletions
|
@ -3,13 +3,14 @@ import asyncfile, uri, strutils, httpclient, os, mimetypes
|
|||
import jester, regex
|
||||
|
||||
import router_utils
|
||||
import ".."/[types, formatters]
|
||||
import ".."/[types, formatters, agents]
|
||||
import ../views/general
|
||||
|
||||
export asyncfile, httpclient, os, strutils
|
||||
export regex
|
||||
|
||||
const m3u8Regex* = re"""url="(.+.m3u8)""""
|
||||
let mediaAgent* = getAgent()
|
||||
|
||||
proc createMediaRouter*(cfg: Config) =
|
||||
router media:
|
||||
|
@ -33,19 +34,23 @@ proc createMediaRouter*(cfg: Config) =
|
|||
createDir(cfg.cacheDir)
|
||||
|
||||
if not existsFile(filename):
|
||||
let client = newAsyncHttpClient()
|
||||
let client = newAsyncHttpClient(userAgent=mediaAgent)
|
||||
var failed = false
|
||||
|
||||
try:
|
||||
await client.downloadFile($uri, filename)
|
||||
except HttpRequestError:
|
||||
client.safeClose()
|
||||
failed = true
|
||||
removeFile(filename)
|
||||
resp Http404
|
||||
except OSError:
|
||||
echo "Disk full"
|
||||
resp Http404
|
||||
except OSError as e:
|
||||
echo "Disk full, or network error: ", e.msg
|
||||
failed = true
|
||||
finally:
|
||||
client.safeClose()
|
||||
|
||||
if failed:
|
||||
resp Http404
|
||||
|
||||
sendFile(filename)
|
||||
|
||||
get "/gif/@url":
|
||||
|
@ -56,7 +61,7 @@ proc createMediaRouter*(cfg: Config) =
|
|||
let url = decodeUrl(@"url")
|
||||
cond isTwitterUrl(url) == true
|
||||
|
||||
let content = await safeFetch(url)
|
||||
let content = await safeFetch(url, mediaAgent)
|
||||
if content.len == 0: resp Http404
|
||||
|
||||
let filename = parseUri(url).path.split(".")[^1]
|
||||
|
@ -70,14 +75,14 @@ proc createMediaRouter*(cfg: Config) =
|
|||
if getHmac(url) != @"sig":
|
||||
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 ".vmap" in url:
|
||||
var m: RegexMatch
|
||||
discard content.find(m3u8Regex, m)
|
||||
url = decodeUrl(content[m.group(0)[0]])
|
||||
content = await safeFetch(url)
|
||||
content = await safeFetch(url, mediaAgent)
|
||||
|
||||
if ".m3u8" in url:
|
||||
content = proxifyVideo(content, prefs.proxyVideos)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import strutils, sequtils, asyncdispatch, httpclient, uri
|
||||
from jester import Request
|
||||
import ../utils, ../prefs
|
||||
import ".."/[utils, prefs]
|
||||
export utils, prefs
|
||||
|
||||
template savePref*(pref, value: string; req: Request; expire=false) =
|
||||
|
@ -33,8 +33,8 @@ proc safeClose*(client: AsyncHttpClient) =
|
|||
try: client.close()
|
||||
except: discard
|
||||
|
||||
proc safeFetch*(url: string): Future[string] {.async.} =
|
||||
let client = newAsyncHttpClient()
|
||||
proc safeFetch*(url, agent: string): Future[string] {.async.} =
|
||||
let client = newAsyncHttpClient(userAgent=agent)
|
||||
try: result = await client.getContent(url)
|
||||
except: discard
|
||||
finally: client.safeClose()
|
||||
|
|
Loading…
Reference in a new issue