-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_data.py
More file actions
120 lines (92 loc) · 3.34 KB
/
Copy pathupdate_data.py
File metadata and controls
120 lines (92 loc) · 3.34 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
import shutil
import tempfile
import zipfile
from pathlib import Path
from dashboard.config import TABELAS_F1
BASE_DIR = Path(__file__).resolve().parent
DATA_DIR = BASE_DIR / "dados f1"
DATASET_SLUG = "jtrotman/formula-1-race-data"
DOTENV_PATH = BASE_DIR / ".env"
def _load_env_file() -> None:
if DOTENV_PATH.exists():
try:
from dotenv import load_dotenv
except ImportError as exc:
raise RuntimeError(
"Dependencia 'python-dotenv' nao instalada. "
"Rode `python -m pip install -r requirements.txt`."
) from exc
load_dotenv(DOTENV_PATH, override=True)
def _configure_kaggle_auth() -> None:
token = os.getenv("KAGGLE_API_TOKEN", "").strip()
if token:
os.environ["KAGGLE_API_TOKEN"] = token
return
raise RuntimeError(
"Credencial Kaggle nao encontrada. Configure `KAGGLE_API_TOKEN` no `.env`."
)
def _build_kaggle_api():
try:
from kaggle.api.kaggle_api_extended import KaggleApi
except ImportError as exc:
raise RuntimeError(
"Dependencia 'kaggle' nao instalada. "
"Rode `python -m pip install -r requirements.txt`."
) from exc
api = KaggleApi()
try:
api.authenticate()
except Exception as exc:
raise RuntimeError(
"Falha ao autenticar na Kaggle. Verifique se `KAGGLE_API_TOKEN` no `.env` "
"esta valido e atualizado."
) from exc
return api
def _find_downloaded_zip(download_dir: Path) -> Path:
zip_files = sorted(download_dir.glob("*.zip"))
if not zip_files:
raise RuntimeError("Nenhum arquivo ZIP foi baixado pela API da Kaggle.")
return zip_files[0]
def _expected_csv_files() -> set[str]:
return {config["arquivo"] for config in TABELAS_F1.values()}
def update_f1_data() -> None:
"""Baixa e atualiza os arquivos CSV mais recentes do dataset da Kaggle."""
_load_env_file()
_configure_kaggle_auth()
if not DATA_DIR.exists():
DATA_DIR.mkdir(parents=True, exist_ok=True)
print(f"Baixando dataset {DATASET_SLUG}...")
api = _build_kaggle_api()
with tempfile.TemporaryDirectory() as tmp_dir:
temp_dir = Path(tmp_dir)
extracted_dir = temp_dir / "extracted"
api.dataset_download_files(
DATASET_SLUG,
path=str(temp_dir),
force=True,
unzip=False,
)
zip_filename = _find_downloaded_zip(temp_dir)
print(f"Extraindo {zip_filename.name}...")
with zipfile.ZipFile(zip_filename, "r") as zip_ref:
zip_ref.extractall(extracted_dir)
arquivos_csv = sorted(extracted_dir.rglob("*.csv"))
if not arquivos_csv:
raise RuntimeError("O ZIP baixado nao contem arquivos CSV.")
for csv_path in arquivos_csv:
shutil.copy2(csv_path, DATA_DIR / csv_path.name)
ausentes = sorted(
arquivo for arquivo in _expected_csv_files() if not (DATA_DIR / arquivo).exists()
)
if ausentes:
raise RuntimeError(
"Atualizacao incompleta. Arquivos obrigatorios ausentes em `dados f1/`: "
+ ", ".join(ausentes)
)
print(
f"Atualizacao concluida. {len(arquivos_csv)} arquivo(s) CSV foram copiados para "
f"'{DATA_DIR.name}/'."
)
if __name__ == "__main__":
update_f1_data()