Spaces:
Paused
Paused
Enhance streaming functionality and progress tracking
Browse filesFiles changed (2)
81 unmodified lines82838485868788894 unmodified lines94959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812937 unmodified lines16716816917017117217381 unmodified linesself.client = clientself.response = Noneself.progress_bar = Noneasync def stream_content(self, url: str, headers: dict):"""Streams content from a URL.4 unmodified linesYields:bytes: Chunks of the streamed content."""async with self.client.stream("GET", url, headers=headers, follow_redirects=True) as self.response:self.response.raise_for_status()if settings.enable_streaming_progress:with tqdm_asyncio(total=self.get_content_length(),initial=self.get_start_byte(headers),unit="B",unit_scale=True,unit_divisor=1024,desc="MediaFlow Proxy Streaming",) as self.progress_bar:async for chunk in self.response.aiter_bytes():yield chunkself.progress_bar.update(len(chunk))else:async for chunk in self.response.aiter_bytes():yield chunkdef get_content_length(self) -> int:content_range = self.response.headers.get("Content-Range")if content_range:return int(content_range.split("/")[-1])return int(self.response.headers.get("Content-Length", 0))@staticmethoddef get_start_byte(headers: dict) -> int:range_header = headers.get("range")start_byte = int(range_header.split("=")[-1].split("-")[0])return start_byteasync def head(self, url: str, headers: dict):"""37 unmodified linesawait self.response.aclose()if self.progress_bar:self.progress_bar.close()logger.info("Stopped the current streaming session")await self.client.aclose()81 unmodified lines8283848586878889909192934 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515637 unmodified lines19419519619719819981 unmodified linesself.client = clientself.response = Noneself.progress_bar = Noneself.bytes_transferred = 0self.start_byte = 0self.end_byte = 0self.total_size = 0async def stream_content(self, url: str, headers: dict) -> typing.AsyncGenerator[bytes, None]:"""Streams content from a URL.4 unmodified linesYields:bytes: Chunks of the streamed content."""try:async with self.client.stream("GET", url, headers=headers, follow_redirects=True) as self.response:self.response.raise_for_status()self.parse_content_range()if settings.enable_streaming_progress:with tqdm_asyncio(total=self.total_size,initial=self.start_byte,unit="B",unit_scale=True,unit_divisor=1024,desc="Streaming",ncols=100,mininterval=1,) as self.progress_bar:async for chunk in self.response.aiter_bytes():yield chunkchunk_size = len(chunk)self.bytes_transferred += chunk_sizeself.progress_bar.set_postfix_str(f"📥 : {self.format_bytes(self.bytes_transferred)}", refresh=False)self.progress_bar.update(chunk_size)else:async for chunk in self.response.aiter_bytes():yield chunkexcept GeneratorExit:logger.info("Streaming session stopped by the user")except Exception as e:logger.error(f"Error streaming content: {e}")finally:await self.close()@staticmethoddef format_bytes(size) -> str:power = 2**10n = 0units = {0: "B", 1: "KB", 2: "MB", 3: "GB", 4: "TB"}while size > power:size /= powern += 1return f"{size:.2f} {units[n]}"def parse_content_range(self):content_range = self.response.headers.get("Content-Range", "")if content_range:range_info = content_range.split()[-1]self.start_byte, self.end_byte, self.total_size = map(int, range_info.replace("/", "-").split("-"))else:self.start_byte = 0self.total_size = int(self.response.headers.get("Content-Length", 0))self.end_byte = self.total_size - 1 if self.total_size > 0 else 0async def head(self, url: str, headers: dict):"""37 unmodified linesawait self.response.aclose()if self.progress_bar:self.progress_bar.close()await self.client.aclose()
123456[tool.poetry]name = "mediaflow-proxy"version = "1.7.5"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"123456[tool.poetry]name = "mediaflow-proxy"version = "1.7.6"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"
mediaflow_proxy/utils/http_utils.py+54-28
pyproject.toml+1-1