Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Twitter: Add announces.

  • Loading branch information...
commit 8e9ccd5886bd8922954b52e1a5436a703d40bfad 1 parent 9c7a92d
@ProgVal authored
Showing with 76 additions and 1 deletion.
  1. +13 −1 Twitter/config.py
  2. +63 −0 Twitter/plugin.py
View
14 Twitter/config.py
@@ -85,10 +85,22 @@ def configure(advanced):
conf.registerChannelValue(Twitter.accounts.channel, 'secret',
registry.String('', _("""The Twitter Access Token secret for this
channel's account (%s)""") % helpGetToken, private=True))
-conf.registerGlobalValue(Twitter.accounts.channel, 'api',
+conf.registerChannelValue(Twitter.accounts.channel, 'api',
registry.String('https://api.twitter.com/1', _("""The URL to the
base API URL (by default, it is Twitter.com, but you can use it
for twitter-compatible services, such as identica/statusnet.""")))
+conf.registerGroup(Twitter, 'announce')
+conf.registerChannelValue(Twitter.announce, 'interval',
+ registry.NonNegativeInteger(0, _("""The interval (in seconds) between
+ two fetches of new tweets from the timeline. 0 (zero) disables this
+ feature.""")))
+conf.registerChannelValue(Twitter.announce, 'withid',
+ registry.Boolean(True, _("""Determines whether or not the ID of
+ announced tweets will be displayed.""")))
+conf.registerChannelValue(Twitter.announce, 'oneline',
+ registry.Boolean(True, _("""Determines whether or not all tweets will
+ be shown in one line.""")))
+
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
View
63 Twitter/plugin.py
@@ -28,7 +28,9 @@
###
+import time
import twitter
+import threading
import simplejson
import supybot.utils as utils
import supybot.world as world
@@ -91,14 +93,18 @@ def __init__(self, irc):
self.__parent = super(Twitter, self)
callbacks.Plugin.__init__(self, irc)
self._apis = {}
+ self._died = False
if world.starting:
try:
self._getApi().PostUpdate(_('I just woke up. :)'))
except:
pass
+ self._runningAnnounces = []
+
def _getApi(self, channel):
if channel in self._apis:
+ # TODO: handle configuration changes (using Limnoria's config hooks)
return self._apis[channel]
if channel is None:
key = self.registryValue('accounts.bot.key')
@@ -118,6 +124,62 @@ def _getApi(self, channel):
self._apis[channel] = api
return api
+ def __call__(self, irc, msg):
+ super(Twitter, self).__call__(irc, msg)
+ irc = callbacks.SimpleProxy(irc, msg)
+ for channel in irc.state.channels:
+ if self.registryValue('announce.interval', channel) != 0 and \
+ channel not in self._runningAnnounces:
+ threading.Thread(target=self._fetchTimeline,
+ args=(irc, channel)).start()
+
+ def _fetchTimeline(self, irc, channel):
+ lastRun = time.time()
+ maxId = None
+ assert channel not in self._runningAnnounces
+ self._runningAnnounces.append(channel)
+ try:
+ while not irc.zombie and not self._died and \
+ self.registryValue('announce.interval', channel) != 0:
+ lastRun = time.time()
+ self.log.debug(_('Fetching tweets for channel %s') % channel)
+ api = self._getApi(channel) # Reload it from conf everytime
+ if not api._oauth_consumer and user is None:
+ return
+ if maxId is None:
+ timeline = api.GetFriendsTimeline()
+ else:
+ timeline = api.GetFriendsTimeline(since_id=maxId)
+ if timeline is None or timeline == []:
+ continue
+ print repr(timeline)
+ timeline.reverse()
+ if maxId is None:
+ maxId = timeline[-1].id
+ continue
+ else:
+ maxId = timeline[-1].id
+ if self.registryValue('announce.withid', channel):
+ replies = ['[%s] @%s> %s' % (x.id, x.user.screen_name, x.text) for x in timeline]
+ else:
+ replies = ['@%s> %s' % (x.user.screen_name, x.text) for x in timeline]
+ print repr(replies)
+
+ replies = [x.replace("&lt;", "<").replace("&gt;", ">")
+ .replace("&amp;", "&").encode('utf8') for x in replies]
+ if self.registryValue('announce.oneline', channel):
+ irc.replies(replies, prefixNick=False, joiner=' | ')
+ else:
+ for reply in replies:
+ irc.reply(reply, prefixNick=False)
+ while lastRun+self.registryValue('announce.interval', channel)>\
+ time.time():
+ time.sleep(5)
+ finally:
+ assert channel in self._runningAnnounces
+ self._runningAnnounces.remove(channel)
+
+
@internationalizeDocstring
def following(self, irc, msg, args, channel, user):
@@ -470,6 +532,7 @@ def profile(self, irc, msg, args, channel, user):
def die(self):
self.__parent.die()
+ self._died = True
Class = Twitter
Please sign in to comment.
Something went wrong with that request. Please try again.