-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (40 loc) · 1.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import requests
from rich import print
from datetime import datetime
from config import api_key
def display_current_weather(city):
""" Displays the current weather """
api_url = f"https://api.shecodes.io/weather/v1/current?query={city}&key={api_key}"
response = requests.get(api_url)
current_weather_data = response.json()
current_weather_city = current_weather_data['city']
current_weather_temperature = round(current_weather_data['temperature']['current'])
print(f"The temperature in {current_weather_city} is {current_weather_temperature}°C")
def display_forecast_weather(city_name):
""" Display the weather forecast of a city """
api_url = f"https://api.shecodes.io/weather/v1/forecast?query={city_name}&key={api_key}"
response = requests.get(api_url)
forecast_weather_data = response.json()
print("\n[bold yellow]Forecast:[/bold yellow]")
for day in forecast_weather_data['daily']: # Loop through the daily list "[]"
timestamp = day['time']
date = datetime.fromtimestamp(timestamp) # convert timestamp to date time
formatted_day = date.strftime("%A") # %A is the code that represents how we want the date format style
temperature = day['temperature']['day']
if date.date() != datetime.today().date():
print(f"{formatted_day}: {round(temperature)}°C")
def credit():
""" Displays credit message """
print("\n[italic]This app was built by Isabela Alcantara[/italic]")
def welcome():
""" Displays welcome message """
print("[purple bold]Welcome to my Weather App[/purple bold]")
welcome()
city_name = input("Enter a city: ")
city_name = city_name.strip()
if city_name:
display_current_weather(city_name)
display_forecast_weather(city_name)
credit()
else:
print("Please try again with a city")