[modules/weather] Replace urllib with requests

Since requests works the same for python2.7 and python3.x, use requests
instead of urllib (which returns a string in python2.7, but byte data in
python3.0, at least).
This commit is contained in:
Tobi-wan Kenobi 2017-02-26 08:19:30 +01:00
parent f51ad0c6ff
commit 8dc07e666e

View file

@ -4,7 +4,7 @@
"""Displays the temperature on the current location based on the ip """Displays the temperature on the current location based on the ip
Requires the following python packages: Requires the following python packages:
* urllib * requests
Parameters: Parameters:
* weather.interval: Interval (in minutes) for updating weather information * weather.interval: Interval (in minutes) for updating weather information
@ -17,7 +17,7 @@ import bumblebee.output
import bumblebee.engine import bumblebee.engine
import json import json
import time import time
from urllib import urlopen import requests
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
@ -52,12 +52,12 @@ class Module(bumblebee.engine.Module):
weather_url = "{}&units={}".format(weather_url, self._unit) weather_url = "{}&units={}".format(weather_url, self._unit)
if self._location == "auto": if self._location == "auto":
location_url = "http://ipinfo.io/json" location_url = "http://ipinfo.io/json"
location = json.loads(urlopen(location_url).read()) location = json.loads(requests.get(location_url).text)
coord = location["loc"].split(",") coord = location["loc"].split(",")
weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1]) weather_url = "{url}&lat={lat}&lon={lon}".format(url=weather_url, lat=coord[0], lon=coord[1])
else: else:
weather_url = "{url}&q={city}".format(url=weather_url, city=self._location) weather_url = "{url}&q={city}".format(url=weather_url, city=self._location)
weather = json.loads(urlopen(weather_url).read()) weather = json.loads(requests.get(weather_url).text)
self._temperature = weather['main']['temp'] self._temperature = weather['main']['temp']
self._timer += 1 self._timer += 1
return return