+ debug("In check_statuses")
+ try:
+ updates = reversed(self.twitter.statuses.friends_timeline())
+ except Exception as e:
+ print("Exception while querying twitter:", file=sys.stderr)
+ traceback.print_exc(file=sys.stderr)
+ return
+
+ nextLastUpdate = self.lastUpdate
+ debug("self.lastUpdate is %s" % self.lastUpdate)
+ for update in updates:
+ crt = parsedate(update['created_at'])
+ if (crt > nextLastUpdate):
+ text = (htmlentitydecode(
+ update['text'].replace('\n', ' '))
+ .encode('utf-8', '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(
+ "%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):
+ self.irc.process_once()
+
+ def handle_privmsg(self, conn, evt):
+ debug('got privmsg')
+ args = evt.arguments()[0].split(' ')
+ try:
+ if (not args):
+ return
+ if (args[0] == 'follow' and args[1:]):
+ self.follow(conn, evt, args[1])
+ elif (args[0] == 'unfollow' and args[1:]):
+ self.unfollow(conn, evt, args[1])
+ else:
+ conn.privmsg(
+ evt.source().split('!')[0],
+ "%sHi! I'm Twitterbot! you can (follow "
+ + "<twitter_name>) to make me follow a user or "
+ + "(unfollow <twitter_name>) to make me stop." %
+ get_prefix())
+ except Exception:
+ traceback.print_exc(file=sys.stderr)
+
+ def handle_ctcp(self, conn, evt):
+ args = evt.arguments()
+ source = evt.source().split('!')[0]
+ if (args):
+ if args[0] == 'VERSION':
+ conn.ctcp_reply(source, "VERSION " + BOT_VERSION)
+ elif args[0] == 'PING':
+ conn.ctcp_reply(source, "PING")
+ 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 privmsg_channels(self, msg):
+ return_response=True
+ channels=self.config.get('irc','channel').split(',')
+ return self.ircServer.privmsg_many(channels, msg.encode('utf-8'))
+
+ def follow(self, conn, evt, name):
+ userNick = evt.source().split('!')[0]
+ friends = [x['name'] for x in self.twitter.statuses.friends()]
+ debug("Current friends: %s" %(friends))
+ if (name in friends):
+ conn.privmsg(
+ userNick,
+ "%sI'm already following %s." %(get_prefix('error'), name))
+ else:
+ try:
+ self.twitter.friendships.create(id=name)
+ except TwitterError:
+ conn.privmsg(
+ userNick,
+ "%sI can't follow that user. Are you sure the name is correct?" %(
+ get_prefix('error')
+ ))
+ return
+ conn.privmsg(
+ userNick,
+ "%sOkay! I'm now following %s." %(get_prefix('followed'), name))
+ self.privmsg_channels(
+ "%s%s has asked me to start following %s" %(
+ get_prefix('inform'), userNick, name))
+
+ def unfollow(self, conn, evt, name):
+ userNick = evt.source().split('!')[0]
+ friends = [x['name'] for x in self.twitter.statuses.friends()]
+ debug("Current friends: %s" %(friends))
+ if (name not in friends):
+ conn.privmsg(
+ userNick,
+ "%sI'm not following %s." %(get_prefix('error'), name))
+ else:
+ self.twitter.friendships.destroy(id=name)
+ conn.privmsg(
+ userNick,
+ "%sOkay! I've stopped following %s." %(
+ get_prefix('stop_follow'), name))
+ self.privmsg_channels(
+ "%s%s has asked me to stop following %s" %(
+ get_prefix('inform'), userNick, name))
+