From ce6dace1d73b60f4bb3743312856e8ba690307de Mon Sep 17 00:00:00 2001 From: Zed Date: Mon, 19 Aug 2019 20:53:47 +0200 Subject: [PATCH] Add video proxy support --- src/formatters.nim | 8 +++++++- src/nitter.nim | 13 ++++++++----- src/prefs_impl.nim | 4 ++++ src/types.nim | 1 + 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/formatters.nim b/src/formatters.nim index 6c68425..1105de5 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -1,7 +1,7 @@ import strutils, strformat, htmlgen, xmltree, times import regex -import types +import types, utils from unicode import Rune, `$` @@ -74,6 +74,12 @@ proc stripTwitterUrls*(text: string): string = result = result.replace(picRegex, "") result = result.replace(ellipsisRegex, "") +proc proxifyVideo*(manifest: string; proxy: bool): string = + proc cb(m: RegexMatch; s: string): string = + result = "https://video.twimg.com" & s[m.group(0)[0]] + if proxy: result = result.getSigUrl("video") + result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb) + proc getUserpic*(userpic: string; style=""): string = let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2") pic.replace(re"(\.[A-z]+)$", style & "$1") diff --git a/src/nitter.nim b/src/nitter.nim index 91831b2..8059f1d 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -182,16 +182,19 @@ routes: get "/video/@sig/@url": cond "http" in @"url" - cond "video.twimg" in @"url" - let url = decodeUrl(@"url") + var url = decodeUrl(@"url") + let prefs = cookiePrefs() if getHmac(url) != @"sig": resp showError("Failed to verify signature", cfg.title) let client = newAsyncHttpClient() - let video = await client.getContent(url) - client.close() + var content = await client.getContent(url) - resp video, mimetype(url) + if ".m3u8" in url: + content = proxifyVideo(content, prefs.proxyVideos) + + client.close() + resp content, mimetype(url) runForever() diff --git a/src/prefs_impl.nim b/src/prefs_impl.nim index eda6cb2..86eb423 100644 --- a/src/prefs_impl.nim +++ b/src/prefs_impl.nim @@ -40,6 +40,10 @@ const prefList*: Table[string, seq[Pref]] = { label: "Enable hls video streaming (requires JavaScript)", defaultState: false), + Pref(kind: checkbox, name: "proxyVideos", + label: "Proxy video streaming through the server (might be slow)", + defaultState: false), + Pref(kind: checkbox, name: "muteVideos", label: "Mute videos by default", defaultState: false), diff --git a/src/types.nim b/src/types.nim index 611b361..bcb9a0a 100644 --- a/src/types.nim +++ b/src/types.nim @@ -53,6 +53,7 @@ db("cache.db", "", "", ""): Prefs* = object hlsPlayback*: bool mp4Playback*: bool + proxyVideos*: bool muteVideos*: bool autoplayGifs*: bool hideTweetStats*: bool