This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for getting user timeline
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Downloads all tweets from a given user. | ||
Uses twitter.Api.GetUserTimeline to retreive the last 3,200 tweets from a user. | ||
Twitter doesn't allow retreiving more tweets than this through the API, so we get | ||
as many as possible. | ||
t.py should contain the imported variables. | ||
""" | ||
|
||
from __future__ import print_function | ||
|
||
import json | ||
import sys | ||
|
||
import twitter | ||
from t import ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET | ||
|
||
|
||
def get_tweets(api=None, screen_name=None): | ||
timeline = api.GetUserTimeline(screen_name=screen_name, count=200) | ||
earliest_tweet = min(timeline, key=lambda x: x.id).id | ||
print("getting tweets before:", earliest_tweet) | ||
|
||
while True: | ||
tweets = api.GetUserTimeline( | ||
screen_name=screen_name, max_id=earliest_tweet, count=200 | ||
) | ||
new_earliest = min(tweets, key=lambda x: x.id).id | ||
|
||
if not tweets or new_earliest == earliest_tweet: | ||
break | ||
else: | ||
earliest_tweet = new_earliest | ||
print("getting tweets before:", earliest_tweet) | ||
timeline += tweets | ||
|
||
return timeline | ||
|
||
|
||
if __name__ == "__main__": | ||
api = twitter.Api( | ||
CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET | ||
) | ||
screen_name = sys.argv[1] | ||
print(screen_name) | ||
timeline = get_tweets(api=api, screen_name=screen_name) | ||
|
||
with open('examples/timeline.json', 'w+') as f: | ||
for tweet in timeline: | ||
f.write(json.dumps(tweet._json)) | ||
f.write('\n') |