Skip to content

Instantly share code, notes, and snippets.

@PyDann PyDann/Forecast.py Secret

Last active Aug 29, 2015
Embed
What would you like to do?
Forecast
# -*- coding: utf-8 -*-
import urllib2, json, location, datetime
def place():
location.start_updates()
address_dict = location.get_location()
location.stop_updates()
latitude = address_dict['latitude']
longitude = address_dict['longitude']
api(latitude, longitude)
def api(latitude, longitude):
url = 'http://api.openweathermap.org/data/2.5/forecast/daily?lat={0}&lon={1}&mode=json&units=imperial' .format (latitude, longitude)
api = urllib2.urlopen(url)
forecastapi = json.load(api)
url = 'http://api.openweathermap.org/data/2.5/weather?lat={0}&lon={1}&mode=json&units=imperial' .format (latitude, longitude)
api = urllib2.urlopen(url)
weatherapi = json.load(api)
forecast(weatherapi, forecastapi)
def forecast(weatherapi, forecastapi):
temperature = str(round(weatherapi['main']['temp'], 0))
low = str(round(weatherapi['main']['temp_min'], 0))
high = str(weatherapi['main']['temp_max'])
description = weatherapi['weather'][0]['main']
print (description + '\n' + temperature[:2] + \n' + high[:2] + '▴ \n' + low[:2] + '▾ \n')
today = datetime.datetime.now()
week = [(today + datetime.timedelta(days=i)).strftime('%A') for i in xrange(7)]
print 'Your 7-Day Forecast:' + '\n'
for day in week:
temp = str(round(forecastapi['list'][week.index(day)]['temp']['day'],0))
desc = forecastapi['list'][week.index(day)]['weather'][0]['main']
print day[:2] + '|' + temp[:2] + '° ' + desc
place()
@cclauss

This comment has been minimized.

Copy link

cclauss commented Feb 13, 2014

Very nice!!

get_location() must be called after start_updates() and before stop_updates().

These two lines should be reversed:

    location.stop_updates()
    address_dict = location.get_location()

Getting location should be a standalone function that can be reused with minimal modification in multiple apps.


description = weatherapi['weather'][1]['main']

Replace the "1" with zero for the first or -1 for the last item in the weather list. The "1" generated an index out of range exception here because there was only one element in the weather list.

@PyDann

This comment has been minimized.

Copy link
Owner Author

PyDann commented Feb 13, 2014

Thanks. Fixed.

@cclauss

This comment has been minimized.

Copy link

cclauss commented Feb 13, 2014

How well does this app deal with temperatures like 101 and -12??

@PyDann

This comment has been minimized.

Copy link
Owner Author

PyDann commented Feb 13, 2014

Because you have asked, I assume you found something? The only issue might be when 'round' gets ahold of it. I'll put in a mock location with - degrees.

The script isn't done. I want to put in the ability to use metric v imperial depending on country(easy). The next major step is going to be when I make an interface in python. I don't even really know where to start except eyeballing a bunch of existing projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.