Last active
August 29, 2015 14:21
-
-
Save AlexLamson/8896c093d2caaa9c1ddc to your computer and use it in GitHub Desktop.
Little python script that prints what to wear by checking the weather in your location
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
import json | |
import urllib2 | |
import sys | |
def internet_on(): | |
try: | |
response=urllib2.urlopen('http://173.194.204.105',timeout=1) | |
return True | |
except urllib2.URLError as err: pass | |
return False | |
if(not internet_on()): | |
print "Cannot connect - internet is disconnected" | |
sys.exit() | |
ip = urllib2.urlopen('http://ip.42.pl/raw').read() | |
jsondata = json.load(urllib2.urlopen('http://freegeoip.net/json/{0}'.format(ip))) | |
city = jsondata['city'] | |
state = jsondata['region_code'] | |
url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q={0},{1}&mode=json&units=imperial&cnt=1'.format(city,state) | |
try: | |
response = urllib2.urlopen(url) | |
except urllib2.URLError as err: | |
print "weather server failed" | |
sys.exit() | |
jsondata = json.loads(response.read()) | |
wind_speed = float(jsondata['list'][0]['speed']) | |
is_windy = (wind_speed > 15) | |
temps = jsondata['list'][0]['temp'] | |
day_temp = temps['day'] | |
if day_temp <= 40: | |
print "Wear a winter coat" | |
elif day_temp <= 45: | |
if is_windy: | |
print "Wear a jacket" | |
else: | |
print "Wear a sweater or a jacket" | |
elif day_temp <= 60: | |
if is_windy: | |
print "Wear a wind breaker" | |
else: | |
print "Wear a pullover or a light jacket" | |
elif day_temp <= 67: | |
if is_windy: | |
print "Wear a wind breaker" | |
else: | |
print "Wear a long-sleeved shirt" | |
else: | |
print "Wear a t-shirt" | |
def isRain(): | |
weather_types = jsondata['list'][0]['weather'] | |
for weather_type in weather_types: | |
if weather_type['main'] == 'Rain': | |
return True | |
return False | |
if(isRain()): | |
mm_of_rain = float(jsondata['list'][0]['rain']) | |
if mm_of_rain >= 5: | |
print "Bring an umbrella" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment