-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.py
More file actions
executable file
·59 lines (43 loc) · 1.67 KB
/
get.py
File metadata and controls
executable file
·59 lines (43 loc) · 1.67 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
#!./.venv/bin/python3
import requests
import argparse
import os
file_path = "session.id"
input_directory = "./input/"
def get_session_id():
try:
with open(file_path, "r") as file:
session_id = file.readline().strip()
return session_id
except FileNotFoundError:
print(f"Error: El archivo '{file_path}' no existe.")
return None
except Exception as e:
print(f"Error al leer el archivo '{file_path}': {e}")
return None
def download_file_with_sessionid(url, sessionid, output_file):
cookies = {"session": sessionid}
try:
response = requests.get(url, cookies=cookies, stream=True)
response.raise_for_status()
with open(output_file, "wb") as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Archivo descargado exitosamente como: {output_file}")
except requests.exceptions.RequestException as e:
print(f"Error al descargar el archivo: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Número del día a descargar")
parser.add_argument(
"number", type=int, help="Un número entre 1 y 25 (ambos incluidos)."
)
args = parser.parse_args()
# Validar el rango del número
if not (1 <= args.number <= 25):
print("Error: El número debe estar entre 1 y 25 (ambos incluidos).")
exit(1)
url = f"https://adventofcode.com/2024/day/{args.number}/input"
sessionid = get_session_id()
output_file = f"day{args.number}"
final_path = os.path.join(input_directory, output_file)
download_file_with_sessionid(url, sessionid, final_path)