Created
February 23, 2014 19:19
-
-
Save bagrow/9175879 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
# weather | |
# Jim Bagrow | |
# Last Modified: 2014-01-01 | |
""" | |
This script requires: | |
1. a weather underground API key | |
http://www.wunderground.com/weather/api | |
put on line 24, and | |
2. LocateMe | |
http://iharder.sourceforge.net/current/macosx/locateme/ | |
a CLI CoreLocation utility for automatically finding your geographic | |
location. This is called on Line 71. | |
""" | |
import os, sys, json, urllib2, argparse | |
def _callWU(w_url): | |
wunder_url = "http://api.wunderground.com/api/<YOURAPIKEY>/%s" % w_url | |
return json.loads( urllib2.urlopen(wunder_url).read() ) | |
def getCurrent(place): | |
try: | |
parsed_json = _callWU('/conditions/q/%s.json' % urllib2.quote(place))['current_observation'] | |
except KeyError: | |
sys.exit("Error, bad place? (place = %s). Exiting..." % place) | |
print 'City: ', parsed_json['display_location']['full'] | |
print 'Conditions: ', parsed_json['weather'] | |
print 'Temperature: ', parsed_json['temperature_string'] | |
print 'Feels like: ', parsed_json['feelslike_string'] | |
print 'Wind: ', parsed_json['wind_string'] | |
print 'Humidity: ', parsed_json[u'relative_humidity'] | |
print 'Hourly Precip:', parsed_json[u'precip_1hr_string'] | |
getSunTimes(place) | |
def getForecast(place): | |
parsed_json = _callWU('/forecast/q/%s.json' % urllib2.quote(place)) | |
try: | |
listF = parsed_json['forecast']['simpleforecast']['forecastday'] # list of forecasts | |
except KeyError: | |
sys.exit("Error, bad place? (place = %s). Exiting..." % place) | |
for F in listF: | |
print F['date']['pretty'] | |
print " High: %s F (%s C)" % (F['high']['fahrenheit'], F['high']['celsius']) | |
print " Low: %s F (%s C)" % (F[ 'low']['fahrenheit'], F[ 'low']['celsius']) | |
print " Conditions: %s" % F['conditions'] | |
def getSunTimes(place): | |
try: | |
parsed_json = _callWU('/astronomy/q/%s.json' % urllib2.quote(place))['sun_phase'] | |
except KeyError: | |
sys.exit("Error, bad place? (place = %s). Exiting..." % place) | |
sunrise_hr = int(parsed_json['sunrise']['hour'] ) | |
sunrise_mn = int(parsed_json['sunrise']['minute']) | |
sunset_hr = int(parsed_json['sunset']['hour'] ) | |
sunset_mn = int(parsed_json['sunset']['minute'] ) | |
print 'Sunrise: ', "%02i:%02i" % (sunrise_hr,sunrise_mn) | |
print 'Sunset: ', "%02i:%02i" % (sunset_hr, sunset_mn ) | |
def getPlace(): | |
lat_lon = os.popen("/Applications/LocateMe -f '{LAT},{LON}'").read().strip() # much faster than whereami | |
return _callWU('/geolookup/q/%s.json' % lat_lon)['location']['zip'] | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("location", nargs='?', help="weather here instead of current location", default=False) | |
parser.add_argument("-f", "--forecast", help="forecast not current conditions", action="store_true") | |
args = parser.parse_args() | |
location = args.location if args.location else getPlace() | |
if args.forecast: | |
getForecast(location) | |
else: | |
getCurrent(location) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment