Skip to content

Commit

Permalink
[clock, lmgtfy, weather] Use 4.0 standards, issue #276
Browse files Browse the repository at this point in the history
  • Loading branch information
embolalia committed Jun 9, 2013
1 parent 9bd8ba7 commit b0de0e1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 113 deletions.
148 changes: 73 additions & 75 deletions willie/modules/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
import struct
import datetime
from decimal import Decimal as dec
from willie.module import commands, command, example


def setup(willie):
#Having a db means pref's exists. Later, we can just use `if willie.db`.
if willie.db and not willie.db.preferences.has_columns('tz'):
willie.db.preferences.add_columns(['tz'])
if willie.db and not willie.db.preferences.has_columns('time_format'):
willie.db.preferences.add_columns(['time_format'])
def setup(bot):
#Having a db means pref's exists. Later, we can just use `if bot.db`.
if bot.db and not bot.db.preferences.has_columns('tz'):
bot.db.preferences.add_columns(['tz'])
if bot.db and not bot.db.preferences.has_columns('time_format'):
bot.db.preferences.add_columns(['time_format'])


def f_time(willie, trigger):
@commands('t', 'time')
@example('.t America/New_York')
def f_time(bot, trigger):
"""Returns the current time."""
tz = trigger.group(2)
if tz:
Expand All @@ -35,167 +38,162 @@ def f_time(willie, trigger):
#more, because we know it's valid. Otherwise, we have to check if it's
#supposed to be a user, or just invalid
if tz not in pytz.all_timezones:
if willie.db and tz in willie.db.preferences:
tz = willie.db.preferences.get(tz, 'tz')
if bot.db and tz in bot.db.preferences:
tz = bot.db.preferences.get(tz, 'tz')
if not tz:
willie.say("I'm sorry, I don't know %s's timezone" % tz)
bot.say("I'm sorry, I don't know %s's timezone" % tz)
else:
willie.say("I'm sorry, I don't know about the %s timezone or"
bot.say("I'm sorry, I don't know about the %s timezone or"
" user." % tz)
return
#We don't have a timzeone. Is there one set? If not, just use UTC
elif willie.db:
if trigger.nick in willie.db.preferences:
tz = willie.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in willie.db.preferences:
tz = willie.db.preferences.get(trigger.sender, 'tz') or 'UTC'
elif bot.db:
if trigger.nick in bot.db.preferences:
tz = bot.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in bot.db.preferences:
tz = bot.db.preferences.get(trigger.sender, 'tz') or 'UTC'
else:
tz = 'UTC'
tzi = pytz.timezone(tz)
now = datetime.datetime.now(tzi)

tformat = ''
if willie.db:
if trigger.nick in willie.db.preferences:
tformat = willie.db.preferences.get(trigger.nick, 'time_format')
if not tformat and trigger.sender in willie.db.preferences:
tformat = willie.db.preferences.get(trigger.sender, 'time_format')
if bot.db:
if trigger.nick in bot.db.preferences:
tformat = bot.db.preferences.get(trigger.nick, 'time_format')
if not tformat and trigger.sender in bot.db.preferences:
tformat = bot.db.preferences.get(trigger.sender, 'time_format')

willie.say(now.strftime(tformat or "%F - %T%Z"))
f_time.commands = ['t', 'time']
f_time.name = 't'
f_time.example = '.t UTC'
bot.say(now.strftime(tformat or "%F - %T%Z"))


def update_user(willie, trigger):
@command('settz')
@example('.settz America/New_York')
def update_user(bot, trigger):
"""
Set your preferred time zone. Most timezones will work, but it's best to
use one from http://dft.ba/-tz
"""
if willie.db:
if bot.db:
tz = trigger.group(2)
if not tz:
willie.reply("What timzeone do you want to set? Try one from "
bot.reply("What timzeone do you want to set? Try one from "
"http://dft.ba/-tz")
return
if tz not in pytz.all_timezones:
willie.reply("I don't know that time zone. Try one from "
bot.reply("I don't know that time zone. Try one from "
"http://dft.ba/-tz")
return

willie.db.preferences.update(trigger.nick, {'tz': tz})
bot.db.preferences.update(trigger.nick, {'tz': tz})
if len(tz) < 7:
willie.say("Okay, " + trigger.nick +
bot.say("Okay, " + trigger.nick +
", but you should use one from http://dft.ba/-tz if "
"you use DST.")
else:
willie.reply('I now have you in the %s time zone.' % tz)
bot.reply('I now have you in the %s time zone.' % tz)
else:
willie.reply("I can't remember that; I don't have a database.")
update_user.commands = ['settz']
bot.reply("I can't remember that; I don't have a database.")


def update_user_format(willie, trigger):
@commands('settimeformat', 'settf')
@example('.settf %FT%T%z')
def update_user_format(bot, trigger):
"""
Sets your preferred format for time. Uses the standard strftime format. You
can use http://strftime.net or your favorite search engine to learn more.
"""
if willie.db:
if bot.db:
tformat = trigger.group(2)
if not tformat:
willie.reply("What format do you want me to use? Try using"
bot.reply("What format do you want me to use? Try using"
" http://strftime.net to make one.")

tz = ''
if willie.db:
if trigger.nick in willie.db.preferences:
tz = willie.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in willie.db.preferences:
tz = willie.db.preferences.get(trigger.sender, 'tz')
if bot.db:
if trigger.nick in bot.db.preferences:
tz = bot.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in bot.db.preferences:
tz = bot.db.preferences.get(trigger.sender, 'tz')
now = datetime.datetime.now(pytz.timezone(tz or 'UTC'))
timef = ''
try:
timef = now.strftime(tformat)
except:
willie.reply("That format doesn't work. Try using"
bot.reply("That format doesn't work. Try using"
" http://strftime.net to make one.")
return
willie.db.preferences.update(trigger.nick, {'time_format': tformat})
willie.reply("Got it. Your time will now appear as %s. (If the "
bot.db.preferences.update(trigger.nick, {'time_format': tformat})
bot.reply("Got it. Your time will now appear as %s. (If the "
"timezone is wrong, you might try the settz command)"
% timef)
else:
willie.reply("I can't remember that; I don't have a database.")
update_user_format.commands = ['settimeformat', 'settf']
update_user_format.example = ".settf %FT%T%z"
bot.reply("I can't remember that; I don't have a database.")


def update_channel(willie, trigger):
@command('channeltz')
@example('.chantz America/New_York')
def update_channel(bot, trigger):
"""
Set the preferred time zone for the channel.
"""
if not trigger.isop:
return
if willie.db:
if bot.db:
tz = trigger.group(2)
if not tz:
willie.reply("What timzeone do you want to set? Try one from "
bot.reply("What timzeone do you want to set? Try one from "
"http://dft.ba/-tz")
return
if tz not in pytz.all_timezones:
willie.reply("I don't know that time zone. Try one from "
bot.reply("I don't know that time zone. Try one from "
"http://dft.ba/-tz")
return

willie.db.preferences.update(trigger.sender, {'tz': tz})
bot.db.preferences.update(trigger.sender, {'tz': tz})
if len(tz) < 7:
willie.say("Okay, " + trigger.nick +
bot.say("Okay, " + trigger.nick +
", but you should use one from http://dft.ba/-tz if "
"you use DST.")
else:
willie.reply('I now have you in the %s time zone.' % tz)
bot.reply('I now have you in the %s time zone.' % tz)
else:
willie.reply("I can't remember that; I don't have a database.")
update_channel.commands = ['channeltz']
bot.reply("I can't remember that; I don't have a database.")


def update_channel_format(willie, trigger):
@commands('setchanneltimeformat', 'setctf')
@example('setctf %FT%T%z')
def update_channel_format(bot, trigger):
"""
Sets your preferred format for time. Uses the standard strftime format. You
can use http://strftime.net or your favorite search engine to learn more.
"""
if not trigger.isop:
return
if willie.db:
if bot.db:
tformat = trigger.group(2)
if not tformat:
willie.reply("What format do you want me to use? Try using"
bot.reply("What format do you want me to use? Try using"
" http://strftime.net to make one.")

tz = ''
if willie.db:
if trigger.nick in willie.db.preferences:
tz = willie.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in willie.db.preferences:
tz = willie.db.preferences.get(trigger.sender, 'tz')
if bot.db:
if trigger.nick in bot.db.preferences:
tz = bot.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in bot.db.preferences:
tz = bot.db.preferences.get(trigger.sender, 'tz')
now = datetime.datetime.now(pytz.timezone(tz or 'UTC'))
timef = ''
try:
timef = now.strftime(tformat)
except:
willie.reply("That format doesn't work. Try using"
bot.reply("That format doesn't work. Try using"
" http://strftime.net to make one.")
return
willie.db.preferences.update(trigger.sender, {'time_format': tformat})
willie.reply("Got it. Times in this channel will now appear as %s "
bot.db.preferences.update(trigger.sender, {'time_format': tformat})
bot.reply("Got it. Times in this channel will now appear as %s "
"unless a user has their own format set. (If the timezone"
" is wrong, you might try the settz and channeltz "
"commands)" % timef)
else:
willie.reply("I can't remember that; I don't have a database.")
update_channel_format.commands = ['setchanneltimeformat', 'setctf']
update_channel_format.example = ".settf %FT%T%z"


if __name__ == '__main__':
print __doc__.strip()
bot.reply("I can't remember that; I don't have a database.")
14 changes: 5 additions & 9 deletions willie/modules/lmgtfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
http://willie.dftba.net/
"""
from willie.module import commands

def googleit(willie, trigger):
@commands('lmgtfy', 'lmgify', 'gify', 'gtfy')
def googleit(bot, trigger):
"""Let me just... google that for you."""
#No input
if not trigger.group(2):
return willie.say('http://google.com/')
willie.say('http://lmgtfy.com/?q='+trigger.group(2).replace(' ','+'))
googleit.commands = ['lmgtfy','lmgify','gify','gtfy']
googleit.priority = 'medium'

if __name__ == '__main__':
print __doc__.strip()

return bot.say('http://google.com/')
bot.say('http://lmgtfy.com/?q=' + trigger.group(2).replace(' ', '+'))
54 changes: 25 additions & 29 deletions willie/modules/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
import re
import urllib
import json
import willie.web as web
from willie import web
from willie.module import command, commands, example
from lxml import etree
import feedparser

r_from = re.compile(r'(?i)([+-]\d+):00 from')


def setup(willie):
#Having a db means pref's exists. Later, we can just use `if willie.db`.
if willie.db and not willie.db.preferences.has_columns('woeid'):
willie.db.preferences.add_columns(['woeid'])
def setup(bot):
#Having a db means pref's exists. Later, we can just use `if bot.db`.
if bot.db and not bot.db.preferences.has_columns('woeid'):
bot.db.preferences.add_columns(['woeid'])


def woeid_search(query):
Expand Down Expand Up @@ -134,25 +133,27 @@ def get_wind(parsed):
return description + ' ' + str(speed) + 'kt (' + degrees + ')'


def weather(willie, trigger):
@command('weather')
@example('.weather London')
def weather(bot, trigger):
""".weather location - Show the weather at the given location."""

location = trigger.group(2)
woeid = ''
if not location:
if willie.db and trigger.nick in willie.db.preferences:
woeid = willie.db.preferences.get(trigger.nick, 'woeid')
if bot.db and trigger.nick in bot.db.preferences:
woeid = bot.db.preferences.get(trigger.nick, 'woeid')
if not woeid:
return willie.msg(trigger.sender, "I don't know where you live. " +
'Give me a location, like .weather London, or tell me where you live by saying .setlocation London, for example.')
return bot.msg(trigger.sender, "I don't know where you live. " +
'Give me a location, like .weather London, or tell me where you live by saying .setlocation London, for example.')
else:
if willie.db and location in willie.db.preferences:
woeid = willie.db.preferences.get(location, 'woeid')
if bot.db and location in bot.db.preferences:
woeid = bot.db.preferences.get(location, 'woeid')
else:
woeid = woeid_search(location).find('woeid').text

if not woeid:
return willie.reply("I don't know where that is.")
return bot.reply("I don't know where that is.")

query = web.urlencode({'w': woeid, 'u': 'c'})
url = 'http://weather.yahooapis.com/forecastrss?' + query
Expand All @@ -163,18 +164,18 @@ def weather(willie, trigger):
temp = get_temp(parsed)
pressure = get_pressure(parsed)
wind = get_wind(parsed)
willie.say(u'%s: %s, %s, %s, %s' % (location, cover, temp, pressure, wind))
weather.commands = ['weather']
weather.example = '.weather London'
bot.say(u'%s: %s, %s, %s, %s' % (location, cover, temp, pressure, wind))


def update_woeid(willie, trigger):
@commands('setlocation', 'setwoeid')
@example('.setlocation Columbus, OH')
def update_woeid(bot, trigger):
"""Set your default weather location."""
if willie.db:
if bot.db:
first_result = woeid_search(trigger.group(2))
woeid = first_result.find('woeid').text

willie.db.preferences.update(trigger.nick, {'woeid': woeid})
bot.db.preferences.update(trigger.nick, {'woeid': woeid})

neighborhood = first_result.find('neighborhood').text or ''
if neighborhood:
Expand All @@ -183,12 +184,7 @@ def update_woeid(willie, trigger):
state = first_result.find('state').text or ''
country = first_result.find('country').text or ''
uzip = first_result.find('uzip').text or ''
willie.reply('I now have you at WOEID %s (%s %s, %s, %s %s.)' %
(woeid, neighborhood, city, state, country, uzip))
bot.reply('I now have you at WOEID %s (%s %s, %s, %s %s.)' %
(woeid, neighborhood, city, state, country, uzip))
else:
willie.reply("I can't remember that; I don't have a database.")
update_woeid.commands = ['setlocation', 'setwoeid']
update_woeid.example = '.setlocation Columbus, OH'

if __name__ == '__main__':
print __doc__.strip()
bot.reply("I can't remember that; I don't have a database.")

0 comments on commit b0de0e1

Please sign in to comment.