mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-05-03 08:51:13 -05:00
Merge branch 'master' of github.com:reticulatingspline/Weather
Merge pull
This commit is contained in:
commit
312d71b89a
@ -1,6 +1,7 @@
|
|||||||
language: python
|
language: python
|
||||||
python:
|
python:
|
||||||
- "2.7"
|
- "2.7"
|
||||||
|
- "3.4"
|
||||||
- pypy
|
- pypy
|
||||||
# command to install dependencies,
|
# command to install dependencies,
|
||||||
install:
|
install:
|
||||||
|
34
plugin.py
34
plugin.py
@ -4,10 +4,14 @@
|
|||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
###
|
###
|
||||||
# my libs
|
# my libs
|
||||||
|
from __future__ import unicode_literals
|
||||||
import json # json.
|
import json # json.
|
||||||
from math import floor # for wind.
|
from math import floor # for wind.
|
||||||
import sqlite3 # userdb.
|
import sqlite3 # userdb.
|
||||||
from itertools import izip # userdb.
|
try:
|
||||||
|
from itertools import izip
|
||||||
|
except ImportError: # python3
|
||||||
|
izip = zip
|
||||||
# extra supybot libs
|
# extra supybot libs
|
||||||
import supybot.conf as conf
|
import supybot.conf as conf
|
||||||
import supybot.log as log
|
import supybot.log as log
|
||||||
@ -241,7 +245,7 @@ class Weather(callbacks.Plugin):
|
|||||||
return ircutils.mircColor(("{0:.0f}F".format(x)), color)
|
return ircutils.mircColor(("{0:.0f}F".format(x)), color)
|
||||||
else: # temp is in F and we need to go back to C.
|
else: # temp is in F and we need to go back to C.
|
||||||
return ircutils.mircColor(("{0:.0f}C".format((x - 32) * 5 / 9)),color)
|
return ircutils.mircColor(("{0:.0f}C".format((x - 32) * 5 / 9)),color)
|
||||||
except Exception, e: # rutroh. something went wrong.
|
except Exception as e: # rutroh. something went wrong.
|
||||||
self.log.info("_temp: ERROR trying to convert temp: {0} message: {1}".format(x, e))
|
self.log.info("_temp: ERROR trying to convert temp: {0} message: {1}".format(x, e))
|
||||||
return x
|
return x
|
||||||
|
|
||||||
@ -330,7 +334,7 @@ class Weather(callbacks.Plugin):
|
|||||||
# try and fetch.
|
# try and fetch.
|
||||||
try:
|
try:
|
||||||
page = utils.web.getUrl(url)
|
page = utils.web.getUrl(url)
|
||||||
except Exception, e: # something didn't work.
|
except Exception as e: # something didn't work.
|
||||||
self.log.info("_wuac: ERROR: Trying to open {0} message: {1}".format(url, e))
|
self.log.info("_wuac: ERROR: Trying to open {0} message: {1}".format(url, e))
|
||||||
return None
|
return None
|
||||||
# now process json and return.
|
# now process json and return.
|
||||||
@ -339,7 +343,7 @@ class Weather(callbacks.Plugin):
|
|||||||
loc = data['RESULTS'][0]['zmw'] # find the first zmw.
|
loc = data['RESULTS'][0]['zmw'] # find the first zmw.
|
||||||
loc = "zmw:%s" % loc # return w/zmw: attached.
|
loc = "zmw:%s" % loc # return w/zmw: attached.
|
||||||
return loc
|
return loc
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self.log.info("_wuac: ERROR processing json in {0} :: {1}".format(url, e))
|
self.log.info("_wuac: ERROR processing json in {0} :: {1}".format(url, e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -356,7 +360,7 @@ class Weather(callbacks.Plugin):
|
|||||||
self.log.info("URL: {0}".format(url))
|
self.log.info("URL: {0}".format(url))
|
||||||
page = utils.web.getUrl(url)
|
page = utils.web.getUrl(url)
|
||||||
return page
|
return page
|
||||||
except Exception, e: # something didn't work.
|
except Exception as e: # something didn't work.
|
||||||
self.log.info("_wunderjson: ERROR Trying to open {0} message: {1}".format(url, e))
|
self.log.info("_wunderjson: ERROR Trying to open {0} message: {1}".format(url, e))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -691,7 +695,7 @@ class Weather(callbacks.Plugin):
|
|||||||
|
|
||||||
# OUTPUT.
|
# OUTPUT.
|
||||||
# we go step-by-step to build the proper string. ° u" \u00B0C"
|
# we go step-by-step to build the proper string. ° u" \u00B0C"
|
||||||
output = "{0} :: {1} ::".format(self._bold(outdata['location'].encode('utf-8')), outdata['weather'].encode('utf-8'))
|
output = "{0} :: {1} ::".format(self._bold(outdata['location']), outdata['weather'])
|
||||||
# add in temperature.
|
# add in temperature.
|
||||||
output += " {0}".format(outdata['temp'])
|
output += " {0}".format(outdata['temp'])
|
||||||
# humidity.
|
# humidity.
|
||||||
@ -708,19 +712,19 @@ class Weather(callbacks.Plugin):
|
|||||||
for (k, v) in args.items():
|
for (k, v) in args.items():
|
||||||
if k in ['wind', 'visibility', 'uv', 'pressure', 'dewpoint']: # if key is in extras
|
if k in ['wind', 'visibility', 'uv', 'pressure', 'dewpoint']: # if key is in extras
|
||||||
if v: # if that key's value is True, we add it.
|
if v: # if that key's value is True, we add it.
|
||||||
output += "| {0}: {1} ".format(self._bold(k.title()), outdata[k].encode('utf-8'))
|
output += "| {0}: {1} ".format(self._bold(k.title()), outdata[k])
|
||||||
# add in the first two forecast item in conditions + updated time.
|
# add in the first two forecast item in conditions + updated time.
|
||||||
output += "| {0}: {1}".format(self._bold(forecastdata[0]['day'].encode('utf-8')), forecastdata[0]['text'].encode('utf-8'))
|
output += "| {0}: {1}".format(self._bold(forecastdata[0]['day']), forecastdata[0]['text'])
|
||||||
output += " {0}: {1}".format(self._bold(forecastdata[1]['day'].encode('utf-8')), forecastdata[1]['text'].encode('utf-8'))
|
output += " {0}: {1}".format(self._bold(forecastdata[1]['day']), forecastdata[1]['text'])
|
||||||
# show Updated?
|
# show Updated?
|
||||||
if args['updated']:
|
if args['updated']:
|
||||||
output += " | {0} {1}".format(self._bold('Updated:'), outdata['observation'].encode('utf-8'))
|
output += " | {0} {1}".format(self._bold('Updated:'), outdata['observation'])
|
||||||
# finally, output the basic weather.
|
# finally, output the basic weather.
|
||||||
irc.reply(output)
|
irc.reply(output)
|
||||||
|
|
||||||
# next, for outputting, handle the extras like alerts, almanac, astronomy, forecast.
|
# next, for outputting, handle the extras like alerts, almanac, astronomy, forecast.
|
||||||
if args['alerts']: # if --alerts issued.
|
if args['alerts']: # if --alerts issued.
|
||||||
irc.reply("{0} :: {1}".format(self._bu("Alerts:"), outdata['alerts'].encode('utf-8')))
|
irc.reply("{0} :: {1}".format(self._bu("Alerts:"), outdata['alerts']))
|
||||||
# handle almanac if --almanac is given.
|
# handle almanac if --almanac is given.
|
||||||
if args['almanac']:
|
if args['almanac']:
|
||||||
if args['nocolortemp']: # disable colored temp?
|
if args['nocolortemp']: # disable colored temp?
|
||||||
@ -745,11 +749,11 @@ class Weather(callbacks.Plugin):
|
|||||||
outforecast = [] # prep string for output.
|
outforecast = [] # prep string for output.
|
||||||
for (k, v) in fullforecastdata.items(): # iterate through forecast data.
|
for (k, v) in fullforecastdata.items(): # iterate through forecast data.
|
||||||
if args['nocolortemp']:
|
if args['nocolortemp']:
|
||||||
outforecast.append("{0}: {1} ({2}/{3})".format(self._bold(v['day'].encode('utf-8')),\
|
outforecast.append("{0}: {1} ({2}/{3})".format(self._bold(v['day']),\
|
||||||
v['text'].encode('utf-8'), v['high'], v['low']))
|
v['text'], v['high'], v['low']))
|
||||||
else:
|
else:
|
||||||
outforecast.append("{0}: {1} ({2}/{3})".format(self._bold(v['day'].encode('utf-8')),\
|
outforecast.append("{0}: {1} ({2}/{3})".format(self._bold(v['day']),\
|
||||||
v['text'].encode('utf-8'), self._temp(v['high']), self._temp(v['low'])))
|
v['text'], self._temp(v['high']), self._temp(v['low'])))
|
||||||
# construct our string to output.
|
# construct our string to output.
|
||||||
output = "{0} :: {1}".format(self._bu('Forecast:'), " | ".join(outforecast))
|
output = "{0} :: {1}".format(self._bu('Forecast:'), " | ".join(outforecast))
|
||||||
# now output to irc.
|
# now output to irc.
|
||||||
|
6
test.py
6
test.py
@ -5,6 +5,7 @@
|
|||||||
#
|
#
|
||||||
###
|
###
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
from supybot.test import *
|
from supybot.test import *
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -14,6 +15,11 @@ class WeatherTestCase(PluginTestCase):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
PluginTestCase.setUp(self)
|
PluginTestCase.setUp(self)
|
||||||
apiKey = os.environ.get('apiKey')
|
apiKey = os.environ.get('apiKey')
|
||||||
|
if not apiKey:
|
||||||
|
e = """The Wunderground API key has not been set.
|
||||||
|
please set this value correctly and try again:
|
||||||
|
'export apiKey=<key>' for bash users"""
|
||||||
|
raise Exception(e)
|
||||||
conf.supybot.plugins.Weather.apiKey.setValue(apiKey)
|
conf.supybot.plugins.Weather.apiKey.setValue(apiKey)
|
||||||
|
|
||||||
def testWeather(self):
|
def testWeather(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user