X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/0bcc8bb9d00268d7a50763cd33d1a6cd391536a4..0f27320be29f6111de94f642867435b7eb59825c:/twitter/ircbot.py diff --git a/twitter/ircbot.py b/twitter/ircbot.py index 1c2a649..6091417 100644 --- a/twitter/ircbot.py +++ b/twitter/ircbot.py @@ -34,7 +34,9 @@ oauth_token_file: """ -BOT_VERSION = "TwitterBot 1.4 (http://mike.verdone.ca/twitter)" +from __future__ import print_function + +BOT_VERSION = "TwitterBot 1.9.1 (http://mike.verdone.ca/twitter)" CONSUMER_KEY = "XryIxN3J2ACaJs50EizfLQ" CONSUMER_SECRET = "j7IuDCNjftVY8DBauRdqXs4jDl5Fgk1IJRag8iE" @@ -46,27 +48,31 @@ IRC_REGULAR = chr(0x0f) import sys import time -from dateutil.parser import parse -from ConfigParser import SafeConfigParser +from datetime import datetime, timedelta +from email.utils import parsedate +try: + from configparser import ConfigParser +except ImportError: + from ConfigParser import ConfigParser from heapq import heappop, heappush import traceback import os import os.path -from api import Twitter, TwitterError -from oauth import OAuth, read_token_file -from oauth_dance import oauth_dance -from util import htmlentitydecode +from .api import Twitter, TwitterError +from .oauth import OAuth, read_token_file +from .oauth_dance import oauth_dance +from .util import htmlentitydecode PREFIXES = dict( cats=dict( new_tweet="=^_^= ", error="=O_o= ", inform="=o_o= " - ) + ), none=dict( new_tweet="" - ) + ), ) ACTIVE_PREFIXES=dict() @@ -76,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' +OAUTH_FILE = os.environ.get('HOME', os.environ.get('USERPROFILE', '')) + 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): @@ -96,10 +102,10 @@ class SchedTask(object): def __repr__(self): return "" %( - self.task.__name__, self.next, self.delta) + 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() @@ -119,7 +125,7 @@ class Scheduler(object): if (wait > 0): time.sleep(wait) task() - debug("tasks: " + str(self.task_heap)) + #debug("tasks: " + str(self.task_heap)) def run_forever(self): while True: @@ -131,6 +137,9 @@ class TwitterBot(object): self.configFilename = configFilename self.config = load_config(self.configFilename) + global ACTIVE_PREFIXES + ACTIVE_PREFIXES = PREFIXES[self.config.get('irc', 'prefixes')] + oauth_file = self.config.get('twitter', 'oauth_token_file') if not os.path.exists(oauth_file): oauth_dance("IRC Bot", CONSUMER_KEY, CONSUMER_SECRET, oauth_file) @@ -139,53 +148,51 @@ class TwitterBot(object): self.twitter = Twitter( auth=OAuth( oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET), - api_version='1', domain='api.twitter.com') self.irc = irclib.IRC() self.irc.add_global_handler('privmsg', self.handle_privmsg) self.irc.add_global_handler('ctcp', self.handle_ctcp) + self.irc.add_global_handler('umode', self.handle_umode) self.ircServer = self.irc.server() self.sched = Scheduler( (SchedTask(self.process_events, 1), SchedTask(self.check_statuses, 120))) - self.lastUpdate = time.gmtime() + self.lastUpdate = (datetime.utcnow() - timedelta(minutes=10)).utctimetuple() def check_statuses(self): debug("In check_statuses") try: - updates = self.twitter.statuses.friends_timeline() - except Exception, e: - print >> sys.stderr, "Exception while querying twitter:" + updates = reversed(self.twitter.statuses.home_timeline()) + except Exception as e: + print("Exception while querying twitter:", file=sys.stderr) traceback.print_exc(file=sys.stderr) return nextLastUpdate = self.lastUpdate for update in updates: - crt = parse(update['created_at']).utctimetuple() - if (crt > self.lastUpdate): + crt = parsedate(update['created_at']) + 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("@"): - self.privmsg_channels( - u"%s %s%s%s %s" %( - get_prefix(), - IRC_BOLD, update['user']['screen_name'], - IRC_BOLD, text.decode('utf-8'))) + if not text.startswith(b"@"): + 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 - else: - break + self.lastUpdate = nextLastUpdate def process_events(self): - debug("In process_events") self.irc.process_once() def handle_privmsg(self, conn, evt): @@ -202,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) @@ -219,14 +226,23 @@ 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.encode('utf-8')) + def handle_umode(self, conn, evt): + """ + QuakeNet ignores all your commands until after the MOTD. This + handler defers joining until after it sees a magic line. It + also tries to join right after connect, but this will just + make it join again which should be safe. + """ + args = evt.arguments() + if (args and args[0] == '+i'): + channels = self.config.get('irc', 'channel').split(',') + for channel in channels: + self.ircServer.join(channel) 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.encode('utf8')) def follow(self, conn, evt, name): userNick = evt.source().split('!')[0] @@ -238,7 +254,7 @@ class TwitterBot(object): "%sI'm already following %s." %(get_prefix('error'), name)) else: try: - self.twitter.friendships.create(id=name) + self.twitter.friendships.create(screen_name=name) except TwitterError: conn.privmsg( userNick, @@ -262,7 +278,7 @@ class TwitterBot(object): userNick, "%sI'm not following %s." %(get_prefix('error'), name)) else: - self.twitter.friendships.destroy(id=name) + self.twitter.friendships.destroy(screen_name=name) conn.privmsg( userNick, "%sOkay! I've stopped following %s." %( @@ -271,7 +287,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'), @@ -280,6 +296,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() @@ -289,11 +308,15 @@ 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 # world. Mega gross. - cp = SafeConfigParser() + cp = ConfigParser() cp.add_section('irc') cp.set('irc', 'port', '6667') cp.set('irc', 'nick', 'twitterbot') @@ -331,14 +354,12 @@ def main(): if not os.path.exists(configFilename): raise Exception() load_config(configFilename) - except Exception, e: - print >> sys.stderr, "Error while loading ini file %s" %( - configFilename) - print >> sys.stderr, e - print >> sys.stderr, __doc__ + except Exception as e: + print("Error while loading ini file %s" %( + configFilename), file=sys.stderr) + print(e, file=sys.stderr) + print(__doc__, file=sys.stderr) sys.exit(1) - global ACTIVE_PREFIXES - ACTIVE_PREFIXES = PREFIXES[cp.get('irc', 'prefixes')] bot = TwitterBot(configFilename) return bot.run()