Skip to content

Commit

Permalink
function to fetch current data
Browse files Browse the repository at this point in the history
  • Loading branch information
hannhegg committed Mar 26, 2025
1 parent 68063bb commit baf7188
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/my_package/fetch_current_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Import of needed libaries
import requests
import os
from dotenv import load_dotenv

load_dotenv()

# Gets the key, from my env file
API_KEY = os.getenv("API_KEY")

# city_name = "Trondheim"
country_code = "NO"


# Gets the data from the API - openweathermap.org
def fetch_current_data(city_name):


# f-string url, to add the "custom" variables to the API-request
url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name},NO&units=metric&appid={API_KEY}"

# Saves the API-request for the url
response = requests.get(url)

# Checks if the status code is OK
if response.status_code == 200:

# Converts the data into json
data = response.json()
folder = "../data/output_current_data"

print("Data fetch: ok")
return data, folder


else:
# If html status code != 200, print the status code
print("Failed to fetch data from API. Status code:", response.status_code)

0 comments on commit baf7188

Please sign in to comment.