Dharsan Decodes Data

(Eng) S5 E4.2. Python essentials for DE II

import requests

url = "https://api.weather.com/v1/current" # Example URL

try:

response = requests.get(url, timeout=5) # Stop waiting after 5 seconds

response.raise_for_status() # Automatically trigger an error if status isn't 200

data = response.json()

print("Data fetched successfully!")

except requests.exceptions.HTTPError as err:

print(f"The Server had an issue: {err}")

except requests.exceptions.ConnectionError:

print("The Internet is down! Checking again in 5 minutes...")

except Exception as e:

print(f"Something weird happened: {e}")

import os

from dotenv import load_dotenv

load_dotenv() # Load the secrets from the .env file

api_key = os.getenv("WEATHER_API_KEY")

print(f"Using API Key: {api_key[:4]}****") # Only print a tiny bit for safety