Skip to content

Commit

Permalink
[ping, isup, rand, tld, wiktionary] Update to 4.0 API
Browse files Browse the repository at this point in the history
Also clean up a bit, and add an example or two

Issues sopel-irc#125 and sopel-irc#276
  • Loading branch information
embolalia committed Jun 22, 2013
1 parent ce2ca64 commit 2cb10cf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
6 changes: 4 additions & 2 deletions isup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
This allows users to check if a website is up through isup.me.
"""

import willie.web as web
from willie import web
from willie.module import commands
import re


@commands('isup')
def isup(willie, trigger):
"""isup.me website status checker"""
site = trigger.group(2)
Expand All @@ -31,4 +34,3 @@ def isup(willie, trigger):
willie.say(site + ' looks fine to me.')
else:
willie.say(site + ' is down from here.')
isup.commands = ['isup']
34 changes: 18 additions & 16 deletions ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
"""

import random
from willie.module import rule, priority, thread

def hello(willie, trigger):
if trigger.owner:
greeting = random.choice(('Fuck off,', 'Screw you,', 'Go away'))
else: greeting = random.choice(('Hi', 'Hey', 'Hello'))
punctuation = random.choice(('', '!'))
willie.say(greeting + ' ' + trigger.nick + punctuation)
hello.rule = r'(?i)(hi|hello|hey) $nickname[ \t]*$'

@rule(r'(?i)(hi|hello|hey) $nickname[ \t]*$')
def hello(willie, trigger):
if trigger.owner:
greeting = random.choice(('Fuck off,', 'Screw you,', 'Go away'))
else:
greeting = random.choice(('Hi', 'Hey', 'Hello'))
punctuation = random.choice(('', '!'))
willie.say(greeting + ' ' + trigger.nick + punctuation)


@rule(r'(?i)(Fuck|Screw) you, $nickname[ \t]*$')
def rude(willie, trigger):
willie.say('Watch your mouth, ' + trigger.nick + ', or I\'ll tell your mother!')
rude.rule = r'(?i)(Fuck|Screw) you, $nickname[ \t]*$'
willie.say('Watch your mouth, ' + trigger.nick + ', or I\'ll tell your mother!')

def interjection(willie, trigger):
willie.say(trigger.nick + '!')
interjection.rule = r'$nickname!'
interjection.priority = 'high'
interjection.thread = False

if __name__ == '__main__':
print __doc__.strip()
@rule('$nickname!')
@priority('high')
@thread(False)
def interjection(willie, trigger):
willie.say(trigger.nick + '!')
16 changes: 7 additions & 9 deletions rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
http://willie.dftba.net
"""

from willie.module import commands, example
import random
import re


@commands('rand')
@example('.rand 1 100')
def rand(willie, trigger):
""".rand <arg1> <arg2> - Generates a random integer between <arg1> and <arg2>."""
if trigger.group(2) == " " or trigger.group(2) == "" or str(trigger.group(2)) == None or str(trigger.group(2)) == "" or trigger.group(2) == None:
"""Generates a random integer between <arg1> and <arg2>."""
if not trigger.group(2):
willie.say("I'm sorry, " + str(trigger.nick) + ", but you must enter at least one number.")
else:
random.seed()
Expand All @@ -29,7 +33,7 @@ def rand(willie, trigger):
randinte = random.randint(0, a)
willie.say(str(trigger.nick) + ": your random integer is: " + str(randinte))
else:
a,b = li_integers.split()
a, b = li_integers.split()
a = re.sub(r'\D', '', str(a))
b = re.sub(r'\D', '', str(b))
a = int(a)
Expand All @@ -39,9 +43,3 @@ def rand(willie, trigger):
else:
randinte = random.randint(b, a)
willie.say(str(trigger.nick) + ": your random integer is: " + str(randinte))

rand.commands = ['rand']
rand.priority = 'medium'

if __name__ == '__main__':
print __doc__.strip()
14 changes: 7 additions & 7 deletions tld.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
http://willie.dftba.net
"""

import re, urllib2
import willie.web as web
from willie import web
from willie.module import commands, example
import re
import urllib2

uri = 'https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains'
r_tag = re.compile(r'<(?!!)[^>]+>')


@commands('tld')
@example('.tld ru')
def gettld(willie, trigger):
"""Show information about the given Top Level Domain."""
page = web.get(uri)
Expand Down Expand Up @@ -55,8 +60,3 @@ def gettld(willie, trigger):
else:
reply = "No matches found for TLD: {0}".format(unicode(trigger.group(2)))
willie.reply(reply)
gettld.commands = ['tld']
gettld.thread = False

if __name__ == '__main__':
print __doc__.strip()
16 changes: 9 additions & 7 deletions wiktionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"""

import re
import willie.web as web
from willie import web
from willie.module import commands, example

uri = 'http://en.wiktionary.org/w/index.php?title=%s&printable=yes'
r_tag = re.compile(r'<[^>]+>')
r_ul = re.compile(r'(?ims)<ul>.*?</ul>')


def text(html):
text = r_tag.sub('', html).strip()
text = text.replace('\n', ' ')
Expand All @@ -21,6 +23,7 @@ def text(html):
text = text.replace('(transitive', '(trans.')
return text


def wikt(word):
bytes = web.get(uri % web.quote(word.encode('utf-8')))
bytes = r_ul.sub('', bytes)
Expand Down Expand Up @@ -60,16 +63,20 @@ def wikt(word):
parts = ('preposition', 'particle', 'noun', 'verb',
'adjective', 'adverb', 'interjection')


def format(word, definitions, number=2):
result = '%s' % word.encode('utf-8')
for part in parts:
if definitions.has_key(part):
if part in definitions:
defs = definitions[part][:number]
result += u' \u2014 '.encode('utf-8') + ('%s: ' % part)
n = ['%s. %s' % (i + 1, e.strip(' .')) for i, e in enumerate(defs)]
result += ', '.join(n)
return result.strip(' .,')


@commands('wt', 'define', 'dict')
@example('.wt bailiwick')
def wiktionary(willie, trigger):
"""Look up a word on Wiktionary."""
word = trigger.group(2)
Expand All @@ -91,8 +98,3 @@ def wiktionary(willie, trigger):
if len(result) > 300:
result = result[:295] + '[...]'
willie.say(result.decode('utf8'))
wiktionary.commands = ['wt', 'define', 'dict']
wiktionary.example = '.wt bailiwick'

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

0 comments on commit 2cb10cf

Please sign in to comment.