X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/0bcc8bb9d00268d7a50763cd33d1a6cd391536a4..a5e40197cf272175a8bc4aa39d4c5b6e545c751c:/twitter/ircbot.py diff --git a/twitter/ircbot.py b/twitter/ircbot.py index 1c2a649..c1bae08 100644 --- a/twitter/ircbot.py +++ b/twitter/ircbot.py @@ -46,7 +46,8 @@ IRC_REGULAR = chr(0x0f) import sys import time -from dateutil.parser import parse +from datetime import datetime, timedelta +from email.utils import parsedate from ConfigParser import SafeConfigParser from heapq import heappop, heappush import traceback @@ -63,10 +64,10 @@ PREFIXES = dict( new_tweet="=^_^= ", error="=O_o= ", inform="=o_o= " - ) + ), none=dict( new_tweet="" - ) + ), ) ACTIVE_PREFIXES=dict() @@ -119,7 +120,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 +132,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) @@ -150,21 +154,22 @@ class TwitterBot(object): 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() + updates = reversed(self.twitter.statuses.friends_timeline()) except Exception, e: print >> sys.stderr, "Exception while querying twitter:" traceback.print_exc(file=sys.stderr) return nextLastUpdate = self.lastUpdate + debug("self.lastUpdate is %s" % 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')) @@ -174,18 +179,17 @@ class TwitterBot(object): # to people who are not on our following list. if not text.startswith("@"): self.privmsg_channels( - u"%s %s%s%s %s" %( + u"%s %s%s%s %s" %( get_prefix(), IRC_BOLD, update['user']['screen_name'], IRC_BOLD, text.decode('utf-8'))) nextLastUpdate = crt - else: - break + + debug("setting self.lastUpdate to %s" % nextLastUpdate) self.lastUpdate = nextLastUpdate def process_events(self): - debug("In process_events") self.irc.process_once() def handle_privmsg(self, conn, evt): @@ -202,8 +206,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) @@ -338,7 +342,5 @@ def main(): print >> sys.stderr, __doc__ sys.exit(1) - global ACTIVE_PREFIXES - ACTIVE_PREFIXES = PREFIXES[cp.get('irc', 'prefixes')] bot = TwitterBot(configFilename) return bot.run()