67 lines
1.9 KiB
Plaintext
Executable File
67 lines
1.9 KiB
Plaintext
Executable File
from django.shortcuts import render
|
|
|
|
# Create your views here.
|
|
|
|
from django.http import HttpResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
import requests, json
|
|
|
|
|
|
@csrf_exempt
|
|
def index(request):
|
|
# Get Zip code from request
|
|
print("Request=")
|
|
print(request)
|
|
print("Request body=")
|
|
print(request.body)
|
|
|
|
# Get weather string - ZIP is ignored for now
|
|
print("Getting weather...")
|
|
weather_string = getWeather(47715)
|
|
|
|
print("Creating payload...")
|
|
payload = { 'response_type': "in_channel", 'text': weather_string }
|
|
print("payload=")
|
|
print(payload)
|
|
print("dumps(payload)=")
|
|
print(json.dumps(payload))
|
|
|
|
print("Returning payload...")
|
|
return HttpResponse(json.dumps(payload), content_type='application/json')
|
|
#return HttpResponse(data=weather_string, status=status.HTTP_200_OK)
|
|
#return HttpResponse(status=status.HTTP_200_OK)
|
|
#return Response(status=status.HTTP_200_OK)
|
|
|
|
print("Done returning. ;)")
|
|
|
|
|
|
def getWeather(zip_code):
|
|
resp = requests.get("http://api.openweathermap.org/data/2.5/weather?zip=47715,us&units=imperial&APPID=ecf63d9fa178fca8a4ab21b49e9bb456")
|
|
|
|
if resp.status_code != 200:
|
|
# This means something went wrong.
|
|
raise ApiError('GET OpenWeatherMap {}'.format(resp.status_code))
|
|
|
|
response = resp.json()
|
|
|
|
location = response['name']
|
|
|
|
weather = response['weather']
|
|
weather = weather[0]
|
|
weather_main = weather['main']
|
|
weather_desc = weather['description']
|
|
|
|
main = response['main']
|
|
temp = main['temp']
|
|
humidity = main['humidity']
|
|
wind = response['wind']
|
|
wind_speed = wind['speed']
|
|
|
|
temp_units = 'F'
|
|
wind_units = 'mph'
|
|
humidity_units = '%'
|
|
|
|
output = location + " is " + str(temp) + temp_units + " and " + weather_main + " with a wind speed of " + str(wind_speed) + wind_units + " and humidity of " + str(humidity) + humidity_units + ". Who wants to walk?"
|
|
|
|
return output
|