]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/ircbot.py
Merge pull request #50 from geeknikbrian/master
[z_archive/twitter.git] / twitter / ircbot.py
index 873850d8715e3805e711e8877d926897de660eb9..9310d0c05278fcb0e81b02fc27663dd5917532f3 100644 (file)
@@ -36,7 +36,7 @@ oauth_token_file: <oauth_token_filename>
 
 from __future__ import print_function
 
-BOT_VERSION = "TwitterBot 1.4 (http://mike.verdone.ca/twitter)"
+BOT_VERSION = "TwitterBot 1.6.1 (http://mike.verdone.ca/twitter)"
 
 CONSUMER_KEY = "XryIxN3J2ACaJs50EizfLQ"
 CONSUMER_SECRET = "j7IuDCNjftVY8DBauRdqXs4jDl5Fgk1IJRag8iE"
@@ -176,17 +176,17 @@ class TwitterBot(object):
             if (crt > nextLastUpdate):
                 text = (htmlentitydecode(
                     update['text'].replace('\n', ' '))
-                    .encode('utf-8', 'replace'))
+                    .encode('utf8', 'replace'))
 
                 # Skip updates beginning with @
                 # TODO This would be better if we only ignored messages
                 #   to people who are not on our following list.
                 if not text.startswith(b"@"):
-                    self.privmsg_channels(
-                        "%s %s%s%s %s" %(
-                            get_prefix(),
-                            IRC_BOLD, update['user']['screen_name'],
-                            IRC_BOLD, text.decode('utf-8')))
+                    msg = "%s %s%s%s %s" %(
+                        get_prefix(),
+                        IRC_BOLD, update['user']['screen_name'],
+                        IRC_BOLD, text.decode('utf8'))
+                    self.privmsg_channels(msg)
 
                 nextLastUpdate = crt
 
@@ -226,14 +226,10 @@ class TwitterBot(object):
             elif args[0] == 'CLIENTINFO':
                 conn.ctcp_reply(source, "CLIENTINFO PING VERSION CLIENTINFO")
 
-    def privmsg_channel(self, msg):
-        return self.ircServer.privmsg(
-            self.config.get('irc', 'channel'), msg)
-
     def privmsg_channels(self, msg):
         return_response=True
         channels=self.config.get('irc','channel').split(',')
-        return self.ircServer.privmsg_many(channels, msg)
+        return self.ircServer.privmsg_many(channels, msg.encode('utf8'))
 
     def follow(self, conn, evt, name):
         userNick = evt.source().split('!')[0]
@@ -278,7 +274,7 @@ class TwitterBot(object):
                 "%s%s has asked me to stop following %s" %(
                     get_prefix('inform'), userNick, name))
 
-    def run(self):
+    def _irc_connect(self):
         self.ircServer.connect(
             self.config.get('irc', 'server'),
             self.config.getint('irc', 'port'),
@@ -287,6 +283,9 @@ class TwitterBot(object):
         for channel in channels:
             self.ircServer.join(channel)
 
+    def run(self):
+        self._irc_connect()
+
         while True:
             try:
                 self.sched.run_forever()
@@ -296,6 +295,10 @@ class TwitterBot(object):
                 # twitter.com is probably down because it
                 # sucks. ignore the fault and keep going
                 pass
+            except irclib.ServerNotConnectedError:
+                # Try and reconnect to IRC.
+                self._irc_connect()
+
 
 def load_config(filename):
     # Note: Python ConfigParser module has the worst interface in the