|
4 | 4 | from bs4 import BeautifulSoup |
5 | 5 | from groq import Groq |
6 | 6 | from dotenv import load_dotenv |
| 7 | +import cloudscraper |
7 | 8 |
|
8 | 9 | # Cargar variables de entorno desde .env si existe (local) |
9 | 10 | load_dotenv() |
|
32 | 33 |
|
33 | 34 | groq_client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None |
34 | 35 |
|
35 | | -# User-Agent y headers avanzados para evitar bloqueos (403 Forbidden) |
| 36 | +# Inicializamos el scraper de Cloudflare una sola vez |
| 37 | +scraper = cloudscraper.create_scraper( |
| 38 | + browser={ |
| 39 | + 'browser': 'chrome', |
| 40 | + 'platform': 'windows', |
| 41 | + 'desktop': True |
| 42 | + } |
| 43 | +) |
| 44 | + |
36 | 45 | HEADERS = { |
37 | | - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', |
38 | 46 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8', |
39 | 47 | 'Accept-Language': 'es-ES,es;q=0.9,en;q=0.8', |
40 | 48 | 'Referer': 'https://www.google.com/', |
41 | | - 'Connection': 'keep-alive', |
42 | | - 'Upgrade-Insecure-Requests': '1', |
43 | | - 'Sec-Fetch-Dest': 'document', |
44 | | - 'Sec-Fetch-Mode': 'navigate', |
45 | | - 'Sec-Fetch-Site': 'cross-site', |
46 | | - 'Sec-Fetch-User': '?1', |
47 | | - 'Cache-Control': 'max-age=0', |
48 | 49 | } |
49 | 50 |
|
50 | 51 | # ── Helpers ─────────────────────────────────────────────────────────────────── |
@@ -108,6 +109,7 @@ def get_github_file(): |
108 | 109 | } |
109 | 110 |
|
110 | 111 | try: |
| 112 | + # Para la API de GitHub usamos requests normal (no suele tener Cloudflare agresivo para API) |
111 | 113 | r = requests.get(url, headers=headers, timeout=10) |
112 | 114 |
|
113 | 115 | if r.status_code == 401: |
@@ -176,6 +178,7 @@ def push_to_github(item, summary_text, categoria): |
176 | 178 | "sha": sha |
177 | 179 | } |
178 | 180 | try: |
| 181 | + # También usamos requests normal para la API de GitHub |
179 | 182 | r = requests.put(url, headers={ |
180 | 183 | "Authorization": f"Bearer {token}", |
181 | 184 | "Accept": "application/vnd.github.v3+json" |
@@ -276,9 +279,10 @@ def send_to_telegram(message): |
276 | 279 |
|
277 | 280 | def scrape_rss_feed(url, source_name, limit=5): |
278 | 281 | try: |
279 | | - # Usamos una sesión para mantener consistencia en los headers |
280 | | - session = requests.Session() |
281 | | - r = session.get(url, headers=HEADERS, timeout=15) |
| 282 | + # Usamos el scraper de Cloudflare para TODAS las fuentes RSS |
| 283 | + r = scraper.get(url, headers=HEADERS, timeout=15) |
| 284 | + |
| 285 | + logger.info(f"FETCH {source_name}: Status {r.status_code}") |
282 | 286 |
|
283 | 287 | if r.status_code != 200: |
284 | 288 | logger.error(f"RSS Error ({source_name}): Status {r.status_code}") |
|
0 commit comments