Skip to content

Commit

Permalink
Added 404 response capture and fixed typing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
alvesvaren committed Dec 3, 2020
1 parent 029e1fa commit 71e5503
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions aoc/_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union
import requests
from pathlib import Path
from datetime import datetime
Expand All @@ -6,7 +7,7 @@
_YEAR_FILE_NAME = "year.txt"


def _set_read_file(filename: str, default: str = None) -> str:
def _set_read_file(filename: str, default: str = None) -> Union[str, None]:
try:
with open(filename) as file:
return file.read()
Expand All @@ -30,7 +31,7 @@ def _set_read_file(filename: str, default: str = None) -> str:
YEAR = _set_read_file(
_YEAR_FILE_NAME,
str(datetime.now().year))
YEAR = YEAR.strip()
YEAR = int(YEAR.strip())

def get_input(day: int, year: int = YEAR, overwrite: bool = False):
"""
Expand All @@ -49,9 +50,11 @@ def get_input(day: int, year: int = YEAR, overwrite: bool = False):
data = None
if not data:
response = requests.get(
f"https://adventofcode.com/{year}/day/{day}/input",
cookies={"session": SESSION})
f"https://adventofcode.com/{year}/day/{day}/input",
cookies={"session": SESSION})
if not response.ok:
if response.status_code == 404:
raise FileNotFoundError(response.text)
raise RuntimeError(f"Request failed, code: {response.status_code}, message: {response.content}")
data = _set_read_file(
file_name,
Expand Down

0 comments on commit 71e5503

Please sign in to comment.