]> jfr.im git - z_archive/twitter.git/blame_incremental - twitter/ircbot.py
Remove agent cruft. Default to api.twitter.com and version 1.
[z_archive/twitter.git] / twitter / ircbot.py
... / ...
CommitLineData
1"""
2twitterbot
3
4 A twitter IRC bot. Twitterbot connected to an IRC server and idles in
5 a channel, polling a twitter account and broadcasting all updates to
6 friends.
7
8USAGE
9
10 twitterbot [config_file]
11
12CONFIG_FILE
13
14 The config file is an ini-style file that must contain the following:
15
16[irc]
17server: <irc_server>
18port: <irc_port>
19nick: <irc_nickname>
20channel: <irc_channels_to_join>
21prefixes: <prefix_type>
22
23[twitter]
24oauth_token_file: <oauth_token_filename>
25
26
27 If no config file is given "twitterbot.ini" will be used by default.
28
29 The channel argument can accept multiple channels separated by commas.
30
31 The default token file is ~/.twitterbot_oauth.
32
33 The default prefix type is 'cats'. You can also use 'none'.
34
35"""
36
37from __future__ import print_function
38
39BOT_VERSION = "TwitterBot 1.4 (http://mike.verdone.ca/twitter)"
40
41CONSUMER_KEY = "XryIxN3J2ACaJs50EizfLQ"
42CONSUMER_SECRET = "j7IuDCNjftVY8DBauRdqXs4jDl5Fgk1IJRag8iE"
43
44IRC_BOLD = chr(0x02)
45IRC_ITALIC = chr(0x16)
46IRC_UNDERLINE = chr(0x1f)
47IRC_REGULAR = chr(0x0f)
48
49import sys
50import time
51from datetime import datetime, timedelta
52from email.utils import parsedate
53try:
54 from configparser import ConfigParser
55except ImportError:
56 from ConfigParser import ConfigParser
57from heapq import heappop, heappush
58import traceback
59import os
60import os.path
61
62from .api import Twitter, TwitterError
63from .oauth import OAuth, read_token_file
64from .oauth_dance import oauth_dance
65from .util import htmlentitydecode
66
67PREFIXES = dict(
68 cats=dict(
69 new_tweet="=^_^= ",
70 error="=O_o= ",
71 inform="=o_o= "
72 ),
73 none=dict(
74 new_tweet=""
75 ),
76 )
77ACTIVE_PREFIXES=dict()
78
79def get_prefix(prefix_typ=None):
80 return ACTIVE_PREFIXES.get(prefix_typ, ACTIVE_PREFIXES.get('new_tweet', ''))
81
82
83try:
84 import irclib
85except ImportError:
86 raise ImportError(
87 "This module requires python irclib available from "
88 + "https://github.com/sixohsix/python-irclib/zipball/python-irclib3-0.4.8")
89
90OAUTH_FILE = os.environ.get('HOME', '') + os.sep + '.twitterbot_oauth'
91
92def debug(msg):
93 # uncomment this for debug text stuff
94 # print(msg, file=sys.stdout)
95 pass
96
97class SchedTask(object):
98 def __init__(self, task, delta):
99 self.task = task
100 self.delta = delta
101 self.next = time.time()
102
103 def __repr__(self):
104 return "<SchedTask %s next:%i delta:%i>" %(
105 self.task.__name__, self.__next__, self.delta)
106
107 def __lt__(self, other):
108 return self.next < other.next
109
110 def __call__(self):
111 return self.task()
112
113class Scheduler(object):
114 def __init__(self, tasks):
115 self.task_heap = []
116 for task in tasks:
117 heappush(self.task_heap, task)
118
119 def next_task(self):
120 now = time.time()
121 task = heappop(self.task_heap)
122 wait = task.next - now
123 task.next = now + task.delta
124 heappush(self.task_heap, task)
125 if (wait > 0):
126 time.sleep(wait)
127 task()
128 #debug("tasks: " + str(self.task_heap))
129
130 def run_forever(self):
131 while True:
132 self.next_task()
133
134
135class TwitterBot(object):
136 def __init__(self, configFilename):
137 self.configFilename = configFilename
138 self.config = load_config(self.configFilename)
139
140 global ACTIVE_PREFIXES
141 ACTIVE_PREFIXES = PREFIXES[self.config.get('irc', 'prefixes')]
142
143 oauth_file = self.config.get('twitter', 'oauth_token_file')
144 if not os.path.exists(oauth_file):
145 oauth_dance("IRC Bot", CONSUMER_KEY, CONSUMER_SECRET, oauth_file)
146 oauth_token, oauth_secret = read_token_file(oauth_file)
147
148 self.twitter = Twitter(
149 auth=OAuth(
150 oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET),
151 api_version='1',
152 domain='api.twitter.com')
153
154 self.irc = irclib.IRC()
155 self.irc.add_global_handler('privmsg', self.handle_privmsg)
156 self.irc.add_global_handler('ctcp', self.handle_ctcp)
157 self.ircServer = self.irc.server()
158
159 self.sched = Scheduler(
160 (SchedTask(self.process_events, 1),
161 SchedTask(self.check_statuses, 120)))
162 self.lastUpdate = (datetime.utcnow() - timedelta(minutes=10)).utctimetuple()
163
164 def check_statuses(self):
165 debug("In check_statuses")
166 try:
167 updates = reversed(self.twitter.statuses.friends_timeline())
168 except Exception as e:
169 print("Exception while querying twitter:", file=sys.stderr)
170 traceback.print_exc(file=sys.stderr)
171 return
172
173 nextLastUpdate = self.lastUpdate
174 for update in updates:
175 crt = parsedate(update['created_at'])
176 if (crt > nextLastUpdate):
177 text = (htmlentitydecode(
178 update['text'].replace('\n', ' '))
179 .encode('utf-8', 'replace'))
180
181 # Skip updates beginning with @
182 # TODO This would be better if we only ignored messages
183 # to people who are not on our following list.
184 if not text.startswith(b"@"):
185 self.privmsg_channels(
186 "%s %s%s%s %s" %(
187 get_prefix(),
188 IRC_BOLD, update['user']['screen_name'],
189 IRC_BOLD, text.decode('utf-8')))
190
191 nextLastUpdate = crt
192
193 self.lastUpdate = nextLastUpdate
194
195 def process_events(self):
196 self.irc.process_once()
197
198 def handle_privmsg(self, conn, evt):
199 debug('got privmsg')
200 args = evt.arguments()[0].split(' ')
201 try:
202 if (not args):
203 return
204 if (args[0] == 'follow' and args[1:]):
205 self.follow(conn, evt, args[1])
206 elif (args[0] == 'unfollow' and args[1:]):
207 self.unfollow(conn, evt, args[1])
208 else:
209 conn.privmsg(
210 evt.source().split('!')[0],
211 "%sHi! I'm Twitterbot! you can (follow "
212 "<twitter_name>) to make me follow a user or "
213 "(unfollow <twitter_name>) to make me stop." %
214 get_prefix())
215 except Exception:
216 traceback.print_exc(file=sys.stderr)
217
218 def handle_ctcp(self, conn, evt):
219 args = evt.arguments()
220 source = evt.source().split('!')[0]
221 if (args):
222 if args[0] == 'VERSION':
223 conn.ctcp_reply(source, "VERSION " + BOT_VERSION)
224 elif args[0] == 'PING':
225 conn.ctcp_reply(source, "PING")
226 elif args[0] == 'CLIENTINFO':
227 conn.ctcp_reply(source, "CLIENTINFO PING VERSION CLIENTINFO")
228
229 def privmsg_channel(self, msg):
230 return self.ircServer.privmsg(
231 self.config.get('irc', 'channel'), msg)
232
233 def privmsg_channels(self, msg):
234 return_response=True
235 channels=self.config.get('irc','channel').split(',')
236 return self.ircServer.privmsg_many(channels, msg)
237
238 def follow(self, conn, evt, name):
239 userNick = evt.source().split('!')[0]
240 friends = [x['name'] for x in self.twitter.statuses.friends()]
241 debug("Current friends: %s" %(friends))
242 if (name in friends):
243 conn.privmsg(
244 userNick,
245 "%sI'm already following %s." %(get_prefix('error'), name))
246 else:
247 try:
248 self.twitter.friendships.create(id=name)
249 except TwitterError:
250 conn.privmsg(
251 userNick,
252 "%sI can't follow that user. Are you sure the name is correct?" %(
253 get_prefix('error')
254 ))
255 return
256 conn.privmsg(
257 userNick,
258 "%sOkay! I'm now following %s." %(get_prefix('followed'), name))
259 self.privmsg_channels(
260 "%s%s has asked me to start following %s" %(
261 get_prefix('inform'), userNick, name))
262
263 def unfollow(self, conn, evt, name):
264 userNick = evt.source().split('!')[0]
265 friends = [x['name'] for x in self.twitter.statuses.friends()]
266 debug("Current friends: %s" %(friends))
267 if (name not in friends):
268 conn.privmsg(
269 userNick,
270 "%sI'm not following %s." %(get_prefix('error'), name))
271 else:
272 self.twitter.friendships.destroy(id=name)
273 conn.privmsg(
274 userNick,
275 "%sOkay! I've stopped following %s." %(
276 get_prefix('stop_follow'), name))
277 self.privmsg_channels(
278 "%s%s has asked me to stop following %s" %(
279 get_prefix('inform'), userNick, name))
280
281 def run(self):
282 self.ircServer.connect(
283 self.config.get('irc', 'server'),
284 self.config.getint('irc', 'port'),
285 self.config.get('irc', 'nick'))
286 channels=self.config.get('irc', 'channel').split(',')
287 for channel in channels:
288 self.ircServer.join(channel)
289
290 while True:
291 try:
292 self.sched.run_forever()
293 except KeyboardInterrupt:
294 break
295 except TwitterError:
296 # twitter.com is probably down because it
297 # sucks. ignore the fault and keep going
298 pass
299
300def load_config(filename):
301 # Note: Python ConfigParser module has the worst interface in the
302 # world. Mega gross.
303 cp = ConfigParser()
304 cp.add_section('irc')
305 cp.set('irc', 'port', '6667')
306 cp.set('irc', 'nick', 'twitterbot')
307 cp.set('irc', 'prefixes', 'cats')
308 cp.add_section('twitter')
309 cp.set('twitter', 'oauth_token_file', OAUTH_FILE)
310
311 cp.read((filename,))
312
313 # attempt to read these properties-- they are required
314 cp.get('twitter', 'oauth_token_file'),
315 cp.get('irc', 'server')
316 cp.getint('irc', 'port')
317 cp.get('irc', 'nick')
318 cp.get('irc', 'channel')
319
320 return cp
321
322# So there was a joke here about the twitter business model
323# but I got rid of it. Not because I want this codebase to
324# be "professional" in any way, but because someone forked
325# this and deleted the comment because they couldn't take
326# a joke. Hi guy!
327#
328# Fact: The number one use of Google Code is to look for that
329# comment in the Linux kernel that goes "FUCK me gently with
330# a chainsaw." Pretty sure Linus himself wrote it.
331
332def main():
333 configFilename = "twitterbot.ini"
334 if (sys.argv[1:]):
335 configFilename = sys.argv[1]
336
337 try:
338 if not os.path.exists(configFilename):
339 raise Exception()
340 load_config(configFilename)
341 except Exception as e:
342 print("Error while loading ini file %s" %(
343 configFilename), file=sys.stderr)
344 print(e, file=sys.stderr)
345 print(__doc__, file=sys.stderr)
346 sys.exit(1)
347
348 bot = TwitterBot(configFilename)
349 return bot.run()