2003-08-11 07:22:15 +02:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-10-16 16:53:42 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
2003-08-11 07:22:15 +02:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Handles URL snarfing for Gameknot.com and the gkstats command.
|
|
|
|
"""
|
|
|
|
|
2003-11-25 09:23:47 +01:00
|
|
|
__revision__ = "$Id$"
|
2003-11-25 00:07:51 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
2003-08-11 07:22:15 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sets
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.registry as registry
|
2004-01-21 03:19:47 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
2004-10-16 04:52:31 +02:00
|
|
|
from supybot.commands import *
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
2004-08-05 20:29:26 +02:00
|
|
|
import supybot.webutils as webutils
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.callbacks as callbacks
|
2003-08-11 07:22:15 +02:00
|
|
|
|
|
|
|
|
2004-01-30 00:58:27 +01:00
|
|
|
def configure(advanced):
|
2004-07-25 20:24:51 +02:00
|
|
|
from supybot.questions import output, expect, anything, something, yn
|
2004-01-28 23:19:25 +01:00
|
|
|
conf.registerPlugin('Gameknot', True)
|
2003-10-29 07:06:56 +01:00
|
|
|
if advanced:
|
2004-01-31 23:24:43 +01:00
|
|
|
output("""The Gameknot plugin has the functionality to watch for URLs
|
|
|
|
that match a specific pattern (we call this a snarfer). When
|
|
|
|
supybot sees such a URL, he will parse the web page for
|
|
|
|
information and reply with the results.""")
|
|
|
|
if yn('Do you want the Gameknot stats snarfer enabled by default?',
|
|
|
|
default=False):
|
2004-01-21 03:19:47 +01:00
|
|
|
conf.supybot.plugins.Gameknot.statSnarfer.setValue(True)
|
2004-01-09 00:03:48 +01:00
|
|
|
if yn('Do you want the Gameknot Game links snarfer enabled by '
|
2004-01-31 23:24:43 +01:00
|
|
|
'default?', default=False):
|
2004-01-21 03:19:47 +01:00
|
|
|
conf.supybot.plugins.Gameknot.gameSnarfer.setValue(True)
|
|
|
|
|
|
|
|
conf.registerPlugin('Gameknot')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Gameknot, 'gameSnarfer',
|
|
|
|
registry.Boolean(False, """Determines whether the game URL snarfer is
|
|
|
|
enabled. If so, the bot will reply to the channel with a summary of the
|
|
|
|
game data when it sees a Gameknot game link on the channel."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Gameknot, 'statSnarfer',
|
|
|
|
registry.Boolean(False, """Determines whether the stats URL snarfer is
|
|
|
|
enabled. If so, the bot will reply to the channel with a summary of the
|
|
|
|
stats of any player whose stats URL is seen on the channel."""))
|
|
|
|
|
|
|
|
class Gameknot(callbacks.PrivmsgCommandAndRegexp):
|
2003-08-11 07:22:15 +02:00
|
|
|
threaded = True
|
2004-09-10 08:36:27 +02:00
|
|
|
callBefore = ['URL']
|
2003-10-21 23:10:20 +02:00
|
|
|
regexps = ['gameknotSnarfer', 'gameknotStatsSnarfer']
|
2003-08-11 07:22:15 +02:00
|
|
|
_gkrating = re.compile(r'<font color="#FFFF33">(\d+)</font>')
|
2003-11-05 00:04:38 +01:00
|
|
|
_gkgames = re.compile(r's:</td><td class=sml>(\d+)</td></tr>')
|
2004-01-09 00:03:48 +01:00
|
|
|
_gkrecord = re.compile(r'"#FFFF00">(\d+)[^"]+"#FFFF00">(\d+)[^"]+'
|
|
|
|
r'"#FFFF00">(\d+)')
|
2003-08-11 07:22:15 +02:00
|
|
|
_gkteam = re.compile(r'Team:(<.*?>)+(?P<name>.*?)</span>')
|
2003-11-06 22:33:50 +01:00
|
|
|
_gkseen = re.compile(r'(seen on GK:\s+([^[]+ago)|.*?is hiding.*?)')
|
2003-11-11 16:58:20 +01:00
|
|
|
|
2003-08-18 00:01:26 +02:00
|
|
|
def getStats(self, name):
|
2003-08-11 07:22:15 +02:00
|
|
|
gkprofile = 'http://www.gameknot.com/stats.pl?%s' % name
|
|
|
|
try:
|
2004-08-05 20:29:26 +02:00
|
|
|
profile = webutils.getUrl(gkprofile)
|
2003-08-11 07:22:15 +02:00
|
|
|
rating = self._gkrating.search(profile).group(1)
|
|
|
|
games = self._gkgames.search(profile).group(1)
|
|
|
|
(w, l, d) = self._gkrecord.search(profile).groups()
|
2003-08-19 00:20:48 +02:00
|
|
|
try:
|
|
|
|
w = int(w)
|
|
|
|
l = int(l)
|
|
|
|
d = int(d)
|
|
|
|
wp = 100. * w / (w + l + d) # win percent
|
|
|
|
lp = 100. * l / (w + l + d) # loss percent
|
|
|
|
dp = 100. * d / (w + l + d) # draw percent
|
2003-11-24 10:40:27 +01:00
|
|
|
except (ValueError, ZeroDivisionError):
|
2003-08-19 00:20:48 +02:00
|
|
|
w = w
|
|
|
|
wp = 0.
|
|
|
|
l = l
|
|
|
|
lp = 0.
|
|
|
|
d = d
|
|
|
|
dp = 0.
|
2003-08-11 07:22:15 +02:00
|
|
|
seen = self._gkseen.search(utils.htmlToText(profile))
|
2003-10-28 00:22:35 +01:00
|
|
|
if seen is None:
|
|
|
|
seen = ''
|
|
|
|
elif 'is hiding' in seen.group(0):
|
2003-08-11 07:22:15 +02:00
|
|
|
seen = '%s is hiding his/her online status.' % name
|
|
|
|
elif seen.group(2).startswith('0'):
|
|
|
|
seen = '%s is on gameknot right now.' % name
|
|
|
|
else:
|
|
|
|
seen = '%s was last seen on Gameknot %s.' % (name,
|
|
|
|
seen.group(2))
|
2004-08-20 06:10:32 +02:00
|
|
|
games = utils.nItems('game', int(games), between='active')
|
2003-08-11 19:16:03 +02:00
|
|
|
if 'Team:' in profile:
|
2003-08-11 07:22:15 +02:00
|
|
|
team = self._gkteam.search(profile).group('name')
|
2004-02-09 17:42:28 +01:00
|
|
|
team = utils.htmlToText(team)
|
2003-08-18 00:01:26 +02:00
|
|
|
s = '%s (team: %s) is rated %s and has %s ' \
|
2003-10-27 06:13:31 +01:00
|
|
|
'and a record of %s, %s, and %s ' \
|
|
|
|
'(win/loss/draw percentage: %.2f%%/%.2f%%/%.2f%%). %s' % \
|
|
|
|
(name, team, rating, games,
|
2003-12-12 16:41:33 +01:00
|
|
|
utils.nItems('win', w),
|
|
|
|
utils.nItems('loss', l),
|
|
|
|
utils.nItems('draw', d),
|
2003-10-27 06:13:31 +01:00
|
|
|
wp, lp, dp, seen)
|
2003-08-11 07:22:15 +02:00
|
|
|
else:
|
2003-08-18 00:01:26 +02:00
|
|
|
s = '%s is rated %s and has %s ' \
|
2003-10-27 06:13:31 +01:00
|
|
|
'and a record of %s, %s, and %s ' \
|
|
|
|
'(win/loss/draw percentage: %.2f%%/%.2f%%/%.2f%%). %s' % \
|
|
|
|
(name, rating, games,
|
2003-12-12 16:41:33 +01:00
|
|
|
utils.nItems('win', w),
|
|
|
|
utils.nItems('loss', l),
|
|
|
|
utils.nItems('draw', d),
|
2003-10-27 06:13:31 +01:00
|
|
|
wp, lp, dp, seen)
|
2003-08-18 00:01:26 +02:00
|
|
|
return s
|
2003-08-11 07:22:15 +02:00
|
|
|
except AttributeError:
|
2004-08-01 16:38:37 +02:00
|
|
|
if ('User %s not found!' % name.lower()) in profile:
|
2003-08-18 00:01:26 +02:00
|
|
|
raise callbacks.Error, 'No user %s exists.' % name
|
2003-08-11 07:22:15 +02:00
|
|
|
else:
|
2004-01-09 00:03:48 +01:00
|
|
|
raise callbacks.Error,'The format of the page was odd. %s' % \
|
2004-01-18 08:58:26 +01:00
|
|
|
conf.supybot.replies.possibleBug()
|
2004-08-05 20:29:26 +02:00
|
|
|
except webutils.WebError, e:
|
|
|
|
raise callbacks.Error, webutils.strError(e)
|
2003-08-20 18:26:23 +02:00
|
|
|
|
|
|
|
|
2004-10-16 04:52:31 +02:00
|
|
|
def gkstats(self, irc, msg, args, name):
|
2003-08-18 00:01:26 +02:00
|
|
|
"""<name>
|
|
|
|
|
|
|
|
Returns the stats Gameknot keeps on <name>. Gameknot is an online
|
|
|
|
website for playing chess (rather similar to correspondence chess, just
|
|
|
|
somewhat faster) against players from all over the world.
|
|
|
|
"""
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(self.getStats(name))
|
2004-10-16 04:52:31 +02:00
|
|
|
gkstats = wrap(gkstats, ['text'])
|
2003-08-11 07:22:15 +02:00
|
|
|
|
|
|
|
_gkPlayer = re.compile(r"popd\('(Rating[^']+)'\).*?>([^<]+)<")
|
|
|
|
_gkRating = re.compile(r": (\d+)[^:]+:<br>(\d+)[^,]+, (\d+)[^,]+, (\d+)")
|
2004-08-01 16:38:37 +02:00
|
|
|
_gkGameTitle = re.compile(r"<td[^<]+><p><b>(.*?)\s*</b> ")
|
2003-08-18 08:58:37 +02:00
|
|
|
_gkWon = re.compile(r'>(\S+)\s+won')
|
|
|
|
_gkReason = re.compile(r'won\s+\(\S+\s+(\S+)\)')
|
2003-08-11 07:22:15 +02:00
|
|
|
def gameknotSnarfer(self, irc, msg, match):
|
2003-10-17 16:23:51 +02:00
|
|
|
r"http://(?:www\.)?gameknot\.com/chess\.pl\?bd=\d+(&r=\d+)?"
|
2004-01-21 03:19:47 +01:00
|
|
|
if not self.registryValue('gameSnarfer', msg.args[0]):
|
2003-10-21 18:43:02 +02:00
|
|
|
return
|
2003-08-11 07:22:15 +02:00
|
|
|
url = match.group(0)
|
2004-08-05 20:29:26 +02:00
|
|
|
s = webutils.getUrl(url)
|
2003-08-11 07:22:15 +02:00
|
|
|
try:
|
2003-11-25 00:07:51 +01:00
|
|
|
if 'no longer available' in s:
|
|
|
|
s = 'That game is no longer available.'
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s, prefixName=True)
|
2003-11-25 00:07:51 +01:00
|
|
|
return
|
|
|
|
m = self._gkGameTitle.search(s)
|
|
|
|
if m is None:
|
2005-01-04 19:44:33 +01:00
|
|
|
self.log.debug('_gkGameTitle didn\'t match (%s).', url)
|
2003-11-25 00:07:51 +01:00
|
|
|
return
|
|
|
|
gameTitle = m.groups()
|
2003-08-11 07:22:15 +02:00
|
|
|
gameTitle = ircutils.bold(gameTitle)
|
2003-11-25 00:07:51 +01:00
|
|
|
L = self._gkPlayer.findall(s)
|
|
|
|
if not L:
|
2005-01-04 19:44:33 +01:00
|
|
|
self.log.debug('_gkPlayer didn\'t match (%s).', url)
|
2003-11-25 00:07:51 +01:00
|
|
|
return
|
|
|
|
((wRating, wName), (bRating, bName)) = L
|
2003-08-11 07:22:15 +02:00
|
|
|
wName = ircutils.bold(wName)
|
|
|
|
bName = ircutils.bold(bName)
|
2003-08-18 08:41:09 +02:00
|
|
|
if 'to move...' in s:
|
|
|
|
if 'white to move' in s:
|
|
|
|
toMove = wName + ' to move.'
|
|
|
|
else:
|
|
|
|
toMove = bName + ' to move.'
|
|
|
|
else:
|
|
|
|
# Game is over.
|
|
|
|
m = self._gkWon.search(s)
|
2003-08-18 08:58:37 +02:00
|
|
|
if m:
|
2003-09-17 23:31:04 +02:00
|
|
|
winner = m.group(1)
|
|
|
|
m = self._gkReason.search(s)
|
|
|
|
if m:
|
|
|
|
reason = m.group(1)
|
|
|
|
else:
|
|
|
|
reason = 'lost'
|
|
|
|
if winner == 'white':
|
|
|
|
toMove = '%s won, %s %s.' % (wName, bName, reason)
|
|
|
|
else:
|
|
|
|
toMove = '%s won, %s %s.' % (bName, wName, reason)
|
2003-08-18 08:58:37 +02:00
|
|
|
else:
|
2003-09-17 23:31:04 +02:00
|
|
|
toMove = 'The game was a draw.'
|
2003-08-11 07:22:15 +02:00
|
|
|
(wRating, wWins, wLosses, wDraws) = \
|
|
|
|
self._gkRating.search(wRating).groups()
|
|
|
|
(bRating, bWins, bLosses, bDraws) = \
|
|
|
|
self._gkRating.search(bRating).groups()
|
|
|
|
wStats = '%s; W-%s, L-%s, D-%s' % (wRating, wWins, wLosses, wDraws)
|
|
|
|
bStats = '%s; W-%s, L-%s, D-%s' % (bRating, bWins, bLosses, bDraws)
|
2003-09-23 22:45:25 +02:00
|
|
|
s = '%s: %s (%s) vs. %s (%s); %s' % \
|
|
|
|
(gameTitle, wName, wStats, bName, bStats, toMove)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s, prefixName=False)
|
2003-08-11 07:22:15 +02:00
|
|
|
except ValueError:
|
2005-01-04 19:44:33 +01:00
|
|
|
s = '%s doesn\'t appear to be a proper Gameknot game.' % url
|
|
|
|
self.log.debug('Unable to snarf. %s' % s)
|
2003-08-11 07:22:15 +02:00
|
|
|
except Exception, e:
|
2005-01-04 19:44:33 +01:00
|
|
|
self.log.warning(utils.exnToString(e))
|
2004-10-22 07:56:55 +02:00
|
|
|
gameknotSnarfer = urlSnarfer(gameknotSnarfer)
|
2003-08-11 07:22:15 +02:00
|
|
|
|
2003-08-18 00:01:26 +02:00
|
|
|
def gameknotStatsSnarfer(self, irc, msg, match):
|
2004-12-29 23:30:54 +01:00
|
|
|
r"http://gameknot\.com/stats\.pl\?([^& ]+)"
|
2004-01-21 03:19:47 +01:00
|
|
|
if not self.registryValue('statSnarfer', msg.args[0]):
|
2003-10-21 18:43:02 +02:00
|
|
|
return
|
2003-08-18 00:01:26 +02:00
|
|
|
name = match.group(1)
|
|
|
|
s = self.getStats(name)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s, prefixName=False)
|
2004-10-22 07:56:55 +02:00
|
|
|
gameknotStatsSnarfer = urlSnarfer(gameknotStatsSnarfer)
|
2003-08-18 00:01:26 +02:00
|
|
|
|
2003-08-11 07:22:15 +02:00
|
|
|
Class = Gameknot
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|