-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClearSky.py
99 lines (82 loc) · 2.82 KB
/
ClearSky.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
'''
ClearSky
Version 2
Created by Marissa Klein, Wellesley College 2022
Intended use is getting evening forecast for the next week
'''
import requests
import json
from geopy.geocoders import Nominatim
class ClearSky:
def __init__(self):
pass
def locationGet(self,loc):
'''
Gets latitude and longitude of a specific location.
args:
loc(str) must be a valid city and state/country
raises:
none
returns:
latitude and longitude as a tuple
'''
self.loc = loc
app = Nominatim(user_agent="ClearSky")
location = app.geocode(loc).raw
latitude = location['lat']
longitude = location['lon']
location = (latitude, longitude)
return location
def URLRet(self,loc):
'''
Retrieves proper NWS API URL.
args:
loc(str) must be a valid city and state/country
raises:
none
returns:
NWS weather JSON data for a specific location
'''
self.loc = loc
coords = self.locationGet(loc)
lat = coords[0]
long = coords[1]
#First API Call
response = requests.get('https://api.weather.gov/points/'+lat+','+long)
json_data = json.loads(response.text)
#Second API Call
url = json_data['properties']['forecast']
forecast = requests.get(url)
forecast_data = json.loads(forecast.text)
return forecast_data
def getForecast(self,loc):
'''
Gets forecast for the next week's evenings.
args:
loc(str) must be a valid city and state/country
raises:
none
returns:
Detailed forecast of the next seven nights.
'''
self.loc = loc
forecast = self.URLRet(loc)
nights = []
nightFor = []
data_len=len(forecast['properties']['periods'])
#Finds the data for nights only
for x in range(data_len):
keyWord = forecast['properties']['periods'][x]['name']
checkOne = keyWord.find('night')
checkTwo = keyWord.find('Night')
if checkOne == -1 and checkTwo == -1:
pass
else:
nights.append(x)
#Pulls the detailed forecast for the identified entries
for x in nights:
name = forecast['properties']['periods'][x]['name']
nightSky = name+": "+forecast['properties']['periods'][x]['detailedForecast']
nightFor.append(nightSky)
#Prints forecast
return nightFor