]>
jfr.im git - z_archive/twitter.git/blob - twitter/ircbot.py
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
10 twitterbot [config_file]
14 The config file is an ini-style file that must contain the following:
20 channel: <irc_channels_to_join>
21 prefixes: <prefix_type>
24 oauth_token_file: <oauth_token_filename>
27 If no config file is given "twitterbot.ini" will be used by default.
29 The channel argument can accept multiple channels separated by commas.
31 The default token file is ~/.twitterbot_oauth.
33 The default prefix type is 'cats'. You can also use 'none'.
37 from __future__
import print_function
39 BOT_VERSION
= "TwitterBot 1.9.1 (http://mike.verdone.ca/twitter)"
41 CONSUMER_KEY
= "XryIxN3J2ACaJs50EizfLQ"
42 CONSUMER_SECRET
= "j7IuDCNjftVY8DBauRdqXs4jDl5Fgk1IJRag8iE"
45 IRC_ITALIC
= chr(0x16)
46 IRC_UNDERLINE
= chr(0x1f)
47 IRC_REGULAR
= chr(0x0f)
51 from datetime
import datetime
, timedelta
52 from email
.utils
import parsedate
54 from configparser
import ConfigParser
56 from ConfigParser
import ConfigParser
57 from heapq
import heappop
, heappush
62 from .api
import Twitter
, TwitterError
63 from .oauth
import OAuth
, read_token_file
64 from .oauth_dance
import oauth_dance
65 from .util
import htmlentitydecode
77 ACTIVE_PREFIXES
=dict()
79 def get_prefix(prefix_typ
=None):
80 return ACTIVE_PREFIXES
.get(prefix_typ
, ACTIVE_PREFIXES
.get('new_tweet', ''))
87 "This module requires python irclib available from "
88 + "https://github.com/sixohsix/python-irclib/zipball/python-irclib3-0.4.8")
90 OAUTH_FILE
= os
.environ
.get('HOME', os
.environ
.get('USERPROFILE', '')) + os
.sep
+ '.twitterbot_oauth'
93 # uncomment this for debug text stuff
94 # print(msg, file=sys.stdout)
97 class SchedTask(object):
98 def __init__(self
, task
, delta
):
101 self
.next
= time
.time()
104 return "<SchedTask %s next:%i delta:%i>" %(
105 self
.task
.__name
__, self
.__next
__, self
.delta
)
107 def __lt__(self
, other
):
108 return self
.next
< other
.next
113 class Scheduler(object):
114 def __init__(self
, tasks
):
117 heappush(self
.task_heap
, task
)
121 task
= heappop(self
.task_heap
)
122 wait
= task
.next
- now
123 task
.next
= now
+ task
.delta
124 heappush(self
.task_heap
, task
)
128 #debug("tasks: " + str(self.task_heap))
130 def run_forever(self
):
135 class TwitterBot(object):
136 def __init__(self
, configFilename
):
137 self
.configFilename
= configFilename
138 self
.config
= load_config(self
.configFilename
)
140 global ACTIVE_PREFIXES
141 ACTIVE_PREFIXES
= PREFIXES
[self
.config
.get('irc', 'prefixes')]
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
)
148 self
.twitter
= Twitter(
150 oauth_token
, oauth_secret
, CONSUMER_KEY
, CONSUMER_SECRET
),
152 domain
='api.twitter.com')
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
.irc
.add_global_handler('umode', self
.handle_umode
)
158 self
.ircServer
= self
.irc
.server()
160 self
.sched
= Scheduler(
161 (SchedTask(self
.process_events
, 1),
162 SchedTask(self
.check_statuses
, 120)))
163 self
.lastUpdate
= (datetime
.utcnow() - timedelta(minutes
=10)).utctimetuple()
165 def check_statuses(self
):
166 debug("In check_statuses")
168 updates
= reversed(self
.twitter
.statuses
.friends_timeline())
169 except Exception as e
:
170 print("Exception while querying twitter:", file=sys
.stderr
)
171 traceback
.print_exc(file=sys
.stderr
)
174 nextLastUpdate
= self
.lastUpdate
175 for update
in updates
:
176 crt
= parsedate(update
['created_at'])
177 if (crt
> nextLastUpdate
):
178 text
= (htmlentitydecode(
179 update
['text'].replace('\n', ' '))
180 .encode('utf8', 'replace'))
182 # Skip updates beginning with @
183 # TODO This would be better if we only ignored messages
184 # to people who are not on our following list.
185 if not text
.startswith(b
"@"):
186 msg
= "%s %s%s%s %s" %(
188 IRC_BOLD
, update
['user']['screen_name'],
189 IRC_BOLD
, text
.decode('utf8'))
190 self
.privmsg_channels(msg
)
194 self
.lastUpdate
= nextLastUpdate
196 def process_events(self
):
197 self
.irc
.process_once()
199 def handle_privmsg(self
, conn
, evt
):
201 args
= evt
.arguments()[0].split(' ')
205 if (args
[0] == 'follow' and args
[1:]):
206 self
.follow(conn
, evt
, args
[1])
207 elif (args
[0] == 'unfollow' and args
[1:]):
208 self
.unfollow(conn
, evt
, args
[1])
211 evt
.source().split('!')[0],
212 "%sHi! I'm Twitterbot! you can (follow "
213 "<twitter_name>) to make me follow a user or "
214 "(unfollow <twitter_name>) to make me stop." %
217 traceback
.print_exc(file=sys
.stderr
)
219 def handle_ctcp(self
, conn
, evt
):
220 args
= evt
.arguments()
221 source
= evt
.source().split('!')[0]
223 if args
[0] == 'VERSION':
224 conn
.ctcp_reply(source
, "VERSION " + BOT_VERSION
)
225 elif args
[0] == 'PING':
226 conn
.ctcp_reply(source
, "PING")
227 elif args
[0] == 'CLIENTINFO':
228 conn
.ctcp_reply(source
, "CLIENTINFO PING VERSION CLIENTINFO")
230 def handle_umode(self
, conn
, evt
):
232 QuakeNet ignores all your commands until after the MOTD. This
233 handler defers joining until after it sees a magic line. It
234 also tries to join right after connect, but this will just
235 make it join again which should be safe.
237 args
= evt
.arguments()
238 if (args
and args
[0] == '+i'):
239 channels
= self
.config
.get('irc', 'channel').split(',')
240 for channel
in channels
:
241 self
.ircServer
.join(channel
)
243 def privmsg_channels(self
, msg
):
245 channels
=self
.config
.get('irc','channel').split(',')
246 return self
.ircServer
.privmsg_many(channels
, msg
.encode('utf8'))
248 def follow(self
, conn
, evt
, name
):
249 userNick
= evt
.source().split('!')[0]
250 friends
= [x
['name'] for x
in self
.twitter
.statuses
.friends()]
251 debug("Current friends: %s" %(friends))
252 if (name
in friends
):
255 "%sI'm already following %s." %(get_prefix('error'), name
))
258 self
.twitter
.friendships
.create(id=name
)
262 "%sI can't follow that user. Are you sure the name is correct?" %(
268 "%sOkay! I'm now following %s." %(get_prefix('followed'), name
))
269 self
.privmsg_channels(
270 "%s%s has asked me to start following %s" %(
271 get_prefix('inform'), userNick
, name
))
273 def unfollow(self
, conn
, evt
, name
):
274 userNick
= evt
.source().split('!')[0]
275 friends
= [x
['name'] for x
in self
.twitter
.statuses
.friends()]
276 debug("Current friends: %s" %(friends))
277 if (name
not in friends
):
280 "%sI'm not following %s." %(get_prefix('error'), name
))
282 self
.twitter
.friendships
.destroy(id=name
)
285 "%sOkay! I've stopped following %s." %(
286 get_prefix('stop_follow'), name
))
287 self
.privmsg_channels(
288 "%s%s has asked me to stop following %s" %(
289 get_prefix('inform'), userNick
, name
))
291 def _irc_connect(self
):
292 self
.ircServer
.connect(
293 self
.config
.get('irc', 'server'),
294 self
.config
.getint('irc', 'port'),
295 self
.config
.get('irc', 'nick'))
296 channels
=self
.config
.get('irc', 'channel').split(',')
297 for channel
in channels
:
298 self
.ircServer
.join(channel
)
305 self
.sched
.run_forever()
306 except KeyboardInterrupt:
309 # twitter.com is probably down because it
310 # sucks. ignore the fault and keep going
312 except irclib
.ServerNotConnectedError
:
313 # Try and reconnect to IRC.
317 def load_config(filename
):
318 # Note: Python ConfigParser module has the worst interface in the
321 cp
.add_section('irc')
322 cp
.set('irc', 'port', '6667')
323 cp
.set('irc', 'nick', 'twitterbot')
324 cp
.set('irc', 'prefixes', 'cats')
325 cp
.add_section('twitter')
326 cp
.set('twitter', 'oauth_token_file', OAUTH_FILE
)
330 # attempt to read these properties-- they are required
331 cp
.get('twitter', 'oauth_token_file'),
332 cp
.get('irc', 'server')
333 cp
.getint('irc', 'port')
334 cp
.get('irc', 'nick')
335 cp
.get('irc', 'channel')
339 # So there was a joke here about the twitter business model
340 # but I got rid of it. Not because I want this codebase to
341 # be "professional" in any way, but because someone forked
342 # this and deleted the comment because they couldn't take
345 # Fact: The number one use of Google Code is to look for that
346 # comment in the Linux kernel that goes "FUCK me gently with
347 # a chainsaw." Pretty sure Linus himself wrote it.
350 configFilename
= "twitterbot.ini"
352 configFilename
= sys
.argv
[1]
355 if not os
.path
.exists(configFilename
):
357 load_config(configFilename
)
358 except Exception as e
:
359 print("Error while loading ini file %s" %(
360 configFilename
), file=sys
.stderr
)
361 print(e
, file=sys
.stderr
)
362 print(__doc__
, file=sys
.stderr
)
365 bot
= TwitterBot(configFilename
)