diff --git a/src/my_package/fetch_data.py b/src/my_package/fetch_data.py new file mode 100644 index 0000000..c48d9a4 --- /dev/null +++ b/src/my_package/fetch_data.py @@ -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.") \ No newline at end of file diff --git a/src/my_package/get_data.py b/src/my_package/get_data.py deleted file mode 100644 index a3e94c4..0000000 --- a/src/my_package/get_data.py +++ /dev/null @@ -1,78 +0,0 @@ -# Import of needed libaries -import requests -import os -from dotenv import load_dotenv -import json -import matplotlib.pyplot as plt - -load_dotenv() - -# Gets the key, from my env file -API_KEY = os.getenv("API_KEY") - -country_code = "NO" - -# Temporarily standard times -# start_date = 1742079600 -# end_date = 1742166000 - -# Gets the data from the API - openweathermap.org -def get_data(start_date, end_date): - # User input the city, for the weather - city_name = input("Enter a city in Norway: ") - - # 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() - - # Ensure the 'json' 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', 'json') - os.makedirs(data_dir, exist_ok=True) # Creates 'data/json' folder if it doesn't exist - - # Write the JSON data to a file inside the 'json' folder - file_path = os.path.join(data_dir, f'data_{city_name}.json') # Creates 'data/json/data_{city_name}.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}") - - return data - else: - # If html status code != 200, print the status code - print("Failed to fetch data from API. Status code:", response.status_code) - -# Stores the data in the myData variable -# myData = get_data(start_date, end_date) - -# # 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) - -# # A test plot, to get a visual look of the data -# x_temp = temperature.keys() -# y_temp = temperature.values() - -# plt.plot(x_temp, y_temp) - -# # plt.hist(x_temp, y_temp) -# plt.show() \ No newline at end of file diff --git a/src/my_package/write_data.py b/src/my_package/write_data.py new file mode 100644 index 0000000..233bb91 --- /dev/null +++ b/src/my_package/write_data.py @@ -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}") \ No newline at end of file