-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (107 loc) · 4.09 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#Weather Forecasting Website (backend)
print("Weather Forecasting Website Application")
# give access to the config file
import config
# import necessary libraries
import requests
import json
import ipinfo
from geopy.geocoders import Nominatim
# obtain api_key and api_host values from the config file
APIKey = config.api_key
API_HOST = config.api_host
# calling the Nominatim tool
loc = Nominatim(user_agent="GetLoc")
# entering the location name
getLoc = loc.geocode(input("\nWhat City Do You Live In? "))
# printing address
print(getLoc.address)
# Weather Options
def weatherGet1(lat,lon):
"""3 hour interval - 5 day forecast for a given lat/lon"""
"""remember to use enviroment variables to hide api keys 341f6c3c6fmsh324475775f8beabp1ada0fjsn7d3aee0230ad"""
url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/3hourly"
querystring = {"lat":lat,"lon":lon,"units":"imperial"}
headers = {
"X-RapidAPI-Key": APIKey,
"X-RapidAPI-Host": API_HOST
}
response = requests.request("GET", url, headers=headers, params=querystring)
parsed = json.loads(response.text)
print(parsed)
def weatherGet2(lat,lon):
'''Returns the current (most recent) weather observation of a given location'''
"""remember to use enviroment variables to hide api keys 341f6c3c6fmsh324475775f8beabp1ada0fjsn7d3aee0230ad"""
url = "https://weatherbit-v1-mashape.p.rapidapi.com/current"
querystring = {"lon":lon,"lat":lat,"units":"imperial"}
headers = {
"X-RapidAPI-Key": APIKey,
"X-RapidAPI-Host": API_HOST
}
response = requests.request("GET", url, headers=headers, params=querystring)
parsed = json.loads(response.text)
print(parsed)
def weatherGet3 (lat,lon):
url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/minutely"
querystring = {"lat":lat,"lon":lon,"units":"imperial"}
headers = {
"X-RapidAPI-Key": APIKey,
"X-RapidAPI-Host": API_HOST
}
response = requests.request("GET", url, headers=headers, params=querystring)
parsed = json.loads(response.text)
print(parsed)
def weatherGet4(lat,lon):
url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/daily"
querystring = {"lat":lat,"lon":lon,"units":"imperial"}
headers = {
"X-RapidAPI-Key": APIKey,
"X-RapidAPI-Host": API_HOST
}
response = requests.request("GET", url, headers=headers, params=querystring)
parsed = json.loads(response.text)
print(parsed)
def weatherGet5(lat,lon,hours):
url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/hourly"
querystring = {"lat":lat,"lon":lon,"hours":hours,"units":"imperial"}
headers = {
"X-RapidAPI-Key": APIKey,
"X-RapidAPI-Host": API_HOST
}
response = requests.request("GET", url, headers=headers, params=querystring)
parsed = json.loads(response.text)
print(parsed)
def weatherGet6(lat,lon):
url = "https://weatherbit-v1-mashape.p.rapidapi.com/alerts"
querystring = {"lat":lat,"lon":lon,"units":"imperial"}
headers = {
"X-RapidAPI-Key": APIKey,
"X-RapidAPI-Host": API_HOST
}
response = requests.request("GET", url, headers=headers, params=querystring)
parsed = json.loads(response.text)
print(parsed)
print("\n1. 5 day forecast")
print("\n2. Current Weather Data of a location")
print("\n3. 1 Hour / Minutely Forecast")
print("\n4. 16 Day Forecast")
print("\n5. 120 Hour Forecast")
print("\n6. Severe Weather Alerts")
choice = int(input("\nEnter your choice: "))
while choice <1 or choice >6:
choice = int(input("\nEnter a valid choice: "))
if choice == 1:
weatherGet1(getLoc.latitude,getLoc.longitude)
elif choice == 2:
weatherGet2(getLoc.latitude,getLoc.longitude)
elif choice == 3:
weatherGet3(getLoc.latitude,getLoc.longitude)
elif choice == 4:
weatherGet4(getLoc.latitude,getLoc.longitude)
elif choice == 5:
hours = int(input("\nHow many hours do you want to see into the future? "))
while hours < 0 or hours > 120:
hours = int(input("\nEnter a valid number of hours: "))
weatherGet5(getLoc.latitude,getLoc.longitude,hours)
else:
weatherGet6(getLoc.latitude,getLoc.longitude)