duck3-create Claude Opus 4.6 commited on
Commit
4123369
·
1 Parent(s): 30b5d98

Fix WorkerProxySession to inherit from requests.Session

Browse files

- youtube-transcript-api expects http_client.headers attribute
- Inherit from requests.Session for full compatibility
- Override request() method to route all URLs through Worker

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. main.py +8 -10
main.py CHANGED
@@ -12,6 +12,7 @@ import asyncio
12
  from concurrent.futures import ThreadPoolExecutor
13
  import os
14
  import urllib.parse
 
15
 
16
  logging.basicConfig(level=logging.INFO)
17
  logger = logging.getLogger(__name__)
@@ -41,21 +42,18 @@ if _proxy_url:
41
  # --- Cloudflare Worker proxy support (WORKER_URL env var) ---
42
  _worker_url = os.environ.get("WORKER_URL", "")
43
 
44
- class _WorkerProxySession:
45
  """Routes requests through a Cloudflare Worker to bypass YouTube IP blocks."""
46
 
47
  def __init__(self, worker_url):
48
- import requests as _req
49
- self._session = _req.Session()
50
  self._worker_url = worker_url.rstrip('/')
51
 
52
- def get(self, url, **kwargs):
53
- proxied = f"{self._worker_url}/?url={urllib.parse.quote(url, safe='')}"
54
- return self._session.get(proxied, **kwargs)
55
-
56
- def post(self, url, **kwargs):
57
- proxied = f"{self._worker_url}/?url={urllib.parse.quote(url, safe='')}"
58
- return self._session.post(proxied, **kwargs)
59
 
60
  # --- API instances: plain (no cookies) + with cookies (fallback) ---
61
  if _worker_url:
 
12
  from concurrent.futures import ThreadPoolExecutor
13
  import os
14
  import urllib.parse
15
+ import requests as _requests_mod
16
 
17
  logging.basicConfig(level=logging.INFO)
18
  logger = logging.getLogger(__name__)
 
42
  # --- Cloudflare Worker proxy support (WORKER_URL env var) ---
43
  _worker_url = os.environ.get("WORKER_URL", "")
44
 
45
+ class _WorkerProxySession(_requests_mod.Session):
46
  """Routes requests through a Cloudflare Worker to bypass YouTube IP blocks."""
47
 
48
  def __init__(self, worker_url):
49
+ super().__init__()
 
50
  self._worker_url = worker_url.rstrip('/')
51
 
52
+ def request(self, method, url, **kwargs):
53
+ if url.startswith('http'):
54
+ proxied = f"{self._worker_url}/?url={urllib.parse.quote(url, safe='')}"
55
+ return super().request(method, proxied, **kwargs)
56
+ return super().request(method, url, **kwargs)
 
 
57
 
58
  # --- API instances: plain (no cookies) + with cookies (fallback) ---
59
  if _worker_url: