-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
101 lines (73 loc) · 3.88 KB
/
weather.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#App will ask city
#Find city and use API to find
#Finds Tempatures for up to 6 Days
import requests
count = 0
works = 0
degree_sign = u"\N{DEGREE SIGN}" #Degree Sign
def celcToFar(num): #Converts Celsius to Fahrenheit
far = (num * 9/5)+32
return int(far)
wApi = "https://www.metaweather.com/api/location/search/" #MetaWeather API
area = input("Please Type in Closest Major City: ") #get Major city from user
location = requests.get(wApi + "?query="+ area)
# print(location.json()) #Prints Location.Json
jsonlocation = location.json()
while works == 0: #If Name of Major City is Invalid
try:
woeid = jsonlocation[0]['woeid'] #Getting WoeID
City = jsonlocation[0]['title'] #Getting Full Name of City
works = 1 #Sets "Works" to 1 ending and ends loop.
except IndexError:
print("Invalid Major City, Try Again: ")
area = input("Please Type in Closest Major City: ") #get Major city from user
location = requests.get(wApi + "?query="+ area)
jsonlocation = location.json()
if jsonlocation == []: #Checks to see if new city given is valid
print("---------------------------------") #If new City is invalid, Prints Lines and restarts
else:
works=1 #if New City Given is valid, Sets "Works" to 1 ending loop.
woeid = jsonlocation[0]['woeid'] #Getting WoeID
City = jsonlocation[0]['title'] #Getting Full Name of City
print("City: "+ City)
print("Woeid: " + str(woeid))
lApi = "https://www.metaweather.com/api/location/" + str(woeid) #Location API
weather = requests.get(lApi)
jsonWeather = weather.json() #Gathering JSON Data
# day1 = jsonWeather.get('consolidated_weather')[0]
# day1Weather = day1.get('weather_state_name')
# day1Min = day1.get('min_temp')
# day1Max = day1.get('max_temp')
# day1Current = day1.get('the_temp')
# print("Todays Weather will feature: " + day1Weather)
# print("Todays Minimum Temperature will be: " + str(celcToFar(day1Min)) + degree_sign + "F")
# print("Todays Maximum Temperature will be: " + str(celcToFar(day1Max)) + degree_sign + "F")
# print("Todays Current Temperature is : " + str(celcToFar(day1Current)) + degree_sign + "F")
daysDisplayedEntered = input("How Many days would you like to see?: ") #Asks how many days from User
daysDisplayed = int(daysDisplayedEntered)
while int(daysDisplayed) > 6: #Max Days showed from MetaWeather is 6
print("6 is Max, Please try again")
daysDisplayedEntered = input("How Many days would you Like to see?: ")
daysDisplayed = int(daysDisplayedEntered)
for f in jsonWeather.get('consolidated_weather'): #Grabbing Value from Keys
day = jsonWeather.get('consolidated_weather')[count]
dayWeather = day.get('weather_state_name')
dayMin = day.get('min_temp')
dayMax = day.get('max_temp')
dayCurrent = day.get('the_temp')
if count == daysDisplayed: break #Ends program when reaches days they asked for
if count ==0: #1st Day is Current Temperature
print("Todays Weather will feature: " + dayWeather)
print("Todays Minimum Temperature will be " + str(celcToFar(dayMin)) + degree_sign + "F")
print("Todays Maximum Temperature will be " + str(celcToFar(dayMax)) + degree_sign + "F")
print("Todays Current Temperature is " + str(celcToFar(dayCurrent)) + degree_sign + "F")
print("---------------------------------") #Seperators between Texts
count+=1
continue
else:
print("Day " + str(count+1) + ": Weather will feature: " + dayWeather)
print("Day " + str(count+1) + ": Minimum Temperature will be " + str(celcToFar(dayMin)) + degree_sign + "F")
print("Day " + str(count+1) + ": Maximum Temperature will be " + str(celcToFar(dayMax)) + degree_sign + "F")
print("Day " + str(count+1) + ": Average Temperature will be " + str(celcToFar(dayCurrent)) + degree_sign + "F")
print("---------------------------------")
count+= 1