From a3148096719e98886e7517dbb10de33c484e2231 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 16 Jan 2016 19:38:24 -0600 Subject: [PATCH] weather: use requests library over urllib/json --- dr_botzo/weather/lib.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/dr_botzo/weather/lib.py b/dr_botzo/weather/lib.py index 029040c..86d73cc 100644 --- a/dr_botzo/weather/lib.py +++ b/dr_botzo/weather/lib.py @@ -1,9 +1,8 @@ # coding: utf-8 -import json import logging import re -import urllib.request, urllib.error, urllib.parse +import requests from django.conf import settings @@ -22,8 +21,8 @@ def get_conditions_for_query(queryitems): try: url = wu_base_url + ('{0:s}/q/{1:s}.json'.format('conditions', query)) log.debug("calling %s", url) - json_resp = urllib.request.urlopen(url) - condition_data = json.load(json_resp) + resp = requests.get(url) + condition_data = resp.json() except IOError as e: log.error("error while making conditions query") log.exception(e) @@ -31,7 +30,7 @@ def get_conditions_for_query(queryitems): # condition data is loaded. the rest of this is obviously specific to # http://www.wunderground.com/weather/api/d/docs?d=data/conditions - log.debug(json.dumps(condition_data, sort_keys=True, indent=4)) + log.debug(condition_data) try: # just see if we have current_observation data @@ -135,8 +134,8 @@ def get_forecast_for_query(queryitems): try: url = wu_base_url + ('{0:s}/q/{1:s}.json'.format('forecast', query)) - json_resp = urllib.request.urlopen(url) - forecast_data = json.load(json_resp) + resp = requests.get(url) + forecast_data = resp.json() except IOError as e: log.error("error while making forecast query") log.exception(e) @@ -144,7 +143,7 @@ def get_forecast_for_query(queryitems): # forecast data is loaded. the rest of this is obviously specific to # http://www.wunderground.com/weather/api/d/docs?d=data/forecast - log.debug(json.dumps(forecast_data, sort_keys=True, indent=4)) + log.debug(forecast_data) try: # just see if we have forecast data