Spaces:
Paused
Paused
Add request & response proxy for extractors
Browse files
mediaflow_proxy/extractors/base.py
CHANGED
|
@@ -9,11 +9,12 @@ from mediaflow_proxy.configs import settings
|
|
| 9 |
class BaseExtractor(ABC):
|
| 10 |
"""Base class for all URL extractors."""
|
| 11 |
|
| 12 |
-
def __init__(self, proxy_enabled: bool
|
| 13 |
self.proxy_url = settings.proxy_url if proxy_enabled else None
|
| 14 |
self.base_headers = {
|
| 15 |
"User-Agent": settings.user_agent,
|
| 16 |
"Accept-Language": "en-US,en;q=0.5",
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
async def _make_request(
|
|
|
|
| 9 |
class BaseExtractor(ABC):
|
| 10 |
"""Base class for all URL extractors."""
|
| 11 |
|
| 12 |
+
def __init__(self, proxy_enabled: bool, request_headers: dict):
|
| 13 |
self.proxy_url = settings.proxy_url if proxy_enabled else None
|
| 14 |
self.base_headers = {
|
| 15 |
"User-Agent": settings.user_agent,
|
| 16 |
"Accept-Language": "en-US,en;q=0.5",
|
| 17 |
+
**request_headers,
|
| 18 |
}
|
| 19 |
|
| 20 |
async def _make_request(
|
mediaflow_proxy/extractors/doodstream.py
CHANGED
|
@@ -8,8 +8,8 @@ from mediaflow_proxy.extractors.base import BaseExtractor
|
|
| 8 |
class DoodStreamExtractor(BaseExtractor):
|
| 9 |
"""DoodStream URL extractor."""
|
| 10 |
|
| 11 |
-
def __init__(self, proxy_enabled: bool
|
| 12 |
-
super().__init__(proxy_enabled)
|
| 13 |
self.base_url = "https://d000d.com"
|
| 14 |
|
| 15 |
async def extract(self, url: str) -> Tuple[str, Dict[str, str]]:
|
|
|
|
| 8 |
class DoodStreamExtractor(BaseExtractor):
|
| 9 |
"""DoodStream URL extractor."""
|
| 10 |
|
| 11 |
+
def __init__(self, proxy_enabled: bool, request_headers: dict):
|
| 12 |
+
super().__init__(proxy_enabled, request_headers)
|
| 13 |
self.base_url = "https://d000d.com"
|
| 14 |
|
| 15 |
async def extract(self, url: str) -> Tuple[str, Dict[str, str]]:
|
mediaflow_proxy/extractors/factory.py
CHANGED
|
@@ -16,9 +16,9 @@ class ExtractorFactory:
|
|
| 16 |
}
|
| 17 |
|
| 18 |
@classmethod
|
| 19 |
-
def get_extractor(cls, host: str, proxy_enabled: bool
|
| 20 |
"""Get appropriate extractor instance for the given host."""
|
| 21 |
extractor_class = cls._extractors.get(host)
|
| 22 |
if not extractor_class:
|
| 23 |
raise ValueError(f"Unsupported host: {host}")
|
| 24 |
-
return extractor_class(proxy_enabled)
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
@classmethod
|
| 19 |
+
def get_extractor(cls, host: str, proxy_enabled: bool, request_headers: dict) -> BaseExtractor:
|
| 20 |
"""Get appropriate extractor instance for the given host."""
|
| 21 |
extractor_class = cls._extractors.get(host)
|
| 22 |
if not extractor_class:
|
| 23 |
raise ValueError(f"Unsupported host: {host}")
|
| 24 |
+
return extractor_class(proxy_enabled, request_headers)
|
mediaflow_proxy/routes/extractor.py
CHANGED
|
@@ -1,11 +1,17 @@
|
|
| 1 |
from typing import Annotated
|
| 2 |
|
| 3 |
-
from fastapi import APIRouter, Query, HTTPException
|
| 4 |
from fastapi.responses import RedirectResponse
|
| 5 |
|
| 6 |
from mediaflow_proxy.configs import settings
|
| 7 |
from mediaflow_proxy.extractors.factory import ExtractorFactory
|
| 8 |
from mediaflow_proxy.schemas import ExtractorURLParams
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
extractor_router = APIRouter()
|
| 11 |
|
|
@@ -13,15 +19,25 @@ extractor_router = APIRouter()
|
|
| 13 |
@extractor_router.get("/video")
|
| 14 |
async def extract_url(
|
| 15 |
extractor_params: Annotated[ExtractorURLParams, Query()],
|
|
|
|
|
|
|
| 16 |
):
|
| 17 |
"""Extract clean links from various video hosting services."""
|
| 18 |
try:
|
| 19 |
-
extractor = ExtractorFactory.get_extractor(
|
|
|
|
|
|
|
| 20 |
final_url, headers = await extractor.extract(extractor_params.destination)
|
| 21 |
|
| 22 |
if extractor_params.redirect_stream:
|
| 23 |
-
|
| 24 |
-
stream_url =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return RedirectResponse(url=stream_url)
|
| 26 |
|
| 27 |
return {"url": final_url, "headers": headers}
|
|
|
|
| 1 |
from typing import Annotated
|
| 2 |
|
| 3 |
+
from fastapi import APIRouter, Query, HTTPException, Request, Depends
|
| 4 |
from fastapi.responses import RedirectResponse
|
| 5 |
|
| 6 |
from mediaflow_proxy.configs import settings
|
| 7 |
from mediaflow_proxy.extractors.factory import ExtractorFactory
|
| 8 |
from mediaflow_proxy.schemas import ExtractorURLParams
|
| 9 |
+
from mediaflow_proxy.utils.http_utils import (
|
| 10 |
+
encode_mediaflow_proxy_url,
|
| 11 |
+
get_original_scheme,
|
| 12 |
+
ProxyRequestHeaders,
|
| 13 |
+
get_proxy_headers,
|
| 14 |
+
)
|
| 15 |
|
| 16 |
extractor_router = APIRouter()
|
| 17 |
|
|
|
|
| 19 |
@extractor_router.get("/video")
|
| 20 |
async def extract_url(
|
| 21 |
extractor_params: Annotated[ExtractorURLParams, Query()],
|
| 22 |
+
request: Request,
|
| 23 |
+
proxy_headers: Annotated[ProxyRequestHeaders, Depends(get_proxy_headers)],
|
| 24 |
):
|
| 25 |
"""Extract clean links from various video hosting services."""
|
| 26 |
try:
|
| 27 |
+
extractor = ExtractorFactory.get_extractor(
|
| 28 |
+
extractor_params.host, extractor_params.use_request_proxy, proxy_headers.request
|
| 29 |
+
)
|
| 30 |
final_url, headers = await extractor.extract(extractor_params.destination)
|
| 31 |
|
| 32 |
if extractor_params.redirect_stream:
|
| 33 |
+
headers.update(proxy_headers.request)
|
| 34 |
+
stream_url = encode_mediaflow_proxy_url(
|
| 35 |
+
str(request.url_for("proxy_stream_endpoint").replace(scheme=get_original_scheme(request))),
|
| 36 |
+
destination_url=final_url,
|
| 37 |
+
query_params={"api_password": settings.api_password},
|
| 38 |
+
request_headers=headers,
|
| 39 |
+
response_headers=proxy_headers.response,
|
| 40 |
+
)
|
| 41 |
return RedirectResponse(url=stream_url)
|
| 42 |
|
| 43 |
return {"url": final_url, "headers": headers}
|