add download support

This commit is contained in:
Roy Olav Purser 2022-02-13 14:15:33 +01:00
parent b667177531
commit 4267f6a7ca
Signed by: roypur
GPG Key ID: E14D26A036F21656
3 changed files with 39 additions and 13 deletions

View File

@ -45,11 +45,13 @@ proxy_server = os.environ.get("PROXY_SERVER")
@dataclasses.dataclass @dataclasses.dataclass
class LinkWithType: class LinkWithType:
upstream: str upstream: str
download: bool = False
ctype: None | str = None ctype: None | str = None
class ProxyCreateLink(pydantic.BaseModel): class ProxyCreateLink(pydantic.BaseModel):
upstream: None | pydantic.HttpUrl upstream: None | pydantic.HttpUrl
download: None | pydantic.StrictBool
ctype: None | pydantic.StrictStr ctype: None | pydantic.StrictStr
region: None | pydantic.StrictStr region: None | pydantic.StrictStr
@ -91,6 +93,7 @@ class ProxyElem:
{ {
"upstream": url.upstream, "upstream": url.upstream,
"ctype": url.ctype, "ctype": url.ctype,
"download": url.download,
"region": self.region, "region": self.region,
} }
) )

View File

@ -2,13 +2,14 @@
import json import json
import sys import sys
import base64 import base64
import dataclasses
from typing import Optional, cast, Any
import logging import logging
import asyncio import asyncio
import tornado.web import tornado.web
import tornado.routing import tornado.routing
import config import config
import stream_providers import stream_providers
from typing import Optional, cast, Any
logging.basicConfig( logging.basicConfig(
format="[%(filename)s:%(lineno)d] %(message)s", format="[%(filename)s:%(lineno)d] %(message)s",
@ -85,6 +86,14 @@ class UpstreamHandler:
future.cancel() future.cancel()
@dataclasses.dataclass
class VideoInfo:
upstream: None | str = None
download: None | str = None
poster: None | str = None
ctype: None | str = None
class NoDataError(Exception): class NoDataError(Exception):
pass pass
@ -127,10 +136,11 @@ class MainHandler(tornado.web.RequestHandler):
if provider_data is None: if provider_data is None:
raise NoDataError() raise NoDataError()
video_info = {} video_info = VideoInfo()
video_info.ctype = provider_data.ctype()
if handler.direct: if handler.direct:
video_info["upstream"] = provider_data.upstream() video_info.upstream = provider_data.upstream()
video_info["poster"] = provider_data.thumbnail() video_info.poster = provider_data.thumbnail()
else: else:
proxied = await handler.proxy.proxy_url( proxied = await handler.proxy.proxy_url(
[ [
@ -138,16 +148,30 @@ class MainHandler(tornado.web.RequestHandler):
upstream=provider_data.upstream(), upstream=provider_data.upstream(),
ctype=provider_data.proxy_ctype(), ctype=provider_data.proxy_ctype(),
), ),
config.LinkWithType(
upstream=provider_data.upstream(),
ctype=provider_data.proxy_ctype(),
download=True,
),
config.LinkWithType( config.LinkWithType(
upstream=provider_data.thumbnail(), upstream=provider_data.thumbnail(),
), ),
] ]
) )
video_info["upstream"] = proxied[0] video_info.upstream = proxied[0]
video_info["poster"] = proxied[1] video_info.download = proxied[1]
if (
isinstance(video_info.ctype, str)
and "mpegurl" in video_info.ctype.lower()
):
video_info.download = None
video_info.poster = proxied[2]
video_info["ctype"] = provider_data.ctype() return (
return (video_info, provider_data.meta(), provider_data.title()) dataclasses.asdict(video_info),
provider_data.meta(),
provider_data.title(),
)
async def handle_raw(self, handler): async def handle_raw(self, handler):
try: try:

View File

@ -15,7 +15,7 @@
options.plugins.chromecast = {}; options.plugins.chromecast = {};
options.plugins.chromecast.addButtonToControlBar = false; options.plugins.chromecast.addButtonToControlBar = false;
const player = videojs(video, options); const player = videojs(video, options);
if((info.poster instanceof String) || ((typeof info.poster) == "string")) { if(info.poster) {
player.poster(info.poster); player.poster(info.poster);
} }
const source = {}; const source = {};
@ -24,8 +24,7 @@
player.src(source); player.src(source);
const dl = document.createElement("a"); const dl = document.createElement("a");
dl.href = info.upstream; dl.href = info.download;
dl.setAttribute("download", true);
const canPlayTypeRaw = player.canPlayType(info.ctype); const canPlayTypeRaw = player.canPlayType(info.ctype);
const canPlayType = (canPlayTypeRaw === "maybe") || (canPlayTypeRaw === "probably"); const canPlayType = (canPlayTypeRaw === "maybe") || (canPlayTypeRaw === "probably");
@ -66,7 +65,7 @@
bigCastButton.classList.add("vjs-big-chromecast"); bigCastButton.classList.add("vjs-big-chromecast");
player.addChild(bigCastButtonWrapper); player.addChild(bigCastButtonWrapper);
if(!info.ctype.toLowerCase().includes("mpegurl")) { if(info.download) {
bigDownloadButton.classList.add("vjs-control"); bigDownloadButton.classList.add("vjs-control");
bigDownloadButton.classList.add("vjs-button"); bigDownloadButton.classList.add("vjs-button");
bigDownloadButton.classList.add("fas"); bigDownloadButton.classList.add("fas");
@ -77,7 +76,7 @@
player.one("error", () => { player.one("error", () => {
player.removeChild(bigCastButtonWrapper); player.removeChild(bigCastButtonWrapper);
if(!info.ctype.toLowerCase().includes("mpegurl")) { if(info.download) {
player.removeChild(bigDownloadButtonWrapper); player.removeChild(bigDownloadButtonWrapper);
} }
}); });