Refactor extractors and routes for improved structure
Browse filesReorganized extractor routes into dedicated modules for better maintainability. Introduced a base class for extractors, enabling consistent request handling across different services. Additionally, updated configurations and error handling in extractors, enhancing code readability and robustness.
Files changed (14)
- mediaflow_proxy/configs.py+4
- mediaflow_proxy/extractors/__init__.pyAdded
- mediaflow_proxy/extractors/base.pyAdded+42
- mediaflow_proxy/extractors/doodstream.py+33-24
- mediaflow_proxy/extractors/factory.pyAdded+24
- mediaflow_proxy/extractors/mixdrop.py+24-20
- mediaflow_proxy/extractors/uqload.py+13-9
- mediaflow_proxy/extractors_routes.pyDeleted-55
- mediaflow_proxy/main.py+6-8
- mediaflow_proxy/routes/__init__.pyAdded+2
- mediaflow_proxy/routes/extractor.pyAdded+32
- mediaflow_proxy/routes.py → mediaflow_proxy/routes/proxy.pyRenamed+16-3
- mediaflow_proxy/schemas.py+8
- mediaflow_proxy/utils/http_utils.py+4
5 unmodified lines678910115 unmodified linesproxy_url: str | None = None # The URL of the proxy server to route requests through.enable_streaming_progress: bool = False # Whether to enable streaming progress tracking.class Config:env_file = ".env"extra = "ignore"5 unmodified lines67891011121314155 unmodified linesproxy_url: str | None = None # The URL of the proxy server to route requests through.enable_streaming_progress: bool = False # Whether to enable streaming progress tracking.user_agent: str = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" # The user agent to use for HTTP requests.)class Config:env_file = ".env"extra = "ignore"
123456789101112131415161718192021222324252627282930313233343536373839404142from abc import ABC, abstractmethodfrom typing import Dict, Tuple, Optional
import httpx
from mediaflow_proxy.configs import settings
class BaseExtractor(ABC): """Base class for all URL extractors."""
def __init__(self, proxy_enabled: bool = False): self.proxy_url = settings.proxy_url if proxy_enabled else None self.base_headers = { "User-Agent": settings.user_agent, "Accept-Language": "en-US,en;q=0.5", }
async def _make_request( self, url: str, headers: Optional[Dict] = None, follow_redirects: bool = True, **kwargs ) -> httpx.Response: """Make HTTP request with error handling.""" try: async with httpx.AsyncClient(proxy=self.proxy_url) as client: response = await client.get( url, headers={**self.base_headers, **(headers or {})}, follow_redirects=follow_redirects, timeout=30, **kwargs, ) response.raise_for_status() return response except httpx.HTTPError as e: raise ValueError(f"HTTP request failed: {str(e)}") except Exception as e: raise ValueError(f"Request failed: {str(e)}")
@abstractmethod async def extract(self, url: str) -> Tuple[str, Dict[str, str]]: """Extract final URL and required headers.""" pass12345678910111213141516171819202122232425import httpximport timeimport refrom mediaflow_proxy.configs import settingsasync def doodstream_url(d: str, use_request_proxy: bool):async with httpx.AsyncClient(proxy=settings.proxy_url if use_request_proxy else None) as client:headers = {"Range": "bytes=0-","Referer": "https://d000d.com/",}response = await client.get(d, follow_redirects=True)if response.status_code == 200:# Get unique timestamp for the requestreal_time = str(int(time.time()))pattern = r"(\/pass_md5\/.*?)'.*(\?token=.*?expiry=)"match = re.search(pattern, response.text, re.DOTALL)if match:url = f"https://d000d.com{match[1]}"rebobo = await client.get(url, headers=headers, follow_redirects=True)final_url = f"{rebobo.text}123456789{match[2]}{real_time}"doodstream_dict = {"Referer": "https://d000d.com/"}return final_url, doodstream_dict12345678910111213141516171819202122232425262728293031323334import reimport timefrom typing import Tuple, Dictfrom mediaflow_proxy.extractors.base import BaseExtractorclass DoodStreamExtractor(BaseExtractor):"""DoodStream URL extractor."""def __init__(self, proxy_enabled: bool = False):super().__init__(proxy_enabled)self.base_url = "https://d000d.com"async def extract(self, url: str) -> Tuple[str, Dict[str, str]]:"""Extract DoodStream URL."""response = await self._make_request(url)# Extract URL patternpattern = r"(\/pass_md5\/.*?)'.*(\?token=.*?expiry=)"match = re.search(pattern, response.text, re.DOTALL)if not match:raise ValueError("Failed to extract URL pattern")# Build final URLpass_url = f"{self.base_url}{match[1]}"referer = f"{self.base_url}/"headers = {"Range": "bytes=0-", "Referer": referer}rebobo_response = await self._make_request(pass_url, headers=headers)timestamp = str(int(time.time()))final_url = f"{rebobo_response.text}123456789{match[2]}{timestamp}"return final_url, {"Referer": referer}
123456789101112131415161718192021222324from typing import Dict, Type
from mediaflow_proxy.extractors.base import BaseExtractorfrom mediaflow_proxy.extractors.doodstream import DoodStreamExtractorfrom mediaflow_proxy.extractors.mixdrop import MixdropExtractorfrom mediaflow_proxy.extractors.uqload import UqloadExtractor
class ExtractorFactory: """Factory for creating URL extractors."""
_extractors: Dict[str, Type[BaseExtractor]] = { "Doodstream": DoodStreamExtractor, "Uqload": UqloadExtractor, "Mixdrop": MixdropExtractor, }
@classmethod def get_extractor(cls, host: str, proxy_enabled: bool = False) -> BaseExtractor: """Get appropriate extractor instance for the given host.""" extractor_class = cls._extractors.get(host) if not extractor_class: raise ValueError(f"Unsupported host: {host}") return extractor_class(proxy_enabled)123456789101112131415161718192021222324252627import httpximport reimport stringfrom mediaflow_proxy.configs import settingsasync def mixdrop_url(d: str, use_request_proxy: bool):headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.10; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3","Accept-Language": "en-US,en;q=0.5",}async with httpx.AsyncClient(proxy=settings.proxy_url if use_request_proxy else None) as client:response = await client.get(d, headers=headers, follow_redirects=True, timeout=30)[s1, s2] = re.search(r"\}\('(.+)',.+,'(.+)'\.split", response.text).group(1, 2)schema = s1.split(";")[2][5:-1]terms = s2.split("|")charset = string.digits + string.ascii_lettersd = dict()for i in range(len(terms)):d[charset[i]] = terms[i] or charset[i]final_url = "https:"for c in schema:final_url += d[c] if c in d else cheaders_dict = {"User-Agent": "Mozilla/5.0 (Windows NT 10.10; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}return final_url, headers_dict12345678910111213141516171819202122232425262728293031import reimport stringfrom typing import Dict, Tuplefrom mediaflow_proxy.extractors.base import BaseExtractorclass MixdropExtractor(BaseExtractor):"""Mixdrop URL extractor."""async def extract(self, url: str) -> Tuple[str, Dict[str, str]]:"""Extract Mixdrop URL."""response = await self._make_request(url)# Extract and decode URLmatch = re.search(r"\}\('(.+)',.+,'(.+)'\.split", response.text)if not match:raise ValueError("Failed to extract URL components")s1, s2 = match.group(1, 2)schema = s1.split(";")[2][5:-1]terms = s2.split("|")# Build character mappingcharset = string.digits + string.ascii_letterschar_map = {charset[i]: terms[i] or charset[i] for i in range(len(terms))}# Construct final URLfinal_url = "https:" + "".join(char_map.get(c, c) for c in schema)return final_url, {"User-Agent": self.base_headers["User-Agent"]}
1234567891011121314import httpximport refrom mediaflow_proxy.configs import settingsasync def uqload_url(d: str, use_request_proxy: bool):async with httpx.AsyncClient(proxy=settings.proxy_url if use_request_proxy else None) as client:response = await client.get(d, follow_redirects=True)video_url_match = re.search(r'sources: \["(.*?)"\]', response.text)if video_url_match:final_url = video_url_match.group(1)uqload_dict = {"Referer": "https://uqload.to/"}return final_url, uqload_dict123456789101112131415161718import refrom typing import Dict, Tuplefrom mediaflow_proxy.extractors.base import BaseExtractorclass UqloadExtractor(BaseExtractor):"""Uqload URL extractor."""async def extract(self, url: str) -> Tuple[str, Dict[str, str]]:"""Extract Uqload URL."""response = await self._make_request(url)video_url_match = re.search(r'sources: \["(.*?)"\]', response.text)if not video_url_match:raise ValueError("Failed to extract video URL")return video_url_match.group(1), {"Referer": "https://uqload.to/"}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from fastapi import APIRouter, Queryfrom fastapi.responses import JSONResponse, RedirectResponsefrom .extractors.doodstream import doodstream_urlfrom .extractors.uqload import uqload_urlfrom .extractors.mixdrop import mixdrop_urlfrom mediaflow_proxy.configs import settings
extractor_router = APIRouter()host_map = {"Doodstream": doodstream_url, "Mixdrop": mixdrop_url, "Uqload": uqload_url}
@extractor_router.get("/extractor")async def doodstream_extractor( d: str = Query(..., description="Extract Clean Link from various Hosts"), use_request_proxy: bool = Query(False, description="Whether to use the MediaFlow proxy configuration."), host: str = Query( ..., description='From which Host the URL comes from, here avaiable ones: "Doodstream","Mixdrop","Uqload"' ), redirect_stream: bool = Query( False, description="If enabled the response will be redirected to stream endpoint automatically and the stream will be proxied", ),): """ Extract a clean link from DoodStream,Mixdrop,Uqload
Args: request (Request): The incoming HTTP request
Returns: The clean link (url) and the headers needed to access the url
N.B. You can't use a rotating proxy if type is set to "Doodstream" """ try: final_url, headers_dict = await host_map[host](d, use_request_proxy) except Exception as e: return JSONResponse(content={"error": str(e)}) if redirect_stream == True: formatted_headers = format_headers(headers_dict) redirected_stream = f"/proxy/stream?api_password={settings.api_password}&d={final_url}&{formatted_headers}" return RedirectResponse(url=redirected_stream) elif redirect_stream == False: return JSONResponse(content={"url": final_url, "headers": headers_dict})
def format_headers(headers): """ Format the headers dictionary into a query string format with 'h_' prefix.
Args: - headers: A dictionary of headers.
Returns: - A query string formatted string of headers. """ return "&".join(f"h_{key}={value}" for key, value in headers.items())1234562 unmodified lines9101112131415161718192036 unmodified lines5758596061626333 unmodified lines9798991001011021031046 unmodified lines111112113114import loggingfrom importlib import resourcesimport uuidfrom fastapi import FastAPI, Depends, Security, HTTPException, BackgroundTasksfrom fastapi.security import APIKeyQuery, APIKeyHeader2 unmodified linesfrom starlette.staticfiles import StaticFilesfrom mediaflow_proxy.configs import settingsfrom mediaflow_proxy.routes import proxy_routerfrom mediaflow_proxy.extractors_routes import extractor_routerfrom mediaflow_proxy.schemas import GenerateUrlRequestfrom mediaflow_proxy.utils.crypto_utils import EncryptionHandler, EncryptionMiddlewarefrom mediaflow_proxy.utils.rd_speedtest import run_speedtest, prune_task, resultsfrom mediaflow_proxy.utils.http_utils import encode_mediaflow_proxy_urllogging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")app = FastAPI()36 unmodified lines# Generate a random UUID as task_idtask_id = str(uuid.uuid4()) # Generate unique task IDbackground_tasks.add_task(run_speedtest, task_id)# Schedule the task to be pruned after 1 hourbackground_tasks.add_task(prune_task, task_id)33 unmodified linesapp.include_router(proxy_router, prefix="/proxy", tags=["proxy"], dependencies=[Depends(verify_api_key)])app.include_router(extractor_router, tags=["extractors"], dependencies=[Depends(verify_api_key)])static_path = resources.files("mediaflow_proxy").joinpath("static")app.mount("/", StaticFiles(directory=str(static_path), html=True), name="static")6 unmodified linesif __name__ == "__main__":run()1234562 unmodified lines91011121314151617181936 unmodified lines5657585960616233 unmodified lines969798991001011026 unmodified lines109110111112import loggingimport uuidfrom importlib import resourcesfrom fastapi import FastAPI, Depends, Security, HTTPException, BackgroundTasksfrom fastapi.security import APIKeyQuery, APIKeyHeader2 unmodified linesfrom starlette.staticfiles import StaticFilesfrom mediaflow_proxy.configs import settingsfrom mediaflow_proxy.routes import proxy_router, extractor_routerfrom mediaflow_proxy.schemas import GenerateUrlRequestfrom mediaflow_proxy.utils.crypto_utils import EncryptionHandler, EncryptionMiddlewarefrom mediaflow_proxy.utils.http_utils import encode_mediaflow_proxy_urlfrom mediaflow_proxy.utils.rd_speedtest import run_speedtest, prune_task, resultslogging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")app = FastAPI()36 unmodified lines# Generate a random UUID as task_idtask_id = str(uuid.uuid4()) # Generate unique task IDbackground_tasks.add_task(run_speedtest, task_id)# Schedule the task to be pruned after 1 hourbackground_tasks.add_task(prune_task, task_id)33 unmodified linesapp.include_router(proxy_router, prefix="/proxy", tags=["proxy"], dependencies=[Depends(verify_api_key)])app.include_router(extractor_router, prefix="/extractor", tags=["extractors"], dependencies=[Depends(verify_api_key)])static_path = resources.files("mediaflow_proxy").joinpath("static")app.mount("/", StaticFiles(directory=str(static_path), html=True), name="static")6 unmodified linesif __name__ == "__main__":run()
12from .proxy import proxy_routerfrom .extractor import extractor_router1234567891011121314151617181920212223242526272829303132from typing import Annotated
from fastapi import APIRouter, Query, HTTPExceptionfrom fastapi.responses import RedirectResponse
from mediaflow_proxy.configs import settingsfrom mediaflow_proxy.extractors.factory import ExtractorFactoryfrom mediaflow_proxy.schemas import ExtractorURLParams
extractor_router = APIRouter()
@extractor_router.get("/video")async def extract_url( extractor_params: Annotated[ExtractorURLParams, Query()],): """Extract clean links from various video hosting services.""" try: extractor = ExtractorFactory.get_extractor(extractor_params.host, extractor_params.use_request_proxy) final_url, headers = await extractor.extract(extractor_params.destination)
if extractor_params.redirect_stream: formatted_headers = "&".join(f"h_{k}={v}" for k, v in headers.items()) stream_url = f"/proxy/stream?api_password={settings.api_password}&d={final_url}&{formatted_headers}" return RedirectResponse(url=stream_url)
return {"url": final_url, "headers": headers}
except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: raise HTTPException(status_code=500, detail=f"Extraction failed: {str(e)}")1 unmodified line23456789101 unmodified linefrom fastapi import Request, Depends, APIRouter, Query, HTTPExceptionfrom .handlers import handle_hls_stream_proxy, proxy_stream, get_manifest, get_playlist, get_segment, get_public_ipfrom .schemas import MPDSegmentParams, MPDPlaylistParams, HLSManifestParams, ProxyStreamParams, MPDManifestParamsfrom .utils.http_utils import get_proxy_headers, ProxyRequestHeadersproxy_router = APIRouter()1 unmodified line2345678910111213141516171819202122231 unmodified linefrom fastapi import Request, Depends, APIRouter, Query, HTTPExceptionfrom mediaflow_proxy.handlers import (handle_hls_stream_proxy,proxy_stream,get_manifest,get_playlist,get_segment,get_public_ip,)from mediaflow_proxy.schemas import (MPDSegmentParams,MPDPlaylistParams,HLSManifestParams,ProxyStreamParams,MPDManifestParams,)from mediaflow_proxy.utils.http_utils import get_proxy_headers, ProxyRequestHeadersproxy_router = APIRouter()
12351 unmodified lines555657from pydantic import BaseModel, Field, IPvAnyAddress, ConfigDict51 unmodified linesmime_type: str = Field(..., description="The MIME type of the segment.")key_id: str | None = Field(None, description="The DRM key ID (optional).")key: str | None = Field(None, description="The DRM key (optional).")1234551 unmodified lines575859606162636465from typing import Literalfrom pydantic import BaseModel, Field, IPvAnyAddress, ConfigDict51 unmodified linesmime_type: str = Field(..., description="The MIME type of the segment.")key_id: str | None = Field(None, description="The DRM key ID (optional).")key: str | None = Field(None, description="The DRM key (optional).")class ExtractorURLParams(GenericParams):host: Literal["Doodstream", "Mixdrop", "Uqload"] = Field(..., description="The host to extract the URL from.")destination: str = Field(..., description="The URL of the stream.", alias="d")redirect_stream: bool = Field(False, description="Whether to redirect to the stream endpoint automatically.")
124 unmodified lines125126127128129130124 unmodified lineselse:async for chunk in self.response.aiter_bytes():yield chunkexcept GeneratorExit:logger.info("Streaming session stopped by the user")except Exception as e:124 unmodified lines125126127128129130131132133134124 unmodified lineselse:async for chunk in self.response.aiter_bytes():yield chunkself.bytes_transferred += len(chunk)except httpx.TimeoutException:logger.warning(f"Timeout while streaming {url}")raise DownloadError(409, f"Timeout while streaming {url}")except GeneratorExit:logger.info("Streaming session stopped by the user")except Exception as e:
mediaflow_proxy/configs.py+4
mediaflow_proxy/extractors/__init__.pyAdded
mediaflow_proxy/extractors/base.pyAdded+42
mediaflow_proxy/extractors/doodstream.py+33-24
mediaflow_proxy/extractors/factory.pyAdded+24
mediaflow_proxy/extractors/mixdrop.py+24-20
mediaflow_proxy/extractors/uqload.py+13-9
mediaflow_proxy/extractors_routes.pyDeleted-55
mediaflow_proxy/main.py+6-8
mediaflow_proxy/routes/__init__.pyAdded+2
mediaflow_proxy/routes/extractor.pyAdded+32
mediaflow_proxy/routes.py → mediaflow_proxy/routes/proxy.pyRenamed+16-3
mediaflow_proxy/schemas.py+8
mediaflow_proxy/utils/http_utils.py+4