|
@@ -20,6 +20,7 @@ |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
# SOFTWARE. |
|
|
# SOFTWARE. |
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime |
|
|
from json import loads |
|
|
from json import loads |
|
|
from urllib import quote |
|
|
from urllib import quote |
|
|
from urllib2 import urlopen |
|
|
from urllib2 import urlopen |
|
@@ -79,7 +80,7 @@ class Weather(Command): |
|
|
else: |
|
|
else: |
|
|
location = " ".join(data.args) |
|
|
location = " ".join(data.args) |
|
|
|
|
|
|
|
|
url = "http://api.wunderground.com/api/{0}/conditions/q/{1}.json" |
|
|
|
|
|
|
|
|
url = "http://api.wunderground.com/api/{0}/conditions/astronomy/q/{1}.json" |
|
|
location = quote(location, safe="") |
|
|
location = quote(location, safe="") |
|
|
query = urlopen(url.format(self.key, location)).read() |
|
|
query = urlopen(url.format(self.key, location)).read() |
|
|
res = loads(query) |
|
|
res = loads(query) |
|
@@ -105,7 +106,8 @@ class Weather(Command): |
|
|
def format_weather(self, data): |
|
|
def format_weather(self, data): |
|
|
"""Format the weather (as dict *data*) to be sent through IRC.""" |
|
|
"""Format the weather (as dict *data*) to be sent through IRC.""" |
|
|
place = data["display_location"]["full"] |
|
|
place = data["display_location"]["full"] |
|
|
icon = self.get_icon(data["icon"]) |
|
|
|
|
|
|
|
|
icon = self.get_icon(data["icon"], data["local_time_rfc822"], |
|
|
|
|
|
data["sunrise"], data["sunset"]) |
|
|
weather = data["weather"] |
|
|
weather = data["weather"] |
|
|
temp_f, temp_c = data["temp_f"], data["temp_c"] |
|
|
temp_f, temp_c = data["temp_f"], data["temp_c"] |
|
|
humidity = data["relative_humidity"] |
|
|
humidity = data["relative_humidity"] |
|
@@ -124,7 +126,7 @@ class Weather(Command): |
|
|
msg += " ({0}″ past hour)".format(data["precip_1hr_in"]) |
|
|
msg += " ({0}″ past hour)".format(data["precip_1hr_in"]) |
|
|
return msg |
|
|
return msg |
|
|
|
|
|
|
|
|
def get_icon(self, condition): |
|
|
|
|
|
|
|
|
def get_icon(self, condition, local_time, sunrise, sunset): |
|
|
"""Return a unicode icon to describe the given weather condition.""" |
|
|
"""Return a unicode icon to describe the given weather condition.""" |
|
|
icons = { |
|
|
icons = { |
|
|
"chanceflurries" : "☃", |
|
|
"chanceflurries" : "☃", |
|
@@ -132,23 +134,34 @@ class Weather(Command): |
|
|
"chancesleet" : "☃", |
|
|
"chancesleet" : "☃", |
|
|
"chancesnow" : "☃", |
|
|
"chancesnow" : "☃", |
|
|
"chancetstorms" : "☂", |
|
|
"chancetstorms" : "☂", |
|
|
"clear" : "☀", |
|
|
|
|
|
|
|
|
"clear" : "☽☀", |
|
|
"cloudy" : "☁", |
|
|
"cloudy" : "☁", |
|
|
"flurries" : "☃", |
|
|
"flurries" : "☃", |
|
|
"fog" : "☁", |
|
|
"fog" : "☁", |
|
|
"hazy" : "☁", |
|
|
"hazy" : "☁", |
|
|
"mostlycloudy" : "☁", |
|
|
"mostlycloudy" : "☁", |
|
|
"mostlysunny" : "☀", |
|
|
|
|
|
|
|
|
"mostlysunny" : "☽☀", |
|
|
"partlycloudy" : "☁", |
|
|
"partlycloudy" : "☁", |
|
|
"partlysunny" : "☀", |
|
|
|
|
|
|
|
|
"partlysunny" : "☽☀", |
|
|
"rain" : "☂", |
|
|
"rain" : "☂", |
|
|
"sleet" : "☃", |
|
|
"sleet" : "☃", |
|
|
"snow" : "☃", |
|
|
"snow" : "☃", |
|
|
"sunny" : "☀", |
|
|
|
|
|
|
|
|
"sunny" : "☽☀", |
|
|
"tstorms" : "☂", |
|
|
"tstorms" : "☂", |
|
|
} |
|
|
} |
|
|
try: |
|
|
try: |
|
|
return icons[condition] |
|
|
|
|
|
|
|
|
icon = icons[condition] |
|
|
|
|
|
if len(icon) == 2: |
|
|
|
|
|
lt_no_tz = local_time.rsplit(" ", 1)[0] |
|
|
|
|
|
dt = datetime.strptime(lt_no_tz, "%a, %d %b %Y %H:%M:%S") |
|
|
|
|
|
srise = datetime(year=dt.year, month=dt.month, day=dt.day, |
|
|
|
|
|
hour=int(sunrise["hour"]), |
|
|
|
|
|
minute=int(sunrise["minute"])) |
|
|
|
|
|
sset = datetime(year=dt.year, month=dt.month, day=dt.day, |
|
|
|
|
|
hour=int(sunset["hour"]), |
|
|
|
|
|
minute=int(sunset["minute"])) |
|
|
|
|
|
return icon[int(srise < dt < sset)] |
|
|
|
|
|
return icon |
|
|
except KeyError: |
|
|
except KeyError: |
|
|
return "?" |
|
|
return "?" |
|
|
|
|
|
|
|
|