From 0d67a17e46e92210a1d8c5d90b10be20211577a9 Mon Sep 17 00:00:00 2001 From: Matthew Vernon Date: Wed, 2 Mar 2011 13:49:40 +0000 Subject: [PATCH] Add twit command (patch from naath) This patch adds the "twit" command; it is mostly code by naath, but I fettled it a bit so we only ever gererate one twitter.Api object, and re-use that --- Servus-chiark.py | 10 +++++++--- commands.py | 27 ++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Servus-chiark.py b/Servus-chiark.py index 2754828..0af2dbc 100644 --- a/Servus-chiark.py +++ b/Servus-chiark.py @@ -35,7 +35,7 @@ owner = "Emperor" # Everything else in this file is configuration-specific. -import os, time, re +import os, time, re, twitter # Most command implementations are stored in a separate module. import commands as c @@ -150,6 +150,9 @@ expirelen=71*60*60 #do an expiry run every hour expirevery=60*60 +# non-authenticated twitter api instance +twitapi = twitter.Api() + # Command processing: whenever something is said that the bot can hear, # "command" is invoked and must decide what to do. This configuration # defines a couple of special cases (for karma) but is otherwise driven @@ -175,7 +178,8 @@ commands = {"karma": (c.karmaq,karmadb), "help": c.helpq, # "game": (c.gameq,game), "say": c.sayq, - "do": c.doq } + "do": c.doq, + "twit": (c.twitterq,twitapi) } # disconnect and hop annoy people # "disconnect": c.disconnq, # "hop": c.disconnq } @@ -185,7 +189,7 @@ triggers = ("!", "~") # what character should the bot be invoked by: # eg !trout, ~trout etc. def command(bot, cmd, nick, conn, public): - global urldb,lastexp,expirelen,expirevery + global urldb,lastexp,expirelen,expirevery,twitapi ours=0 try: if public and cmd[0] in triggers: diff --git a/commands.py b/commands.py index f869cd3..32ac05e 100644 --- a/commands.py +++ b/commands.py @@ -1,5 +1,5 @@ # Part of Acrobat. -import string, cPickle, random, urllib, sys, time, re, os +import string, cPickle, random, urllib, sys, time, re, os, twitter from irclib import irc_lower, nm_to_n # query karma @@ -387,3 +387,28 @@ def canonical_url(urlstring): urlstring.replace(middle,"/hi/") return urlstring + +#get tweet text +def twitterq(bot,cmd,nick,conn,public,twitapi): + + if (not urlre.search(cmd)): + bot.automsg(False,nick,"Please use 'twit' only with http URLs") + return + + urlstring = urlre.search(cmd).group(1) + if (urlstring.find("twitter.com") !=-1): + stringout = getTweet(urlstring,twitapi) + if public: + conn.action(bot.channel,stringout) + else: + bot.automsg(public, nick, stringout) + +def getTweet(urlstring,twitapi): + parts = string.split(urlstring,'/') + tweetID = parts[-1] + status = twitapi.GetStatus(tweetID) + tweeter_screen = status.user.screen_name + tweeter_name = status.user.name + tweetText = status.text + stringout = "tweet by %s (%s): %s" %(tweeter_screen,tweeter_name,tweetText) + return stringout -- 2.30.2