Twitter: Add supybot.plugins.Twitter.announce.users.

master
Valentin Lorentz 2013-05-10 16:57:25 +02:00
parent 59661beb6b
commit 86c3b9ad55
3 changed files with 43 additions and 20 deletions

View File

@ -120,6 +120,9 @@ conf.registerChannelValue(Twitter.announce, 'mentions',
registry.Boolean(True, _("""Determines whether the bot will stream
mentions to the linked account on the channel (only if
supybot.plugins.Twitter.announce.interval is greater than 0).""")))
conf.registerChannelValue(Twitter.announce, 'users',
registry.SpaceSeparatedListOfStrings([], _("""Determines users whose
tweets will be announced on the channel.""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

View File

@ -35,6 +35,8 @@ import re
import sys
import time
import json
import operator
import functools
import threading
import supybot.log as log
import supybot.conf as conf
@ -110,6 +112,24 @@ def expandLinks(tweet):
else:
return tweet
def fetch(method, maxIds, name):
if name not in maxIds:
maxIds[name] = None
if maxIds[name] is None:
tweets = method()
else:
tweets = method(since_id=maxIds[name])
if not tweets:
return []
tweets.sort(key=operator.attrgetter('id'))
if maxIds[name] is None:
maxIds[name] = tweets[-1].id
return []
else:
maxIds[name] = tweets[-1].id
return tweets
@internationalizeDocstring
class Twitter(callbacks.Plugin):
"""Add the help for "@plugin help Twitter" here
@ -223,7 +243,7 @@ class Twitter(callbacks.Plugin):
# Prevent race conditions
return
lastRun = time.time()
maxId = None
maxIds = {}
self._runningAnnounces.append(channel)
try:
while not irc.zombie and not self._died and \
@ -238,28 +258,28 @@ class Twitter(callbacks.Plugin):
return
retweets = self.registryValue('announce.retweets', channel)
try:
tweets = []
if self.registryValue('announce.timeline', channel):
if maxId is None:
timeline = api.GetFriendsTimeline(retweets=retweets)
else:
timeline = api.GetFriendsTimeline(retweets=retweets,
since_id=maxId)
tweets.extend(fetch(
functools.partial(api.GetFriendsTimeline,
retweets=retweets),
maxIds, 'timeline'))
if self.registryValue('announce.mentions', channel):
if maxId is None:
timeline = api.GetReplies()
else:
timeline = api.GetReplies(since_id=maxId)
tweets.extend(fetch(api.GetReplies,
maxIds, 'mentions'))
for user in self.registryValue('announce.users', channel):
if not user.startswith('@'):
user = '@' + user
tweets.extend(fetch(
functools.partial(api.GetUserTimeline,
screen_name=user[1:]),
maxIds, user))
except twitter.TwitterError as e:
self.log.error('Could not fetch timeline: %s' % e)
continue
if timeline is None or timeline == []:
if not tweets:
continue
timeline.reverse()
if maxId is None:
maxId = timeline[-1].id
continue
else:
maxId = timeline[-1].id
tweets.sort(key=operator.attrgetter('id'))
format_ = '@%(user)s> %(msg)s'
if self.registryValue('announce.withid', channel):
format_ = '[%(longid)s] ' + format_
@ -269,7 +289,7 @@ class Twitter(callbacks.Plugin):
'shortid': self._get_shortid(x.id),
'user': x.user.screen_name,
'msg': x.text
} for x in timeline]
} for x in tweets]
replies = map(self._unescape, replies)
replies = map(expandLinks, replies)

View File

@ -2685,7 +2685,7 @@ class Api(object):
if id:
url = '%s/statuses/user_timeline/%s.json' % (self.base_url, id)
elif user_id:
url = '%s/statuses/user_timeline.json?user_id=%d' % (self.base_url, user_id)
url = '%s/statuses/user_timeline.json?user_id=%s' % (self.base_url, user_id)
elif screen_name:
url = ('%s/statuses/user_timeline.json?screen_name=%s' % (self.base_url,
screen_name))
@ -2939,7 +2939,7 @@ class Api(object):
Returns:
A sequence of twitter.Status instances, one for each reply to the user.
'''
url = '%s/statuses/replies.json' % self.base_url
url = '%s/statuses/mentions_timeline.json' % self.base_url
if not self._oauth_consumer:
raise TwitterError("The twitter.Api instance must be authenticated.")
parameters = {}