Spaces:
Paused
Paused
Fix key URL in m3u8 & update libs & bump version
Browse filesFiles changed (5)
12345678976 unmodified lines86878889909192106 unmodified lines1992002012022032042053 unmodified lines209210211212213214215import base64import loggingimport httpxfrom fastapi import Request, Response, HTTPExceptionfrom pydantic import HttpUrlfrom starlette.background import BackgroundTaskfrom .configs import settings76 unmodified linesclient, streamer = await setup_client_and_streamer(hls_params.use_request_proxy, hls_params.verify_ssl)try:if hls_params.destination.endswith((".m3u", ".m3u8")):return await fetch_and_process_m3u8(streamer, hls_params.destination, proxy_headers, request, hls_params.key_url)106 unmodified linesasync def fetch_and_process_m3u8(streamer: Streamer, url: str, proxy_headers: ProxyRequestHeaders, request: Request, key_url: HttpUrl = None):"""Fetches and processes the m3u8 playlist, converting it to an HLS playlist.3 unmodified linesurl (str): The URL of the m3u8 playlist.proxy_headers (ProxyRequestHeaders): The headers to include in the request.request (Request): The incoming HTTP request.key_url (HttpUrl, optional): The HLS Key URL to replace the original key URL. Defaults to None.Returns:Response: The HTTP response with the processed m3u8 playlist.12345678976 unmodified lines86878889909192106 unmodified lines1992002012022032042053 unmodified lines209210211212213214215import base64import loggingfrom urllib.parse import urlparseimport httpxfrom fastapi import Request, Response, HTTPExceptionfrom starlette.background import BackgroundTaskfrom .configs import settings76 unmodified linesclient, streamer = await setup_client_and_streamer(hls_params.use_request_proxy, hls_params.verify_ssl)try:if urlparse(hls_params.destination).path.endswith((".m3u", ".m3u8")):return await fetch_and_process_m3u8(streamer, hls_params.destination, proxy_headers, request, hls_params.key_url)106 unmodified linesasync def fetch_and_process_m3u8(streamer: Streamer, url: str, proxy_headers: ProxyRequestHeaders, request: Request, key_url: str = None):"""Fetches and processes the m3u8 playlist, converting it to an HLS playlist.3 unmodified linesurl (str): The URL of the m3u8 playlist.proxy_headers (ProxyRequestHeaders): The headers to include in the request.request (Request): The incoming HTTP request.key_url (str, optional): The HLS Key URL to replace the original key URL. Defaults to None.Returns:Response: The HTTP response with the processed m3u8 playlist.
369 unmodified lines370371372373374375376369 unmodified linesawait send({"type": "http.response.body", "body": b"", "more_body": False})except Exception as e:logger.error(f"Error in stream_response: {str(e)}")async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:async with anyio.create_task_group() as task_group:369 unmodified lines370371372373374375376369 unmodified linesawait send({"type": "http.response.body", "body": b"", "more_body": False})except Exception as e:logger.exception(f"Error in stream_response: {str(e)}")async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:async with anyio.create_task_group() as task_group:
12345678910111213142 unmodified lines1718192021222334 unmodified lines58596061626364import refrom urllib import parsefrom pydantic import HttpUrlfrom mediaflow_proxy.utils.crypto_utils import encryption_handlerfrom mediaflow_proxy.utils.http_utils import encode_mediaflow_proxy_url, get_original_schemeclass M3U8Processor:def __init__(self, request, key_url: HttpUrl = None):"""Initializes the M3U8Processor with the request and URL prefix.2 unmodified lineskey_url (HttpUrl, optional): The URL of the key server. Defaults to None."""self.request = requestself.key_url = key_urlself.mediaflow_proxy_url = str(request.url_for("hls_stream_proxy").replace(scheme=get_original_scheme(request)))async def process_m3u8(self, content: str, base_url: str) -> str:34 unmodified linesoriginal_uri = uri_match.group(1)uri = parse.urlparse(original_uri)if self.key_url:uri = uri._replace(scheme=self.key_url.scheme, netloc=self.key_url.host)new_uri = await self.proxy_url(uri.geturl(), base_url)line = line.replace(f'URI="{original_uri}"', f'URI="{new_uri}"')return line1234567891011122 unmodified lines1516171819202134 unmodified lines56575859606162import refrom urllib import parsefrom mediaflow_proxy.utils.crypto_utils import encryption_handlerfrom mediaflow_proxy.utils.http_utils import encode_mediaflow_proxy_url, get_original_schemeclass M3U8Processor:def __init__(self, request, key_url: str = None):"""Initializes the M3U8Processor with the request and URL prefix.2 unmodified lineskey_url (HttpUrl, optional): The URL of the key server. Defaults to None."""self.request = requestself.key_url = parse.urlparse(key_url) if key_url else Noneself.mediaflow_proxy_url = str(request.url_for("hls_stream_proxy").replace(scheme=get_original_scheme(request)))async def process_m3u8(self, content: str, base_url: str) -> str:34 unmodified linesoriginal_uri = uri_match.group(1)uri = parse.urlparse(original_uri)if self.key_url:uri = uri._replace(scheme=self.key_url.scheme, netloc=self.key_url.netloc)new_uri = await self.proxy_url(uri.geturl(), base_url)line = line.replace(f'URI="{original_uri}"', f'URI="{new_uri}"')return line
34 unmodified lines3536373839404142434445464748495051525354555657585960616263646566677 unmodified lines75767778798081497 unmodified lines5795805815825835845855865875885895905916 unmodified lines59859960060160260360460560660760860961061161261334 unmodified lines[[package]]name = "black"version = "24.8.0"description = "The uncompromising code formatter."optional = falsepython-versions = ">=3.8"files = [{file = "black-24.8.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:09cdeb74d494ec023ded657f7092ba518e8cf78fa8386155e4a03fdcc44679e6"},{file = "black-24.8.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:81c6742da39f33b08e791da38410f32e27d632260e599df7245cccee2064afeb"},{file = "black-24.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:707a1ca89221bc8a1a64fb5e15ef39cd755633daa672a9db7498d1c19de66a42"},{file = "black-24.8.0-cp310-cp310-win_amd64.whl", hash = "sha256:d6417535d99c37cee4091a2f24eb2b6d5ec42b144d50f1f2e436d9fe1916fe1a"},{file = "black-24.8.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:fb6e2c0b86bbd43dee042e48059c9ad7830abd5c94b0bc518c0eeec57c3eddc1"},{file = "black-24.8.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:837fd281f1908d0076844bc2b801ad2d369c78c45cf800cad7b61686051041af"},{file = "black-24.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:62e8730977f0b77998029da7971fa896ceefa2c4c4933fcd593fa599ecbf97a4"},{file = "black-24.8.0-cp311-cp311-win_amd64.whl", hash = "sha256:72901b4913cbac8972ad911dc4098d5753704d1f3c56e44ae8dce99eecb0e3af"},{file = "black-24.8.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:7c046c1d1eeb7aea9335da62472481d3bbf3fd986e093cffd35f4385c94ae368"},{file = "black-24.8.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:649f6d84ccbae73ab767e206772cc2d7a393a001070a4c814a546afd0d423aed"},{file = "black-24.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:2b59b250fdba5f9a9cd9d0ece6e6d993d91ce877d121d161e4698af3eb9c1018"},{file = "black-24.8.0-cp312-cp312-win_amd64.whl", hash = "sha256:6e55d30d44bed36593c3163b9bc63bf58b3b30e4611e4d88a0c3c239930ed5b2"},{file = "black-24.8.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:505289f17ceda596658ae81b61ebbe2d9b25aa78067035184ed0a9d855d18afd"},{file = "black-24.8.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:b19c9ad992c7883ad84c9b22aaa73562a16b819c1d8db7a1a1a49fb7ec13c7d2"},{file = "black-24.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:1f13f7f386f86f8121d76599114bb8c17b69d962137fc70efe56137727c7047e"},{file = "black-24.8.0-cp38-cp38-win_amd64.whl", hash = "sha256:f490dbd59680d809ca31efdae20e634f3fae27fba3ce0ba3208333b713bc3920"},{file = "black-24.8.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:eab4dd44ce80dea27dc69db40dab62d4ca96112f87996bca68cd75639aeb2e4c"},{file = "black-24.8.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:3c4285573d4897a7610054af5a890bde7c65cb466040c5f0c8b732812d7f0e5e"},{file = "black-24.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:9e84e33b37be070ba135176c123ae52a51f82306def9f7d063ee302ecab2cf47"},{file = "black-24.8.0-cp39-cp39-win_amd64.whl", hash = "sha256:73bbf84ed136e45d451a260c6b73ed674652f90a2b3211d6a35e78054563a9bb"},{file = "black-24.8.0-py3-none-any.whl", hash = "sha256:972085c618ee94f402da1af548a4f218c754ea7e5dc70acb168bfaca4c2542ed"},{file = "black-24.8.0.tar.gz", hash = "sha256:2500945420b6784c38b9ee885af039f5e7471ef284ab03fa35ecdde4688cd83f"},][package.dependencies]7 unmodified lines[package.extras]colorama = ["colorama (>=0.4.3)"]d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"]jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]uvloop = ["uvloop (>=0.15.2)"]497 unmodified lines[[package]]name = "uvicorn"version = "0.31.0"description = "The lightning-fast ASGI server."optional = falsepython-versions = ">=3.8"files = [{file = "uvicorn-0.31.0-py3-none-any.whl", hash = "sha256:cac7be4dd4d891c363cd942160a7b02e69150dcbc7a36be04d5f4af4b17c8ced"},{file = "uvicorn-0.31.0.tar.gz", hash = "sha256:13bc21373d103859f68fe739608e2eb054a816dea79189bc3ca08ea89a275906"},][package.dependencies]6 unmodified lines[[package]]name = "xmltodict"version = "0.13.0"description = "Makes working with XML feel like you are working with JSON"optional = falsepython-versions = ">=3.4"files = [{file = "xmltodict-0.13.0-py2.py3-none-any.whl", hash = "sha256:aa89e8fd76320154a40d19a0df04a4695fb9dc5ba977cbb68ab3e4eb225e7852"},{file = "xmltodict-0.13.0.tar.gz", hash = "sha256:341595a488e3e01a85a9d8911d8912fd922ede5fecc4dce437eb4b6c8d037e56"},][metadata]lock-version = "2.0"python-versions = ">=3.10"content-hash = "cfa51491c2cccf2ff0bd593a2ede5734b580e2c20f6380e6a50ea3d900c9e809"34 unmodified lines3536373839404142434445464748495051525354555657585960616263646566677 unmodified lines75767778798081497 unmodified lines5795805815825835845855865875885895905916 unmodified lines59859960060160260360460560660760860961061161261334 unmodified lines[[package]]name = "black"version = "24.10.0"description = "The uncompromising code formatter."optional = falsepython-versions = ">=3.9"files = [{file = "black-24.10.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:e6668650ea4b685440857138e5fe40cde4d652633b1bdffc62933d0db4ed9812"},{file = "black-24.10.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:1c536fcf674217e87b8cc3657b81809d3c085d7bf3ef262ead700da345bfa6ea"},{file = "black-24.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:649fff99a20bd06c6f727d2a27f401331dc0cc861fb69cde910fe95b01b5928f"},{file = "black-24.10.0-cp310-cp310-win_amd64.whl", hash = "sha256:fe4d6476887de70546212c99ac9bd803d90b42fc4767f058a0baa895013fbb3e"},{file = "black-24.10.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:5a2221696a8224e335c28816a9d331a6c2ae15a2ee34ec857dcf3e45dbfa99ad"},{file = "black-24.10.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f9da3333530dbcecc1be13e69c250ed8dfa67f43c4005fb537bb426e19200d50"},{file = "black-24.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4007b1393d902b48b36958a216c20c4482f601569d19ed1df294a496eb366392"},{file = "black-24.10.0-cp311-cp311-win_amd64.whl", hash = "sha256:394d4ddc64782e51153eadcaaca95144ac4c35e27ef9b0a42e121ae7e57a9175"},{file = "black-24.10.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:b5e39e0fae001df40f95bd8cc36b9165c5e2ea88900167bddf258bacef9bbdc3"},{file = "black-24.10.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:d37d422772111794b26757c5b55a3eade028aa3fde43121ab7b673d050949d65"},{file = "black-24.10.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:14b3502784f09ce2443830e3133dacf2c0110d45191ed470ecb04d0f5f6fcb0f"},{file = "black-24.10.0-cp312-cp312-win_amd64.whl", hash = "sha256:30d2c30dc5139211dda799758559d1b049f7f14c580c409d6ad925b74a4208a8"},{file = "black-24.10.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:1cbacacb19e922a1d75ef2b6ccaefcd6e93a2c05ede32f06a21386a04cedb981"},{file = "black-24.10.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:1f93102e0c5bb3907451063e08b9876dbeac810e7da5a8bfb7aeb5a9ef89066b"},{file = "black-24.10.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ddacb691cdcdf77b96f549cf9591701d8db36b2f19519373d60d31746068dbf2"},{file = "black-24.10.0-cp313-cp313-win_amd64.whl", hash = "sha256:680359d932801c76d2e9c9068d05c6b107f2584b2a5b88831c83962eb9984c1b"},{file = "black-24.10.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:17374989640fbca88b6a448129cd1745c5eb8d9547b464f281b251dd00155ccd"},{file = "black-24.10.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:63f626344343083322233f175aaf372d326de8436f5928c042639a4afbbf1d3f"},{file = "black-24.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ccfa1d0cb6200857f1923b602f978386a3a2758a65b52e0950299ea014be6800"},{file = "black-24.10.0-cp39-cp39-win_amd64.whl", hash = "sha256:2cd9c95431d94adc56600710f8813ee27eea544dd118d45896bb734e9d7a0dc7"},{file = "black-24.10.0-py3-none-any.whl", hash = "sha256:3bb2b7a1f7b685f85b11fed1ef10f8a9148bceb49853e47a294a3dd963c1dd7d"},{file = "black-24.10.0.tar.gz", hash = "sha256:846ea64c97afe3bc677b761787993be4991810ecc7a4a937816dd6bddedc4875"},][package.dependencies]7 unmodified lines[package.extras]colorama = ["colorama (>=0.4.3)"]d = ["aiohttp (>=3.10)"]jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"]uvloop = ["uvloop (>=0.15.2)"]497 unmodified lines[[package]]name = "uvicorn"version = "0.31.1"description = "The lightning-fast ASGI server."optional = falsepython-versions = ">=3.8"files = [{file = "uvicorn-0.31.1-py3-none-any.whl", hash = "sha256:adc42d9cac80cf3e51af97c1851648066841e7cfb6993a4ca8de29ac1548ed41"},{file = "uvicorn-0.31.1.tar.gz", hash = "sha256:f5167919867b161b7bcaf32646c6a94cdbd4c3aa2eb5c17d36bb9aa5cfd8c493"},][package.dependencies]6 unmodified lines[[package]]name = "xmltodict"version = "0.14.1"description = "Makes working with XML feel like you are working with JSON"optional = falsepython-versions = ">=3.6"files = [{file = "xmltodict-0.14.1-py2.py3-none-any.whl", hash = "sha256:3ef4a7b71c08f19047fcbea572e1d7f4207ab269da1565b5d40e9823d3894e63"},{file = "xmltodict-0.14.1.tar.gz", hash = "sha256:338c8431e4fc554517651972d62f06958718f6262b04316917008e8fd677a6b0"},][metadata]lock-version = "2.0"python-versions = ">=3.10"content-hash = "82b614db25311cde2b5ae15465b537fcb82c4c9cef5817fe45f030d1ea588646"
12345619 unmodified lines26272829303132[tool.poetry]name = "mediaflow-proxy"version = "1.7.2"description = "A high-performance proxy server for streaming media, supporting HTTP(S), HLS, and MPEG-DASH with real-time DRM decryption."authors = ["mhdzumair <mhdzumair@gmail.com>"]readme = "README.md"19 unmodified linesfastapi = "0.115.0"httpx = {extras = ["socks"], version = "^0.27.2"}tenacity = "^9.0.0"xmltodict = "^0.13.0"cachetools = "^5.4.0"pydantic-settings = "^2.5.2"gunicorn = "^23.0.0"12345619 unmodified lines26272829303132[tool.poetry]name = "mediaflow-proxy"version = "1.7.3"description = "A high-performance proxy server for streaming media, supporting HTTP(S), HLS, and MPEG-DASH with real-time DRM decryption."authors = ["mhdzumair <mhdzumair@gmail.com>"]readme = "README.md"19 unmodified linesfastapi = "0.115.0"httpx = {extras = ["socks"], version = "^0.27.2"}tenacity = "^9.0.0"xmltodict = "^0.14.0"cachetools = "^5.4.0"pydantic-settings = "^2.5.2"gunicorn = "^23.0.0"
mediaflow_proxy/handlers.py+4-4
mediaflow_proxy/utils/http_utils.py+1-1
mediaflow_proxy/utils/m3u8_processor.py+3-5
poetry.lock+33-33
pyproject.toml+2-2