Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
New steam integration facility
  • Loading branch information
aquarion committed Nov 17, 2011
1 parent e339de1 commit ed476f0
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 8 deletions.
1 change: 1 addition & 0 deletions defaults.ini
Expand Up @@ -49,6 +49,7 @@ thanks = true
whatis = true
cutout = true
nano = true
play = true

[Haiku]
#Setting things here redirects haikus found in some places to other channels. Set to the magic value "ignore" to never see them
Expand Down
156 changes: 156 additions & 0 deletions lampstand/reactions/play.py
@@ -0,0 +1,156 @@
import re, time, random, sys, urllib, os, datetime
import lampstand.reactions.base

from xml.dom.minidom import parse, parseString

def __init__ ():
pass

class Reaction(lampstand.reactions.base.Reaction):
__name = 'Steam Play'

cooldown_number = 5
cooldown_time = 120
uses = []

def __init__(self, connection):

self.default = "chocolate";

self.channelMatch = (
re.compile('%s: what should I play\?' % connection.nickname, re.IGNORECASE),
re.compile('%s: my steam profile is (\S*)' % connection.nickname, re.IGNORECASE))

self.privateMatch = (
re.compile('what should I play\?', re.IGNORECASE),
re.compile('my steam profile is (\S*)', re.IGNORECASE))

self.dbconnection = connection.dbconnection


def channelAction(self, connection, user, channel, message, matchIndex = False):

if self.overUsed(self.uses):
connection.msg(channel, self.overuseReactions[matchIndex])
return True


## Overuse Detectection ##
self.uses.append(int(time.time()))
if len(self.uses) > self.cooldown_number:
self.uses = self.uses[0:self.cooldown_number-1]
## Overuse Detectection ##

if matchIndex == 0:
output = self.playWhat(user)
elif matchIndex == 1:
result = self.channelMatch[matchIndex].findall(message)
output = self.setSteam(user, result)

output = "%s: %s" % (user, output)

connection.msg(channel, output.encode("utf-8"))


def privateAction(self, connection, user, channel, message, matchIndex = False):

if matchIndex == 0:
connection.msg(user, self.playWhat(user).encode("utf-8"))
elif matchIndex == 1:
result = self.privateMatch[matchIndex].findall(message)
connection.msg(user, self.setSteam(user, result).encode("utf-8"))


def playWhat(self, username):

cursor = self.dbconnection.cursor()
cursor.execute('SELECT steamname from gameaccounts where username = %s', username)
result = cursor.fetchone()

if result == None:
return self.helptext()

steam = self.getSteamXML(result[0])

print steam

if hasattr(steam, '__getitem__'):
return steam[1]

return self.pickAGame(steam)

def helptext(self):

return """I don't have a steam account for you, sorry. Set this by saying "my steam profile is [SOMETHING]" to me. To find what '[SOMETHING]' should be log in to steamcommunity.com and it's the word after "/id/" or the numbers after "/profile/" in the URL of your home page."""


def setSteam(self, username, result):

steamname = result[0]

steam = self.getSteamXML(steamname)

if hasattr(steam, '__getitem__'):
return steam[1]

nameElement = steam.getElementsByTagName('steamID')[0]
accountName = nameElement.childNodes[0].data

cursor = self.dbconnection.cursor()
cursor.execute('REPLACE into gameaccounts (username, steamname) values (%s, %s)', (username, steamname) )
self.dbconnection.commit()

return "Okay, remembering that %s's steam name is %s, aka '%s'" % (username,steamname, accountName)

return "Stub %s" % result



def pickAGame(self, steam):

games = steam.getElementsByTagName('game')

game = random.choice(games)

gamename = game.getElementsByTagName('name')[0].childNodes[0].data

return gamename

def getSteamXML(self, username):

username = username.lower()

try:
i = int(username)
except ValueError, TypeError:
profiletype = 'id'
else:
profiletype = 'profiles'

steamurl = "http://steamcommunity.com/%s/%s/games?tab=all&xml=1" % (profiletype, username)

print steamurl

cachename = "/tmp/steam.lampstand.%s.xml" % username;

(fileopen, fileheaders) = urllib.urlretrieve(steamurl, cachename)

stat = os.stat(fileopen)
delta = datetime.datetime.now() - datetime.datetime.fromtimestamp(stat.st_mtime)
if delta.seconds > 60*60*12:
os.remove(fileopen)
(fileopen, fileheaders) = urllib.urlretrieve(steamurl, cachename)

try:
steam = parse(fileopen)
except ParseError:
return (16, "Error getting the response from steam")
except:
return (128, "Something crazy happened: <%s>" % sys.exc_info()[0])


steamerror = steam.getElementsByTagName('error')
if len(steamerror) > 0:
return (32, "Sorry, Steam said: %s" % steamerror[0].childNodes[0].data)

return steam
70 changes: 62 additions & 8 deletions schema.sql
Expand Up @@ -2,7 +2,7 @@
--
-- Host: localhost Database: maelfroth
-- ------------------------------------------------------
-- Server version 5.0.51a-24+lenny4-log
-- Server version 5.0.51a-24+lenny5-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
Expand Down Expand Up @@ -30,7 +30,7 @@ CREATE TABLE `define` (
`timestamp` tinytext NOT NULL,
PRIMARY KEY (`id`),
KEY `word` (`word`)
) ENGINE=MyISAM AUTO_INCREMENT=847 DEFAULT CHARSET=latin1;
) ENGINE=MyISAM AUTO_INCREMENT=1389 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
Expand All @@ -48,8 +48,23 @@ CREATE TABLE `events` (
`class` varchar(64) default NULL,
`datetime_end` datetime default NULL,
`aliases` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=104 DEFAULT CHARSET=latin1;
) ENGINE=MyISAM AUTO_INCREMENT=147 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
-- Table structure for table `gameaccounts`
--

DROP TABLE IF EXISTS `gameaccounts`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `gameaccounts` (
`username` varchar(255) NOT NULL,
`steamname` varchar(255) NOT NULL,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
Expand Down Expand Up @@ -82,7 +97,7 @@ CREATE TABLE `item` (
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `item` (`item`)
) ENGINE=MyISAM AUTO_INCREMENT=300 DEFAULT CHARSET=latin1;
) ENGINE=MyISAM AUTO_INCREMENT=598 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
Expand Down Expand Up @@ -132,6 +147,45 @@ CREATE TABLE `nickserv` (
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
-- Table structure for table `part`
--

DROP TABLE IF EXISTS `part`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `part` (
`id` int(11) NOT NULL auto_increment,
`title` tinytext NOT NULL,
`author` int(10) unsigned default NULL,
`dateadded` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`content` mediumtext,
`coolness` int(11) default NULL,
`votes` mediumtext,
`restricted` bit(1) default NULL,
`story` int(10) unsigned default '0',
`backto` int(10) unsigned default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
-- Table structure for table `partlink`
--

DROP TABLE IF EXISTS `partlink`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `partlink` (
`id` int(11) NOT NULL auto_increment,
`linkto` int(11) NOT NULL,
`linkfrom` int(11) NOT NULL,
`linkname` tinytext NOT NULL,
`linklock` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
-- Table structure for table `revision`
--
Expand All @@ -147,7 +201,7 @@ CREATE TABLE `revision` (
`creator` tinytext NOT NULL,
`created` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`revision`)
) ENGINE=MyISAM AUTO_INCREMENT=354 DEFAULT CHARSET=latin1;
) ENGINE=MyISAM AUTO_INCREMENT=501 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
Expand Down Expand Up @@ -184,7 +238,7 @@ CREATE TABLE `users` (
`access_level` int(11) default '0',
`creationsite` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=59 DEFAULT CHARSET=latin1;
) ENGINE=MyISAM AUTO_INCREMENT=84 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;

--
Expand Down Expand Up @@ -222,7 +276,7 @@ CREATE TABLE `wikipage` (
`origin` tinytext,
`yalelock` tinytext,
PRIMARY KEY (`page`)
) ENGINE=MyISAM AUTO_INCREMENT=113 DEFAULT CHARSET=latin1;
) ENGINE=MyISAM AUTO_INCREMENT=131 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

Expand All @@ -234,4 +288,4 @@ SET character_set_client = @saved_cs_client;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2010-07-17 1:41:56
-- Dump completed on 2011-11-17 10:24:29

0 comments on commit ed476f0

Please sign in to comment.