diff --git a/flake.lock b/flake.lock index 344deab..dbf2fd5 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1760524057, - "narHash": "sha256-EVAqOteLBFmd7pKkb0+FIUyzTF61VKi7YmvP1tw4nEw=", + "lastModified": 1770197578, + "narHash": "sha256-AYqlWrX09+HvGs8zM6ebZ1pwUqjkfpnv8mewYwAo+iM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "544961dfcce86422ba200ed9a0b00dd4b1486ec5", + "rev": "00c21e4c93d963c50d4c0c89bfa84ed6e0694df2", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index d0d3e32..ab6e3c9 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,7 @@ virtualenv requests pandas + types-requests # Optional: Add other helpful dev tools outside of the python-pkgs set if needed # (e.g., git, pre-commit, bash-completion) ])) diff --git a/src/dad_joke.py b/src/dad_joke.py new file mode 100644 index 0000000..f7d566e --- /dev/null +++ b/src/dad_joke.py @@ -0,0 +1,35 @@ +import requests + +def get_joke(): + try: + api_url = "https://icanhazdadjoke.com/" + + # VERY IMPORTANT: request JSON response + headers = { + "Accept": "application/json", + "User-Agent": "python-script (learning requests)" + } + + # Send GET request + response = requests.get(api_url, headers=headers) + + if response.status_code == 200: + joke_data = response.json() + + print("Joke:") + print(joke_data.get("joke", "No joke found.")) + + else: + print(f"Error: {response.status_code}") + print(response.text) # useful debugging + + except requests.exceptions.RequestException as e: + print(f"Network error: {e}") + except ValueError as e: + # This is the JSON decode error you were hitting + print("Server did not return valid JSON!") + print("Raw response was:") + print(response.text) + +get_joke() +