Added forecast command to plugins/weather.py
This commit is contained in:
parent
7f53bdcb49
commit
6cad8792d4
|
@ -16,6 +16,10 @@ For the spell.py plugin to work you need *python-enchant*. This can be installed
|
||||||
```sudo apt-get install python-enchant
|
```sudo apt-get install python-enchant
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The whois.py plugin will work by default, but *not very well*. To make it work properly you will need to install the native *whois* package. This can be installed with:
|
||||||
|
```sudo apt-get install whois
|
||||||
|
```
|
||||||
|
|
||||||
If you use another OS or distro you can find source packages on the module(s) web site, or you can try to find suitable packages in your package manager.
|
If you use another OS or distro you can find source packages on the module(s) web site, or you can try to find suitable packages in your package manager.
|
||||||
|
|
||||||
Once installing these packages run the bot once with ```python bot.py``` to generate the config file. Stop the bot, edit the config, and run the bot again with ```python bot.py``` to start it up :)
|
Once installing these packages run the bot once with ```python bot.py``` to generate the config file. Stop the bot, edit the config, and run the bot again with ```python bot.py``` to start it up :)
|
||||||
|
|
|
@ -2,11 +2,9 @@
|
||||||
|
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
|
||||||
@hook.command(autohelp=False)
|
@hook.command(autohelp=False)
|
||||||
def weather(inp, nick='', server='', reply=None, db=None, notice=None):
|
def forecast(inp, nick='', server='', reply=None, db=None, notice=None, say=None):
|
||||||
".weather <location> [dontsave] -- gets weather data from Google"
|
".forecast <location> [dontsave] -- gets a weather forecast from Google"
|
||||||
|
|
||||||
loc = inp
|
loc = inp
|
||||||
|
|
||||||
dontsave = loc.endswith(" dontsave")
|
dontsave = loc.endswith(" dontsave")
|
||||||
|
@ -16,6 +14,46 @@ def weather(inp, nick='', server='', reply=None, db=None, notice=None):
|
||||||
db.execute("create table if not exists weather(nick primary key, loc)")
|
db.execute("create table if not exists weather(nick primary key, loc)")
|
||||||
|
|
||||||
if not loc: # blank line
|
if not loc: # blank line
|
||||||
|
loc = db.execute("select loc from weather where nick=lower(?)",
|
||||||
|
(nick,)).fetchone()
|
||||||
|
if not loc:
|
||||||
|
notice(forecast.__doc__)
|
||||||
|
return
|
||||||
|
loc = loc[0]
|
||||||
|
|
||||||
|
w = http.get_xml('http://www.google.com/ig/api', weather=loc)
|
||||||
|
w = w.find('weather')
|
||||||
|
|
||||||
|
if w.find('problem_cause') is not None:
|
||||||
|
notice("Couldn't fetch weather data for '%s', try using a zip or " \
|
||||||
|
"postal code." % inp)
|
||||||
|
return
|
||||||
|
city = w.find('forecast_information/city').get('data')
|
||||||
|
|
||||||
|
out = "%s: " % city
|
||||||
|
|
||||||
|
for elem in w.findall('forecast_conditions'):
|
||||||
|
info = dict((e.tag, e.get('data')) for e in elem)
|
||||||
|
info['high'] = elem.find('high').get('data')
|
||||||
|
info['low'] = elem.find('low').get('data')
|
||||||
|
|
||||||
|
out += '[%(day_of_week)s]: %(condition)s (H:%(high)sF'\
|
||||||
|
', L:%(low)sF) ' % info
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
@hook.command(autohelp=False)
|
||||||
|
def weather(inp, nick='', server='', reply=None, db=None, notice=None):
|
||||||
|
".weather <location> [dontsave] -- gets weather data from Google"
|
||||||
|
loc = inp
|
||||||
|
|
||||||
|
dontsave = loc.endswith(" dontsave")
|
||||||
|
if dontsave:
|
||||||
|
loc = loc[:-9].strip().lower()
|
||||||
|
|
||||||
|
db.execute("create table if not exists weather(nick primary key, loc)")
|
||||||
|
|
||||||
|
if not loc:
|
||||||
loc = db.execute("select loc from weather where nick=lower(?)",
|
loc = db.execute("select loc from weather where nick=lower(?)",
|
||||||
(nick,)).fetchone()
|
(nick,)).fetchone()
|
||||||
if not loc:
|
if not loc:
|
||||||
|
|
Reference in a new issue