X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/1e28b4c31f31bcb6cb8d8dcdf883fbed4e79e43b..ea5231b2fe0f40d87c9c7991b949c39c7cddd7e3:/twitter/ircbot.py diff --git a/twitter/ircbot.py b/twitter/ircbot.py index ee6adee..873850d 100644 --- a/twitter/ircbot.py +++ b/twitter/ircbot.py @@ -34,6 +34,8 @@ oauth_token_file: """ +from __future__ import print_function + BOT_VERSION = "TwitterBot 1.4 (http://mike.verdone.ca/twitter)" CONSUMER_KEY = "XryIxN3J2ACaJs50EizfLQ" @@ -48,7 +50,10 @@ import sys import time from datetime import datetime, timedelta from email.utils import parsedate -from ConfigParser import SafeConfigParser +try: + from configparser import ConfigParser +except ImportError: + from ConfigParser import ConfigParser from heapq import heappop, heappush import traceback import os @@ -77,16 +82,16 @@ def get_prefix(prefix_typ=None): try: import irclib -except: +except ImportError: raise ImportError( "This module requires python irclib available from " - + "http://python-irclib.sourceforge.net/") + + "https://github.com/sixohsix/python-irclib/zipball/python-irclib3-0.4.8") OAUTH_FILE = os.environ.get('HOME', '') + os.sep + '.twitterbot_oauth' def debug(msg): # uncomment this for debug text stuff - # print >> sys.stderr, msg + # print(msg, file=sys.stdout) pass class SchedTask(object): @@ -99,8 +104,8 @@ class SchedTask(object): return "" %( self.task.__name__, self.__next__, self.delta) - def __cmp__(self, other): - return cmp(self.__next__, other.__next__) + def __lt__(self, other): + return self.next < other.next def __call__(self): return self.task() @@ -114,7 +119,7 @@ class Scheduler(object): def next_task(self): now = time.time() task = heappop(self.task_heap) - wait = task.__next__ - now + wait = task.next - now task.next = now + task.delta heappush(self.task_heap, task) if (wait > 0): @@ -154,7 +159,7 @@ class TwitterBot(object): self.sched = Scheduler( (SchedTask(self.process_events, 1), SchedTask(self.check_statuses, 120))) - self.lastUpdate = (datetime.now() - timedelta(minutes=10)).utctimetuple() + self.lastUpdate = (datetime.utcnow() - timedelta(minutes=10)).utctimetuple() def check_statuses(self): debug("In check_statuses") @@ -166,7 +171,6 @@ class TwitterBot(object): return nextLastUpdate = self.lastUpdate - debug("self.lastUpdate is %s" % self.lastUpdate) for update in updates: crt = parsedate(update['created_at']) if (crt > nextLastUpdate): @@ -177,19 +181,15 @@ class TwitterBot(object): # 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("@"): + 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'))) - debug("tweet has crt %s, updating nextLastUpdate (was %s)" %( - crt, nextLastUpdate, - )) nextLastUpdate = crt - debug("setting self.lastUpdate to %s" % nextLastUpdate) self.lastUpdate = nextLastUpdate def process_events(self): @@ -209,8 +209,8 @@ class TwitterBot(object): conn.privmsg( evt.source().split('!')[0], "%sHi! I'm Twitterbot! you can (follow " - + ") to make me follow a user or " - + "(unfollow ) to make me stop." % + ") to make me follow a user or " + "(unfollow ) to make me stop." % get_prefix()) except Exception: traceback.print_exc(file=sys.stderr) @@ -228,12 +228,12 @@ class TwitterBot(object): def privmsg_channel(self, msg): return self.ircServer.privmsg( - self.config.get('irc', 'channel'), msg.encode('utf-8')) + 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.encode('utf-8')) + return self.ircServer.privmsg_many(channels, msg) def follow(self, conn, evt, name): userNick = evt.source().split('!')[0] @@ -300,7 +300,7 @@ class TwitterBot(object): def load_config(filename): # Note: Python ConfigParser module has the worst interface in the # world. Mega gross. - cp = SafeConfigParser() + cp = ConfigParser() cp.add_section('irc') cp.set('irc', 'port', '6667') cp.set('irc', 'nick', 'twitterbot')