fix: Usunięto Session z decode_google_news_url, dodano max_depth
- Zamieniono requests.Session() na bezpośredni requests.get() - Dodano max_depth=3 jako zabezpieczenie przed nieskończoną rekurencją - Jawne zamykanie response.close() po każdym request Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
af4c0d157a
commit
8055589a08
@ -46,7 +46,7 @@ HEADERS = {
|
||||
REQUEST_TIMEOUT = 15
|
||||
|
||||
|
||||
def decode_google_news_url(google_url: str) -> str | None:
|
||||
def decode_google_news_url(google_url: str, max_depth: int = 3) -> str | None:
|
||||
"""
|
||||
Dekoduj URL Google News do oryginalnego źródła.
|
||||
|
||||
@ -54,9 +54,15 @@ def decode_google_news_url(google_url: str) -> str | None:
|
||||
1. /rss/articles/CBMi... - Base64 encoded
|
||||
2. /articles/CBMi... - Base64 encoded
|
||||
3. Przekierowania przez consent.google.com
|
||||
"""
|
||||
|
||||
# Metoda 1: Dekodowanie Base64 z URL
|
||||
Args:
|
||||
google_url: URL do zdekodowania
|
||||
max_depth: Maksymalna głębokość (zabezpieczenie przed nieskończoną pętlą)
|
||||
"""
|
||||
if max_depth <= 0:
|
||||
return None
|
||||
|
||||
# Metoda 1: Dekodowanie Base64 z URL (preferowana - bez HTTP request)
|
||||
try:
|
||||
# Znajdź zakodowaną część
|
||||
match = re.search(r'/articles/([A-Za-z0-9_-]+)', google_url)
|
||||
@ -73,7 +79,6 @@ def decode_google_news_url(google_url: str) -> str | None:
|
||||
decoded = base64.urlsafe_b64decode(encoded)
|
||||
|
||||
# Szukaj URL-ów w zdekodowanych danych
|
||||
# Format: często zaczyna się od \x08 i zawiera URL po kilku bajtach
|
||||
urls = re.findall(rb'https?://[^\x00-\x1f\s"\'<>]+', decoded)
|
||||
|
||||
for url in urls:
|
||||
@ -82,9 +87,9 @@ def decode_google_news_url(google_url: str) -> str | None:
|
||||
# Pomijamy URL-e Google
|
||||
if 'google.' not in url_str and len(url_str) > 20:
|
||||
# Wyczyść URL
|
||||
url_str = url_str.split('\x00')[0] # Usuń null bytes
|
||||
url_str = url_str.split('\r')[0] # Usuń CR
|
||||
url_str = url_str.split('\n')[0] # Usuń LF
|
||||
url_str = url_str.split('\x00')[0]
|
||||
url_str = url_str.split('\r')[0]
|
||||
url_str = url_str.split('\n')[0]
|
||||
if url_str.startswith('http'):
|
||||
return url_str
|
||||
except:
|
||||
@ -94,30 +99,32 @@ def decode_google_news_url(google_url: str) -> str | None:
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
# Metoda 2: Podążaj za przekierowaniami
|
||||
# Metoda 2: Podążaj za przekierowaniami (tylko jeśli Base64 nie zadziałał)
|
||||
# UWAGA: Ta metoda wykonuje HTTP request
|
||||
try:
|
||||
# Użyj context manager żeby zamknąć sesję po użyciu
|
||||
with requests.Session() as session:
|
||||
session.headers.update(HEADERS)
|
||||
response = requests.get(
|
||||
google_url,
|
||||
headers=HEADERS,
|
||||
timeout=REQUEST_TIMEOUT,
|
||||
allow_redirects=True
|
||||
)
|
||||
final_url = response.url
|
||||
response.close()
|
||||
|
||||
response = session.get(google_url, timeout=REQUEST_TIMEOUT, allow_redirects=True)
|
||||
final_url = response.url
|
||||
response.close() # Jawne zamknięcie odpowiedzi
|
||||
# Jeśli wylądowaliśmy na consent.google.com, wyciągnij URL z parametrów
|
||||
if 'consent.google.com' in final_url:
|
||||
parsed = urlparse(final_url)
|
||||
params = parse_qs(parsed.query)
|
||||
if 'continue' in params:
|
||||
continue_url = unquote(params['continue'][0])
|
||||
# Iteracyjnie dekoduj (nie rekurencyjnie!)
|
||||
if 'news.google.com' in continue_url:
|
||||
return decode_google_news_url(continue_url, max_depth - 1)
|
||||
return continue_url
|
||||
|
||||
# Jeśli wylądowaliśmy na consent.google.com, szukaj URL w parametrach
|
||||
if 'consent.google.com' in final_url:
|
||||
parsed = urlparse(final_url)
|
||||
params = parse_qs(parsed.query)
|
||||
if 'continue' in params:
|
||||
continue_url = unquote(params['continue'][0])
|
||||
# Rekurencyjnie dekoduj
|
||||
if 'news.google.com' in continue_url:
|
||||
return decode_google_news_url(continue_url)
|
||||
return continue_url
|
||||
|
||||
# Jeśli to nie jest Google, mamy oryginalny URL
|
||||
if 'google.com' not in final_url:
|
||||
return final_url
|
||||
# Jeśli to nie jest Google, mamy oryginalny URL
|
||||
if 'google.com' not in final_url:
|
||||
return final_url
|
||||
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
Loading…
Reference in New Issue
Block a user