-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Get user tweets #5593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Get user tweets #5593
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
010c4b8
updating DIRECTORY.md
8d3cbdc
Create get_user_tweets.py
FirePing32 6d7546c
updating DIRECTORY.md
184709e
Reformat code with black
FirePing32 71f2008
Add argument type
FirePing32 bccb8f2
Add return type
FirePing32 f515e7a
Add tweepy
FirePing32 bb67328
Fix isort issues
FirePing32 8b13026
Fix flake8 issues
FirePing32 e0c4d44
WIP: doctest
FirePing32 18dd999
Doctest setup and format with pre-commit
FirePing32 1863372
Remove doctests
FirePing32 e71c24a
Update web_programming/get_user_tweets.py
FirePing32 c5810f1
Update get_user_tweets.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,61 @@ | ||
import tweepy | ||
import csv | ||
|
||
# Twitter API credentials | ||
consumer_key = "" | ||
consumer_secret = "" | ||
access_key = "" | ||
access_secret = "" | ||
|
||
|
||
def get_all_tweets(screen_name): | ||
|
||
# authorize twitter, initialize tweepy | ||
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | ||
auth.set_access_token(access_key, access_secret) | ||
api = tweepy.API(auth) | ||
|
||
# initialize a list to hold all the tweepy Tweets | ||
alltweets = [] | ||
|
||
# make initial request for most recent tweets (200 is the maximum allowed count) | ||
new_tweets = api.user_timeline(screen_name=screen_name, count=200) | ||
|
||
# save most recent tweets | ||
alltweets.extend(new_tweets) | ||
|
||
# save the id of the oldest tweet less one | ||
oldest = alltweets[-1].id - 1 | ||
|
||
# keep grabbing tweets until there are no tweets left to grab | ||
while len(new_tweets) > 0: | ||
print(f"getting tweets before {oldest}") | ||
|
||
# all subsiquent requests use the max_id param to prevent duplicates | ||
new_tweets = api.user_timeline( | ||
screen_name=screen_name, count=200, max_id=oldest | ||
) | ||
|
||
# save most recent tweets | ||
alltweets.extend(new_tweets) | ||
|
||
# update the id of the oldest tweet less one | ||
oldest = alltweets[-1].id - 1 | ||
|
||
print(f"...{len(alltweets)} tweets downloaded so far") | ||
|
||
# transform the tweepy tweets into a 2D array that will populate the csv | ||
outtweets = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets] | ||
|
||
# write the csv | ||
with open(f"new_{screen_name}_tweets.csv", "w") as f: | ||
writer = csv.writer(f) | ||
writer.writerow(["id", "created_at", "text"]) | ||
writer.writerows(outtweets) | ||
|
||
pass | ||
FirePing32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
# pass in the username of the account you want to download | ||
get_all_tweets("FirePing32") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.