+ debug("In check_statuses")
+ try:
+ updates = 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
+ for update in updates:
+ crt = parse(update['created_at']).utctimetuple()
+ if (crt > self.lastUpdate):
+ 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(
+ u"=^_^= %s%s%s %s" %(
+ IRC_BOLD, update['user']['screen_name'],
+ IRC_BOLD, text.decode('utf-8')))
+
+ 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):
+ 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],
+ "=^_^= Hi! I'm Twitterbot! you can (follow "
+ + "<twitter_name>) to make me follow a user or "
+ + "(unfollow <twitter_name>) to make me stop.")
+ 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,
+ "=O_o= I'm already following %s." %(name))
+ else:
+ try:
+ self.twitter.friendships.create(id=name)
+ except TwitterError:
+ conn.privmsg(
+ userNick,
+ "=O_o= I can't follow that user. Are you sure the name is correct?")
+ return
+ conn.privmsg(
+ userNick,
+ "=^_^= Okay! I'm now following %s." %(name))
+ self.privmsg_channels(
+ "=o_o= %s has asked me to start following %s" %(
+ 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,
+ "=O_o= I'm not following %s." %(name))
+ else:
+ self.twitter.friendships.destroy(id=name)
+ conn.privmsg(
+ userNick,
+ "=^_^= Okay! I've stopped following %s." %(name))
+ self.privmsg_channels(
+ "=o_o= %s has asked me to stop following %s" %(
+ userNick, name))
+