feat: enable use of http headers for providers

This commit is contained in:
Benex254
2024-08-17 15:17:53 +03:00
parent abab2540a3
commit 454341eaf5
10 changed files with 101 additions and 22 deletions

View File

@@ -2,6 +2,8 @@ import re
import shutil
import subprocess
from fastanime.constants import S_PLATFORM
def stream_video(MPV, url, mpv_args, custom_args):
process = subprocess.Popen(
@@ -52,6 +54,7 @@ def run_mpv(
start_time: str = "0",
ytdl_format="",
custom_args=[],
headers={},
):
# Determine if mpv is available
MPV = shutil.which("mpv")
@@ -61,7 +64,7 @@ def run_mpv(
# Regex to check if the link is a YouTube URL
youtube_regex = r"(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/.+"
if not MPV:
if not MPV and not S_PLATFORM == "win32":
# Determine if the link is a YouTube URL
if re.match(youtube_regex, link):
# Android specific commands to launch mpv with a YouTube URL
@@ -100,6 +103,11 @@ def run_mpv(
else:
# General mpv command with custom arguments
mpv_args = []
if headers:
mpv_headers = "--http-header-fields="
for header_name, header_value in headers.items():
mpv_headers += f"{header_name}:{header_value},"
mpv_args.append(mpv_headers)
if start_time != "0":
mpv_args.append(f"--start={start_time}")
if title:

View File

@@ -151,6 +151,7 @@ class MpvPlayer(object):
fastanime_runtime_state,
config: "Config",
title,
headers={},
):
self.anime_provider = anime_provider
self.fastanime_runtime_state = fastanime_runtime_state
@@ -174,6 +175,11 @@ class MpvPlayer(object):
# mpv_player.cache = "yes"
# mpv_player.cache_pause = "no"
mpv_player.title = title
mpv_headers = ""
if headers:
for header_name, header_value in headers.items():
mpv_headers += f"{header_name}:{header_value},"
mpv_player.http_header_fields = mpv_headers
mpv_player.play(stream_link)

View File

@@ -4,7 +4,7 @@ import subprocess
from .tools import exit_app
def SyncPlayer(url: str, anime_title=None, *args):
def SyncPlayer(url: str, anime_title=None, headers={}, *args):
# TODO: handle m3u8 multi quality streams
#
# check for SyncPlay
@@ -14,6 +14,12 @@ def SyncPlayer(url: str, anime_title=None, *args):
exit_app(1)
return "0", "0"
# start SyncPlayer
mpv_args = []
if headers:
mpv_headers = "--http-header-fields="
for header_name, header_value in headers.items():
mpv_headers += f"{header_name}:{header_value},"
mpv_args.append(mpv_headers)
if not anime_title:
subprocess.run(
[
@@ -23,7 +29,13 @@ def SyncPlayer(url: str, anime_title=None, *args):
)
else:
subprocess.run(
[SYNCPLAY_EXECUTABLE, url, "--", f"--force-media-title={anime_title}"]
[
SYNCPLAY_EXECUTABLE,
url,
"--",
f"--force-media-title={anime_title}",
*mpv_args,
]
)
# for compatability