-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
toravest
committed
Mar 20, 2025
1 parent
c92af28
commit b89aeb2
Showing
3 changed files
with
92 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Import of needed libaries | ||
| import requests | ||
| import os | ||
| from dotenv import load_dotenv | ||
|
|
||
|
|
||
| # only for change unix to date for plot | ||
| # import matplotlib.pyplot as plt | ||
| # import datetime | ||
| # import time | ||
|
|
||
| load_dotenv() | ||
|
|
||
| # Gets the key, from my env file | ||
| API_KEY = os.getenv("API_KEY") | ||
|
|
||
| country_code = "NO" | ||
|
|
||
| # Temporarily standard times | ||
| start_date = 1735686000 | ||
| end_date = 1740009323 | ||
|
|
||
| # Gets the data from the API - openweathermap.org | ||
| def fetch_data(start_date, end_date, city_name): | ||
|
|
||
|
|
||
| # f-string url, to add the "custom" variables to the API-request | ||
| url = f"https://history.openweathermap.org/data/2.5/history/city?q={city_name},{"NO"}&units=metric&type=hour&start={start_date}&end={end_date}&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() | ||
|
|
||
| print("Data fetch: ok") | ||
| return data | ||
|
|
||
| else: | ||
| # If html status code != 200, print the status code | ||
| print("Failed to fetch data from API. Status code:", response.status_code) | ||
|
|
||
|
|
||
| # myData = get_data(start_date, end_date) | ||
|
|
||
| # if myData is not None: | ||
| # # Empty dict for temperature | ||
| # temperature = {} | ||
| # # Iterates through myData dict, and add the dt (unix_time) and temp to temperature dict | ||
| # if "list" in myData: | ||
| # for item in myData["list"]: | ||
| # if "main" in item and "temp" in item["main"]: | ||
| # temperature[item["dt"]] = (item["main"]["temp"]) | ||
|
|
||
| # # print(temperature) | ||
|
|
||
| # x_temp = [datetime.datetime.fromtimestamp(ts) for ts in temperature.keys()] | ||
| # # A test plot, to get a visual look of the data | ||
| # # x_temp = list(temperature.keys()) | ||
| # y_temp = list(temperature.values()) | ||
|
|
||
| # plt.plot(x_temp, y_temp) | ||
| # plt.xlabel('Unix Time') | ||
| # plt.ylabel('Temperature (°C)') | ||
| # plt.title('Temperature over Time') | ||
| # plt.grid() | ||
| # plt.show() | ||
|
|
||
| # else: | ||
| # print("No data to process.") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import json | ||
| import os | ||
|
|
||
| def write_data(data, filename): | ||
| # Ensure the 'output_stedsdata' folder exists inside the 'data' folder at the root of the project | ||
| script_dir = os.path.dirname(os.path.abspath(__file__)) # Get the directory of the script | ||
| project_root = os.path.abspath(os.path.join(script_dir, os.pardir, os.pardir)) # Navigate to the root of the project | ||
| data_dir = os.path.join(project_root, 'data', 'output_stedsdata') | ||
| os.makedirs(data_dir, exist_ok=True) # Creates 'data/output_stedsdata' folder if it doesn't exist | ||
|
|
||
| # Write the JSON data to a file inside the 'output_stedsdata' folder | ||
| file_path = os.path.join(data_dir, f'data_{filename}.json') # Creates 'data/output_stedsdata/data_{filename}.json' | ||
|
|
||
| # Opens and write the data to a json file | ||
| with open(file_path, 'w') as json_file: | ||
| json.dump(data, json_file, indent=4) | ||
|
|
||
| # Prints when succed | ||
| print(f"Data has been written to {file_path}") |