Skip to content

Commit

Permalink
[calc, bugzilla, radio] Update to 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 2f9bfad commit d381f3e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 78 deletions.
27 changes: 14 additions & 13 deletions willie/modules/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from lxml import etree
import re
from willie import web
from willie.module import rule
import urllib
import urllib2

Expand All @@ -28,27 +29,30 @@ def configure(config):
'Domain:')


def setup(willie):
def setup(bot):
regexes = []
if not (willie.config.has_option('bugzilla', 'domains')
and willie.config.bugzilla.get_list('domains')):
if not (bot.config.has_option('bugzilla', 'domains')
and bot.config.bugzilla.get_list('domains')):
return
if not willie.memory.contains('url_callbacks'):
willie.memory['url_callbacks'] = {}
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = {}

domains = '|'.join(willie.config.bugzilla.get_list('domains'))
domains = '|'.join(bot.config.bugzilla.get_list('domains'))
regex = re.compile((r'https?://(%s)'
'(/show_bug.cgi\?\S*?)'
'(id=\d+)')
% domains)
willie.memory['url_callbacks'][regex] = show_bug
bot.memory['url_callbacks'][regex] = show_bug


def show_bug(willie, trigger, match=None):
@rule(r'.*https?://(\S+?)'
'(/show_bug.cgi\?\S*?)'
'(id=\d+).*')
def show_bug(bot, trigger, match=None):
"""Show information about a Bugzilla bug."""
match = match or trigger
domain = match.group(1)
if domain not in willie.config.bugzilla.get_list('domains'):
if domain not in bot.config.bugzilla.get_list('domains'):
return
url = 'https://%s%sctype=xml&%s' % match.groups()
data = web.get(url)
Expand All @@ -70,7 +74,4 @@ def show_bug(willie, trigger, match=None):
(bug.find('priority').text + ' ' + bug.find('bug_severity').text),
status, bug.find('assigned_to').text, bug.find('creation_ts').text,
bug.find('delta_ts').text)
willie.say(message)
show_bug.rule = (r'.*https?://(\S+?)'
'(/show_bug.cgi\?\S*?)'
'(id=\d+).*')
bot.say(message)
43 changes: 20 additions & 23 deletions willie/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"""

import re
import willie.web as web
from willie import web
from willie.module import command, commands, example
from socket import timeout
import string
import HTMLParser
Expand All @@ -35,39 +36,41 @@ def calculate(q):
return 'Sorry, no result.'


def c(willie, trigger):
@commands('c', 'calc')
@example('.c 5 + 3')
def c(bot, trigger):
"""Google calculator."""
if not trigger.group(2):
return willie.reply("Nothing to calculate.")
return bot.reply("Nothing to calculate.")
result = calculate(trigger.group(2))
willie.reply(result)
c.commands = ['c', 'calc']
c.example = '.c 5 + 3'
bot.reply(result)


def py(willie, trigger):
@command('py')
@example('.py len([1,2,3])')
def py(bot, trigger):
"""Evaluate a Python expression."""
query = trigger.group(2)
uri = 'http://tumbolia.appspot.com/py/'
answer = web.get(uri + web.quote(query))
if answer:
willie.say(answer)
bot.say(answer)
else:
willie.reply('Sorry, no result.')
py.commands = ['py']
py.example = '.py len([1,2,3])'
bot.reply('Sorry, no result.')


def wa(willie, trigger):
@commands('wa', 'wolfram')
@example('.wa circumference of the sun * pi')
def wa(bot, trigger):
"""Wolfram Alpha calculator"""
if not trigger.group(2):
return willie.reply("No search term.")
return bot.reply("No search term.")
query = trigger.group(2)
uri = 'http://tumbolia.appspot.com/wa/'
try:
answer = web.get(uri + web.quote(query.replace('+', '%2B')), 45)
except timeout as e:
return willie.say('[WOLFRAM ERROR] Request timed out')
return bot.say('[WOLFRAM ERROR] Request timed out')
if answer:
answer = answer.decode('string_escape')
answer = HTMLParser.HTMLParser().unescape(answer)
Expand All @@ -81,17 +84,11 @@ def wa(willie, trigger):
answer = answer.replace('\:' + char_code, char)
waOutputArray = string.split(answer, ";")
if(len(waOutputArray) < 2):
willie.say('[WOLFRAM ERROR]' + answer)
bot.say('[WOLFRAM ERROR]' + answer)
else:

willie.say('[WOLFRAM] ' + waOutputArray[0] + " = "
bot.say('[WOLFRAM] ' + waOutputArray[0] + " = "
+ waOutputArray[1])
waOutputArray = []
else:
willie.reply('Sorry, no result.')
wa.commands = ['wa', 'wolfram']
wa.example = '.wa circumference of the sun * pi'
wa.commands = ['wa']

if __name__ == '__main__':
print __doc__.strip()
bot.reply('Sorry, no result.')
86 changes: 44 additions & 42 deletions willie/modules/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from time import sleep
from xml.dom.minidom import parseString
import willie.web as web
from willie.module import command
import xml.dom.minidom


def configure(config):
"""
| [radio] | example | purpose |
Expand All @@ -23,83 +25,88 @@ def configure(config):
config.interactive_add('radio', 'url', 'URL to the ShoutCAST administration page', 'http://127.0.0.1:8000/')
config.interactive_add('radio', 'sid', 'Stream ID (only required for multi-stream servers.)', '1')

radioURL = '' # Set once, after the first .radio request.
radioURL = '' # Set once, after the first .radio request.
checkSongs = 0
current_song = ''

def getAPI(willie, trigger):

def getAPI(bot, trigger):
#contact the 'heavyweight' XML API
try:
raw = web.get(radioURL % 'stats')
except Exception as e:
willie.say('The radio is not responding to the stats request.')
bot.say('The radio is not responding to the stats request.')
return 0

#Parse the XML
XML = parseString(raw).documentElement
status = XML.getElementsByTagName('STREAMSTATUS')[0].firstChild.nodeValue
if status == '0':
willie.say('The radio is currently offline.')
bot.say('The radio is currently offline.')
return 0

status = 'Online'
servername = XML.getElementsByTagName('SERVERTITLE')[0].firstChild.nodeValue
curlist = XML.getElementsByTagName('CURRENTLISTENERS')[0].firstChild.nodeValue
maxlist = XML.getElementsByTagName('MAXLISTENERS')[0].firstChild.nodeValue

#Garbage disposal
XML.unlink()

#print results
willie.say('[%s]Status: %s. Listeners: %s/%s.' % (servername, status, curlist, maxlist))
bot.say('[%s]Status: %s. Listeners: %s/%s.' % (servername, status, curlist, maxlist))
return 1

def currentSong(willie, trigger):

def currentSong(bot, trigger):
# This function uses the PLAINTEXT API to get the current song only.
try:
song = web.get(radioURL % 'currentsong')
except Exception as e:
willie.say('The radio is not responding to the song request.')
willie.debug('radio', 'Exception while trying to get current song: %s' % e, 'warning')
bot.say('The radio is not responding to the song request.')
bot.debug('radio', 'Exception while trying to get current song: %s' % e, 'warning')
if song:
willie.say('Now playing: '+song)
bot.say('Now playing: ' + song)
else:
willie.say('The radio is currently offline.')
bot.say('The radio is currently offline.')

def nextSong(willie, trigger):

def nextSong(bot, trigger):
# This function uses the PLAINTEXT API to get the next song only.
try:
song = web.get(radioURL % 'nextsong')
except Exception as e:
willie.say('The radio is not responding to the song request.')
willie.debug('radio', 'Exception while trying to get next song: %s' % e, 'warning')
bot.say('The radio is not responding to the song request.')
bot.debug('radio', 'Exception while trying to get next song: %s' % e, 'warning')
if song:
willie.say('Next up: '+song)
bot.say('Next up: ' + song)
else:
willie.say('No songs are queued up.')
bot.say('No songs are queued up.')


def radio(willie, trigger):
@command('radio')
def radio(bot, trigger):
""" Radio functions, valid parameters: on, off, song, now, next, soon, stats. """
global checkSongs, current_song, radioURL
if not radioURL:
if not hasattr(willie.config, 'radio'):
willie.say('Radio module not configured')
if not hasattr(bot.config, 'radio'):
bot.say('Radio module not configured')
return
else:
radioURL = willie.config.radio.url+'%s?sid='+willie.config.radio.sid
radioURL = bot.config.radio.url + '%s?sid=' + bot.config.radio.sid
try:
args = trigger.group(2).lower().split(' ')
except AttributeError:
willie.say('Usage: .radio (next|now|off|on|song|soon|stats)')
bot.say('Usage: .radio (next|now|off|on|song|soon|stats)')
return
if args[0] == 'on':
if not trigger.isop:
return
if checkSongs != 0:
return willie.reply('Radio data checking is already on.')
if not getAPI(willie, trigger):
return bot.reply('Radio data checking is already on.')
if not getAPI(bot, trigger):
checkSongs = 0
return willie.say('Radio data checking not enabled.')
return bot.say('Radio data checking not enabled.')
checkSongs = 10
while checkSongs:
last = current_song
Expand All @@ -109,37 +116,32 @@ def radio(willie, trigger):
except Exception as e:
checkSongs -= 1
if checkSongs == 0:
willie.debug('radio', 'Exception while trying to get periodic radio data: %s' % e, 'warning')
willie.say('The radio is not responding to the song request.')
willie.say('Turning off radio data checking.')
bot.debug('radio', 'Exception while trying to get periodic radio data: %s' % e, 'warning')
bot.say('The radio is not responding to the song request.')
bot.say('Turning off radio data checking.')
break
if not current_song == last:
if not current_song:
csong = 'The radio is currently offline.'
else:
csong = 'Now Playing: '+current_song
csong = 'Now Playing: ' + current_song
if nextsong and current_song:
willie.say(csong+' | Coming Up: '+nextsong)
bot.say(csong + ' | Coming Up: ' + nextsong)
else:
willie.say(csong)
bot.say(csong)
sleep(5)
elif args[0] == 'off':
if not trigger.isop:
return;
return
if checkSongs == 0:
willie.reply('Radio data checking is already off.')
bot.reply('Radio data checking is already off.')
return
checkSongs = 0
current_song = ''
willie.reply('Turning off radio data checking.')
bot.reply('Turning off radio data checking.')
elif args[0] == 'song' or args[0] == 'now':
currentSong(willie, trigger)
currentSong(bot, trigger)
elif args[0] == 'next' or args[0] == 'soon':
nextSong(willie, trigger)
nextSong(bot, trigger)
elif args[0] == 'stats':
getAPI(willie, trigger)
radio.commands = ['radio']
radio.priority = 'medium'

if __name__ == '__main__':
print __doc__.strip()
getAPI(bot, trigger)

0 comments on commit d381f3e

Please sign in to comment.