Created
May 2, 2018 14:15
-
-
Save Siliconrob/45fbe14abb7c62a979505cef3a49a7bc to your computer and use it in GitHub Desktop.
Read Weather Underground for DateTime and return sky condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from pprint import pprint | |
import arrow | |
from WunderWeather import weather | |
def eclipse_conditions(api_key, lat, lng, start_time_utc, end_time_utc): | |
if (start_time_utc > end_time_utc): | |
raise Exception('Start time must be less than end time') | |
extractor = weather.Extract(api_key) | |
location = "{0},{1}".format(lat, lng) | |
date_fmt = "YYYYMMDDhhmm" | |
response = extractor.date(location, start_time_utc.format(date_fmt)) | |
conditions = [] | |
if response.observations is None: | |
return conditions | |
for observation in response.observations: | |
obs = observation.data.utcdate | |
utc_date_str = "{0}{1}{2}{3}{4}".format(obs.year, obs.mon, obs.mday, obs.hour, obs.min) | |
obs_date_utc = arrow.get(utc_date_str, date_fmt) | |
if start_time_utc <= obs_date_utc <= end_time_utc: | |
match_date = obs_date_utc.format("YYYY-MM-DD hhmm") | |
current_condition = match_date, observation.data.conds | |
conditions.append(current_condition) | |
return conditions | |
def print_conditions(conditions): | |
for current_condition in conditions: | |
if current_condition[1] not in ['Clear', 'Unknown']: | |
pprint("OH NOES! {0} sky was {1}".format(current_condition[0], current_condition[1])) | |
else: | |
pprint("YAY! {0} sky was {1}".format(current_condition[0], current_condition[1])) | |
if len(conditions) == 0: | |
pprint("NO OBSERVATIONS") | |
def main(): | |
api_key = 'YOUR API KEY' | |
lat = 32 | |
lng = -97 | |
# Overcast | |
start = arrow.get("1985-03-15 1400", "YYYY-MM-DD hhmm") | |
end = arrow.get("1985-03-15 1500", "YYYY-MM-DD hhmm") | |
conditions = eclipse_conditions(api_key, lat, lng, start, end) | |
print_conditions(conditions) | |
# Unknown | |
start = arrow.get("1995-06-15 1400", "YYYY-MM-DD hhmm") | |
end = arrow.get("1995-06-15 1500", "YYYY-MM-DD hhmm") | |
conditions = eclipse_conditions(api_key, lat, lng, start, end) | |
print_conditions(conditions) | |
# No Observations | |
lat = 36.1699 | |
lng = -115.13 | |
conditions = eclipse_conditions(api_key, lat, lng, start, end) | |
print_conditions(conditions) | |
# San Francisco | |
lat = 37.7749 | |
lng = -122.4194 | |
conditions = eclipse_conditions(api_key, lat, lng, start, end) | |
print_conditions(conditions) | |
# Cleveland 12 hours ago | |
lat = 41.4993 | |
lng = -81.6944 | |
start = arrow.utcnow().shift(hours=-12) | |
end = start.shift(hours=1) | |
conditions = eclipse_conditions(api_key, lat, lng, start, end) | |
print_conditions(conditions) | |
# Cleveland last hour | |
start = arrow.utcnow().shift(hours=-1) | |
end = start.shift(hours=1) | |
conditions = eclipse_conditions(api_key, lat, lng, start, end) | |
print_conditions(conditions) | |
if __name__ == "__main__": | |
print('Starting {0}'.format(str(sys.argv))) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment