-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditions.py
61 lines (51 loc) · 1.92 KB
/
conditions.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
58
59
60
61
import requests
from haversine import haversine
from config import api_key, home
LOC_HOME = [float(x) for x in home.split(",")]
def get_loc_weather(loc):
degree_sign = "\N{DEGREE SIGN}"
condition_url = f"https://api.aerisapi.com/observations/{loc}?&format=json&filter=allstations&limit=1&{api_key}"
resp = requests.get(condition_url).json()
observation = resp["response"]["ob"]
condition = observation["weatherShort"]
temp_cel = str(observation["tempC"]) + degree_sign + "C"
humidity = str(observation["humidity"]) + " %"
wind_spe = str(observation["windKPH"]) + " kmph"
wind_dir = str(observation["windDirDEG"]) + degree_sign
full_location = str(resp["response"]["place"]["name"]).capitalize()
print(
f"The current conditions in {full_location} are:",
f"Temeprature : {temp_cel}",
f"Condition : {condition}",
f"Humidity : {humidity}",
f"Wind Speed : {wind_spe}",
f"Wind Dir. : {wind_dir}",
sep="\n"
)
def get_weather(loc):
co_ors = []
index = 1
lookup_url = f"https://api.aerisapi.com/places/search?query=name:{loc}&limit=10&{api_key}"
geolookup = requests.get(lookup_url)
places = geolookup.json()["response"]
if not places:
print("Location lookup failed")
return
for place in places:
coor = [place["loc"]["lat"], place["loc"]["long"]]
co_ors.append(coor)
how_far = round(haversine(coor, LOC_HOME), ndigits=2)
city = place["place"]["name"]
country = place["place"]["countryFull"]
print(index, city, "({})".format(country), how_far, "km")
index += 1
choice = int(input("Enter location: "))
get_loc_weather(co_ors[choice - 1])
def conditions():
selection = input("Enter City Name:")
if selection.lower() == "h":
get_loc_weather(home)
else:
get_weather(selection)
if __name__ == "__main__":
conditions()