-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
58 lines (48 loc) · 1.77 KB
/
Copy pathdata_loader.py
File metadata and controls
58 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import pandas as pd
from datetime import datetime, timedelta
def build_date_range(dias: int) -> tuple[str, str]:
fecha_fin = datetime.today().date()
fecha_inicio = fecha_fin - timedelta(days=dias)
return fecha_inicio.isoformat(), fecha_fin.isoformat()
def fetch_articles(
keywords: list[str],
api_key: str,
fecha_inicio: str,
fecha_fin: str,
idioma: str,
max_articulos: int,
) -> tuple[pd.DataFrame, list[str]]:
"""
Descarga artículos de NewsAPI para cada keyword en el rango de fechas.
Devuelve (DataFrame limpio, lista de errores por keyword).
"""
url = "https://newsapi.org/v2/everything"
articulos_total = []
errores = []
for kw in keywords:
params = {
"q": kw,
"from": fecha_inicio,
"to": fecha_fin,
"language": idioma,
"sortBy": "publishedAt",
"pageSize": max_articulos,
"apiKey": api_key,
}
response = requests.get(url, params=params, timeout=10)
data = response.json()
if data.get("status") != "ok":
errores.append(f"'{kw}': {data.get('message', 'error desconocido')}")
continue
for art in data.get("articles", []):
art["keyword"] = kw
articulos_total.extend(data.get("articles", []))
if not articulos_total:
return pd.DataFrame(), errores
df = pd.DataFrame(articulos_total)
df = df[["title", "source", "publishedAt", "keyword"]].dropna()
df["date"] = pd.to_datetime(df["publishedAt"]).dt.date
df["media"] = df["source"].apply(lambda x: x.get("name", "") if isinstance(x, dict) else "")
df = df.drop_duplicates(subset="title").reset_index(drop=True)
return df, errores