|
|
|
@@ -3,89 +3,89 @@ import time
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
# Class to handle logging to multiple files
|
|
|
|
# Klasse om te loggen naar meerdere bestanden
|
|
|
|
class Tee:
|
|
|
|
class Tee:
|
|
|
|
def __init__(self, *files):
|
|
|
|
def __init__(self, *files):
|
|
|
|
# Initialize with a list of file objects to write to
|
|
|
|
# Initialiseer met een lijst van bestandobjecten om naar te schrijven
|
|
|
|
self.files = files
|
|
|
|
self.files = files
|
|
|
|
|
|
|
|
|
|
|
|
def write(self, obj):
|
|
|
|
def write(self, obj):
|
|
|
|
# Write the given object to all files and flush the output
|
|
|
|
# Schrijf het gegeven object naar alle bestanden en flush de output
|
|
|
|
for f in self.files:
|
|
|
|
for f in self.files:
|
|
|
|
f.write(obj)
|
|
|
|
f.write(obj)
|
|
|
|
f.flush()
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
def flush(self):
|
|
|
|
# Flush the output for all files
|
|
|
|
# Flush de output voor alle bestanden
|
|
|
|
for f in self.files:
|
|
|
|
for f in self.files:
|
|
|
|
f.flush()
|
|
|
|
f.flush()
|
|
|
|
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
def close(self):
|
|
|
|
# Close all files
|
|
|
|
# Sluit alle bestanden
|
|
|
|
for f in self.files:
|
|
|
|
for f in self.files:
|
|
|
|
f.close()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
# Function to get the latitude and longitude of a city using OpenWeatherMap API
|
|
|
|
# Functie om de breedte- en lengtegraad van een stad op te halen met de OpenWeatherMap API
|
|
|
|
def get_coordinates(city_name):
|
|
|
|
def get_coordinates(city_name):
|
|
|
|
api_key = 'cf2b92cba5cdb89baccb2fe05cacb3a5' # API key for OpenWeatherMap
|
|
|
|
api_key = 'cf2b92cba5cdb89baccb2fe05cacb3a5' # API-sleutel voor OpenWeatherMap
|
|
|
|
base_url = 'http://api.openweathermap.org/geo/1.0/direct' # Base URL for geocoding API
|
|
|
|
base_url = 'http://api.openweathermap.org/geo/1.0/direct' # Basis-URL voor geocoding API
|
|
|
|
params = {
|
|
|
|
params = {
|
|
|
|
'q': city_name, # City name to query
|
|
|
|
'q': city_name, # Stadnaam om te queryen
|
|
|
|
'appid': api_key # API key parameter
|
|
|
|
'appid': api_key # API-sleutel parameter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = requests.get(base_url, params=params) # Make a GET request to the API
|
|
|
|
response = requests.get(base_url, params=params) # Maak een GET-verzoek naar de API
|
|
|
|
if response.status_code == 200:
|
|
|
|
if response.status_code == 200:
|
|
|
|
data = response.json() # Parse the JSON response
|
|
|
|
data = response.json() # Parse de JSON-respons
|
|
|
|
if data:
|
|
|
|
if data:
|
|
|
|
# Return the latitude and longitude of the first result
|
|
|
|
# Retourneer de breedte- en lengtegraad van het eerste resultaat
|
|
|
|
return data[0]['lat'], data[0]['lon']
|
|
|
|
return data[0]['lat'], data[0]['lon']
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# Print an error message if the city is not found
|
|
|
|
# Print een foutmelding als de stad niet gevonden is
|
|
|
|
print(f"City {city_name} not found.")
|
|
|
|
print(f"Stad {city_name} niet gevonden.")
|
|
|
|
return None, None
|
|
|
|
return None, None
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# Print an error message if the API request fails
|
|
|
|
# Print een foutmelding als het API-verzoek mislukt
|
|
|
|
print(f"Failed to get coordinates for city {city_name}. Error code: {response.status_code}")
|
|
|
|
print(f"Kon de coördinaten voor stad {city_name} niet ophalen. Foutcode: {response.status_code}")
|
|
|
|
return None, None
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
# Function to get the weather data for given coordinates using OpenWeatherMap API
|
|
|
|
# Functie om de weersgegevens voor gegeven coördinaten op te halen met de OpenWeatherMap API
|
|
|
|
def get_weather(lat, lon, city_name):
|
|
|
|
def get_weather(lat, lon, city_name):
|
|
|
|
api_key = 'cf2b92cba5cdb89baccb2fe05cacb3a5' # API key for OpenWeatherMap
|
|
|
|
api_key = 'cf2b92cba5cdb89baccb2fe05cacb3a5' # API-sleutel voor OpenWeatherMap
|
|
|
|
base_url = 'https://api.openweathermap.org/data/2.5/weather' # Base URL for weather API
|
|
|
|
base_url = 'https://api.openweathermap.org/data/2.5/weather' # Basis-URL voor weer API
|
|
|
|
params = {
|
|
|
|
params = {
|
|
|
|
'lat': lat, # Latitude parameter
|
|
|
|
'lat': lat, # Breedtegraad parameter
|
|
|
|
'lon': lon, # Longitude parameter
|
|
|
|
'lon': lon, # Lengtegraad parameter
|
|
|
|
'appid': api_key, # API key parameter
|
|
|
|
'appid': api_key, # API-sleutel parameter
|
|
|
|
'units': 'metric' # Units parameter to get temperature in Celsius
|
|
|
|
'units': 'metric' # Eenheden parameter om temperatuur in Celsius te krijgen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response = requests.get(base_url, params=params) # Make a GET request to the API
|
|
|
|
response = requests.get(base_url, params=params) # Maak een GET-verzoek naar de API
|
|
|
|
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
if response.status_code == 200:
|
|
|
|
data = response.json() # Parse the JSON response
|
|
|
|
data = response.json() # Parse de JSON-respons
|
|
|
|
# Print the weather information
|
|
|
|
# Print de weersinformatie
|
|
|
|
print(f"Weather at coordinates ({lat}, {lon}) in {city_name}:")
|
|
|
|
print(f"Weer op coördinaten ({lat}, {lon}) in {city_name}:")
|
|
|
|
print(f"Temperature: {data['main']['temp']}°C")
|
|
|
|
print(f"Temperatuur: {data['main']['temp']}°C")
|
|
|
|
print(f"Weather: {data['weather'][0]['description']}")
|
|
|
|
print(f"Weer: {data['weather'][0]['description']}")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# Print an error message if the API request fails
|
|
|
|
# Print een foutmelding als het API-verzoek mislukt
|
|
|
|
print(f"Failed to get weather data for coordinates ({lat}, {lon}). Error code: {response.status_code}")
|
|
|
|
print(f"Kon de weersgegevens voor coördinaten ({lat}, {lon}) niet ophalen. Foutcode: {response.status_code}")
|
|
|
|
|
|
|
|
|
|
|
|
# Main function to get city name input from the user and fetch weather data
|
|
|
|
# Hoofdfunctie om de stadnaam van de gebruiker te vragen en de weersgegevens op te halen
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
log_file_path = os.path.join(os.path.dirname(__file__), 'weather.log')
|
|
|
|
log_file_path = os.path.join(os.path.dirname(__file__), 'weather.log')
|
|
|
|
if os.path.exists(log_file_path):
|
|
|
|
if os.path.exists(log_file_path):
|
|
|
|
with open(log_file_path, 'a') as log_file:
|
|
|
|
with open(log_file_path, 'a') as log_file:
|
|
|
|
log_file.write('\n') # Add 2 empty lines before appending new logs
|
|
|
|
log_file.write('\n') # Voeg 2 lege regels toe voordat nieuwe logs worden toegevoegd
|
|
|
|
with open(log_file_path, 'a') as log_file:
|
|
|
|
with open(log_file_path, 'a') as log_file:
|
|
|
|
city_name = input("Enter city name: ") # Prompt the user to enter a city name
|
|
|
|
city_name = input("Voer de naam van de stad in: ") # Vraag de gebruiker om een stadnaam in te voeren
|
|
|
|
lat, lon = get_coordinates(city_name) # Get the coordinates of the city
|
|
|
|
lat, lon = get_coordinates(city_name) # Haal de coördinaten van de stad op
|
|
|
|
if lat is not None and lon is not None:
|
|
|
|
if lat is not None and lon is not None:
|
|
|
|
original_stdout = sys.stdout # Save the original stdout
|
|
|
|
original_stdout = sys.stdout # Sla de originele stdout op
|
|
|
|
tee = Tee(original_stdout, log_file)
|
|
|
|
tee = Tee(original_stdout, log_file)
|
|
|
|
sys.stdout = tee # Redirect stdout to both terminal and log file
|
|
|
|
sys.stdout = tee # Redirect stdout naar zowel terminal als logbestand
|
|
|
|
get_weather(lat, lon, city_name) # Get the weather data for the coordinates
|
|
|
|
get_weather(lat, lon, city_name) # Haal de weersgegevens voor de coördinaten op
|
|
|
|
sys.stdout = original_stdout # Reset stdout to original
|
|
|
|
sys.stdout = original_stdout # Reset stdout naar origineel
|
|
|
|
# Sleep for 5 seconds before exiting to ensure all logs are written
|
|
|
|
# Wacht 5 seconden voordat het script wordt afgesloten om ervoor te zorgen dat alle logs zijn geschreven
|
|
|
|
time.sleep(5)
|
|
|
|
time.sleep(5)
|
|
|
|
tee.close() # Close the Tee object to ensure all files are properly closed
|
|
|
|
tee.close() # Sluit het Tee-object om ervoor te zorgen dat alle bestanden correct worden gesloten
|