Skip to content

Commit

Permalink
Merge pull request #17 from barnabywalters/master
Browse files Browse the repository at this point in the history
Added ability to get tweets by list timeline
  • Loading branch information
snarfed committed Aug 10, 2014
2 parents 325f49a + b778a72 commit 3f9a042
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
21 changes: 19 additions & 2 deletions twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
'https://api.twitter.com/1.1/statuses/home_timeline.json?include_entities=true&count=%d'
API_SELF_TIMELINE_URL = \
'https://api.twitter.com/1.1/statuses/user_timeline.json?include_entities=true&count=%d'
API_LIST_TIMELINE_URL = \
'https://api.twitter.com/1.1/lists/statuses.json?include_entities=true&count=%(count)d&slug=%(slug)s&owner_screen_name=%(owner_screen_name)s'
API_STATUS_URL = \
'https://api.twitter.com/1.1/statuses/show.json?id=%s&include_entities=true'
API_RETWEETS_URL = \
Expand Down Expand Up @@ -165,14 +167,29 @@ def get_activities_response(self, user_id=None, group_id=None, app_id=None,
However, retweets are only fetched for the first 15 tweets that have them,
since that's Twitter's rate limit per 15 minute window. :(
https://dev.twitter.com/docs/rate-limiting/1.1/limits
group_id can be used to specify the slug of a list for which to return tweets.
By default the current API user’s lists will be used, but lists owned by other
users can be fetched by explicitly passing a username to user_id, e.g. to
fetch tweets from the list @exampleuser/example-list you would call
get_activities(user_id='exampleuser', group_id='example-list').
"""
if activity_id:
resp = self.urlopen(API_STATUS_URL % activity_id)
tweets = [json.loads(resp.read())]
total_count = len(tweets)
else:
url = API_SELF_TIMELINE_URL if group_id == source.SELF else API_TIMELINE_URL
url = url % (count + start_index)
if group_id == source.SELF:
url = API_SELF_TIMELINE_URL % (count + start_index)
elif group_id in (None, source.FRIENDS, source.ALL):
url = API_TIMELINE_URL % (count + start_index)
else:
url = API_LIST_TIMELINE_URL % {
'count': count + start_index,
'slug': group_id,
'owner_screen_name': user_id or self.get_actor().get('username')
}

headers = {'If-None-Match': etag} if etag else {}
total_count = None
try:
Expand Down
15 changes: 15 additions & 0 deletions twitter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,21 @@ def test_get_activities_self(self):

self.assert_equals([], self.twitter.get_activities(group_id=source.SELF))

def test_get_activities_list_explicit_user(self):
self.expect_urlopen('https://api.twitter.com/1.1/lists/statuses.json?include_entities=true&count=0&slug=testlist&owner_screen_name=schnarfed',
'[]')
self.mox.ReplayAll()

self.assert_equals([], self.twitter.get_activities(group_id='testlist', user_id='schnarfed'))

def test_get_activities_list_implicit_user(self):
self.expect_urlopen('https://api.twitter.com/1.1/account/verify_credentials.json', json.dumps({'screen_name': 'schnarfed'}))
self.expect_urlopen('https://api.twitter.com/1.1/lists/statuses.json?include_entities=true&count=0&slug=testlist&owner_screen_name=schnarfed',
'[]')
self.mox.ReplayAll()

self.assert_equals([], self.twitter.get_activities(group_id='testlist'))

def test_get_activities_fetch_replies(self):
tweet = copy.deepcopy(TWEET)
self.expect_urlopen(TIMELINE, json.dumps([tweet]))
Expand Down

0 comments on commit 3f9a042

Please sign in to comment.