SprinklerStudio/weather/WeatherUtils.py

34 lines
1022 B
Python
Raw Normal View History

2013-05-19 20:55:06 -07:00
# Create your views here.
import json
import urllib2
from django.core.cache import cache
from django.http import HttpResponse
from weather.models import Location
weatherBaseURL = "http://api.openweathermap.org/data/2.5/weather?"
weatherFormat = "units=metric&"
weatherURL = weatherBaseURL + weatherFormat
def currentTemp():
#cache.clear()
latitude = Location.objects.all()[0].latitude
longitude = Location.objects.all()[0].longitude
queryURL = weatherURL + "lat=" + str(latitude) + "&lon=" + str(longitude)
response = urllib2.urlopen(queryURL)
decoder = json.JSONDecoder()
2013-05-21 04:23:53 -07:00
temperature = decoder.decode(response.read())["main"]["temp"]
2013-05-19 20:55:06 -07:00
2013-05-21 04:23:53 -07:00
return (str(temperature) + " Degrees")
2013-05-19 20:55:06 -07:00
2013-05-21 04:23:53 -07:00
def currentTempByLatLon(latitude, longitude):
queryURL = weatherURL + "lat=" + str(latitude) + "&lon=" + str(longitude)
response = urllib2.urlopen(queryURL)
decoder = json.JSONDecoder()
temperature = decoder.decode(response.read())["main"]["temp"]
return (str(temperature) + " Degrees")