refucktored the weather plugin some more
This commit is contained in:
parent
16d26a0a01
commit
991f6bdfe6
2 changed files with 23 additions and 28 deletions
|
@ -1,4 +1,3 @@
|
||||||
import re
|
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
@ -13,8 +12,6 @@ def invite(paraml, conn=None):
|
||||||
invite_join = conn.conf.get('invite_join', True)
|
invite_join = conn.conf.get('invite_join', True)
|
||||||
if invite_join:
|
if invite_join:
|
||||||
conn.join(paraml[-1])
|
conn.join(paraml[-1])
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
# Identify to NickServ (or other service)
|
# Identify to NickServ (or other service)
|
||||||
|
@ -40,7 +37,7 @@ def onjoin(paraml, conn=None, bot=None):
|
||||||
conn.join(channel)
|
conn.join(channel)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
print "onjoin() sucessfully completed."
|
print "Bot ready."
|
||||||
|
|
||||||
|
|
||||||
@hook.event("KICK")
|
@hook.event("KICK")
|
||||||
|
@ -51,8 +48,7 @@ def onkick(paraml, conn=None, chan=None):
|
||||||
auto_rejoin = conn.conf.get('auto_rejoin', False)
|
auto_rejoin = conn.conf.get('auto_rejoin', False)
|
||||||
if auto_rejoin:
|
if auto_rejoin:
|
||||||
conn.join(paraml[0])
|
conn.join(paraml[0])
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
@hook.singlethread
|
@hook.singlethread
|
||||||
@hook.event('004')
|
@hook.event('004')
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
from util import hook
|
from util import hook
|
||||||
import yql
|
import yql as YQL
|
||||||
|
|
||||||
|
|
||||||
def get_weather(location_id):
|
def get_weather(location):
|
||||||
"""uses the yahoo weather API to get weather information for a location"""
|
"""uses the yahoo weather API to get weather information for a location"""
|
||||||
|
|
||||||
y = yql.Public()
|
yql = YQL.Public()
|
||||||
query = 'select * from weather.forecast where woeid = "{}"'.format(location_id)
|
query = "use 'http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml' as we;" \
|
||||||
data = y.execute(query).one()
|
"SELECT * FROM we WHERE location=@location LIMIT 1"
|
||||||
|
data = yql.execute(query, {"location": location}).one()
|
||||||
|
|
||||||
|
data = data["rss"]["channel"]
|
||||||
|
|
||||||
# wind conversions
|
# wind conversions
|
||||||
data['wind']['chill_c'] = int(round((int(data['wind']['chill']) - 32) / 1.8, 0))
|
data['wind']['chill_c'] = int(round((int(data['wind']['chill']) - 32) / 1.8, 0))
|
||||||
|
@ -64,22 +67,22 @@ def get_weather(location_id):
|
||||||
|
|
||||||
|
|
||||||
@hook.command(autohelp=False)
|
@hook.command(autohelp=False)
|
||||||
def weather(inp, nick='', server='', reply=None, db=None, notice=None):
|
def weather(inp, nick="", reply=None, db=None, notice=None):
|
||||||
"weather <location> [dontsave] -- Gets weather data"\
|
"weather <location> [dontsave] -- Gets weather data"\
|
||||||
" for <location> from Google."
|
" for <location> from Yahoo."
|
||||||
|
|
||||||
# initalise weather DB
|
# initalise weather DB
|
||||||
db.execute("create table if not exists y_weather(nick primary key, location_id)")
|
db.execute("create table if not exists weather(nick primary key, loc)")
|
||||||
|
|
||||||
# if there is no input, try getting the users last location from the DB
|
# if there is no input, try getting the users last location from the DB
|
||||||
if not inp:
|
if not inp:
|
||||||
location_id = db.execute("select location_id from y_weather where nick=lower(?)",
|
location = db.execute("select loc from weather where nick=lower(?)",
|
||||||
[nick]).fetchone()
|
[nick]).fetchone()
|
||||||
if not location_id:
|
if not location:
|
||||||
# no location saved in the database, send the user help text
|
# no location saved in the database, send the user help text
|
||||||
notice(weather.__doc__)
|
notice(weather.__doc__)
|
||||||
return
|
return
|
||||||
location_id = location_id[0]
|
location = location[0]
|
||||||
|
|
||||||
# no need to save a location, we already have it
|
# no need to save a location, we already have it
|
||||||
dontsave = True
|
dontsave = True
|
||||||
|
@ -93,15 +96,11 @@ def weather(inp, nick='', server='', reply=None, db=None, notice=None):
|
||||||
else:
|
else:
|
||||||
location = inp
|
location = inp
|
||||||
|
|
||||||
# get the weather.com location id from the location
|
|
||||||
query = 'select * from geo.places where text = "{}" limit 1'.format(location)
|
|
||||||
|
|
||||||
y = yql.Public()
|
|
||||||
results = y.execute(query).one()
|
|
||||||
location_id = results.get("woeid")
|
|
||||||
|
|
||||||
# now, to get the actual weather
|
# now, to get the actual weather
|
||||||
d = get_weather(location_id)
|
try:
|
||||||
|
d = get_weather(location)
|
||||||
|
except KeyError:
|
||||||
|
return "Could not get weather for that location."
|
||||||
|
|
||||||
reply("Current Conditions for \x02{}\x02 - {}, {}F/{}C, {}%, " \
|
reply("Current Conditions for \x02{}\x02 - {}, {}F/{}C, {}%, " \
|
||||||
"Wind: {}KPH/{}MPH {}.".format(d['location']['city'], \
|
"Wind: {}KPH/{}MPH {}.".format(d['location']['city'], \
|
||||||
|
@ -109,7 +108,7 @@ def weather(inp, nick='', server='', reply=None, db=None, notice=None):
|
||||||
d['item']['condition']['temp_c'], d['atmosphere']['humidity'], \
|
d['item']['condition']['temp_c'], d['atmosphere']['humidity'], \
|
||||||
d['wind']['speed_kph'], d['wind']['speed'], d['wind']['text']))
|
d['wind']['speed_kph'], d['wind']['speed'], d['wind']['text']))
|
||||||
|
|
||||||
if location_id and not dontsave:
|
if location and not dontsave:
|
||||||
db.execute("insert or replace into y_weather(nick, location_id) values (?,?)",
|
db.execute("insert or replace into weather(nick, loc) values (?,?)",
|
||||||
(nick.lower(), location_id))
|
(nick.lower(), location))
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
Reference in a new issue