chiark / gitweb /
Add twit command (patch from naath)
authorMatthew Vernon <matthewv@chiark.greenend.org.uk>
Wed, 2 Mar 2011 13:49:40 +0000 (13:49 +0000)
committerMatthew Vernon <matthewv@chiark.greenend.org.uk>
Wed, 2 Mar 2011 13:49:40 +0000 (13:49 +0000)
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
commands.py

index 2754828581505b2faec1fbfb4ca57549613e31b7..0af2dbce4424bc423676a1581ed22e6ba35b1bf3 100644 (file)
@@ -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:
index f869cd38e059a51022fc7b4f874f1737fe39fb66..32ac05e8b49bcac10dbbd635f81cc3eb89fa48bc 100644 (file)
@@ -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