-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (38 loc) · 1.58 KB
/
main.py
File metadata and controls
48 lines (38 loc) · 1.58 KB
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
import datetime as dt
import requests
# defining the Base URL, API, and destination city
BASE_URL = 'http://api.openweathermap.org/data/2.5/weather?'
API_KEY = open('API_KEY', 'r').read()
CITY = input(str('Which city are you looking for?'))
# defining the access URL structure
URL = BASE_URL + "appid=" + API_KEY + '&q=' + CITY
# reaching the API
response = requests.get(URL).json()
# converting kelvin temperature to celsius
def kelvinToCelsius(kelvin):
return kelvin - 273.15
# converting celsius temperature to fahrenheit
def kelvinToFahrenheit(kelvin):
return kelvin * 1.8 - 459.67
# defining object to be pulled from the API
temp_kelvin = response['main']['temp']
temp_cel = kelvinToCelsius(temp_kelvin)
humidity = response['main']['humidity']
feels_Like = response['main']['feels_like']
feelsLikeCel = kelvinToCelsius(feels_Like)
# desc = {response['weather']['description']}
country = response['sys']['country']
sunrise_time = dt.datetime.utcfromtimestamp(response['sys']['sunrise'] + response['timezone'])
sunset_time = dt.datetime.utcfromtimestamp(response['sys']['sunset'] + response['timezone'])
def recommendation(temp):
if temp < 18:
print("we recommend getting a coat")
else:
print("it's nice outside :)")
# Printing the desired info from the API
print(f'Below are the stats for {CITY} in {country}')
print(f"The temperature in Celsius is: {temp_cel:.2f}°C")
print(f'But it actually feels like: {feelsLikeCel:.2f}°C')
print(f"The humidity is: {humidity}%")
print(f"The sun sets in {CITY} at {sunset_time}")
print(f"The sun rises in {CITY} at {sunrise_time}")