Skip to content

Commit 84657be

Browse files
authored
Merge branch 'master' into hn-posts
2 parents 36794a6 + f93c7d4 commit 84657be

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@
989989
* [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py)
990990
* [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py)
991991
* [Get Top Hn Posts](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_top_hn_posts.py)
992+
* [Get User Tweets](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_user_tweets.py)
992993
* [Giphy](https://github.com/TheAlgorithms/Python/blob/master/web_programming/giphy.py)
993994
* [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py)
994995
* [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ sklearn
1414
statsmodels
1515
sympy
1616
tensorflow
17+
tweepy
1718
types-requests
1819
xgboost

web_programming/get_user_tweets.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import csv
2+
3+
import tweepy
4+
5+
# Twitter API credentials
6+
consumer_key = ""
7+
consumer_secret = ""
8+
access_key = ""
9+
access_secret = ""
10+
11+
12+
def get_all_tweets(screen_name: str) -> None:
13+
14+
# authorize twitter, initialize tweepy
15+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
16+
auth.set_access_token(access_key, access_secret)
17+
api = tweepy.API(auth)
18+
19+
# initialize a list to hold all the tweepy Tweets
20+
alltweets = []
21+
22+
# make initial request for most recent tweets (200 is the maximum allowed count)
23+
new_tweets = api.user_timeline(screen_name=screen_name, count=200)
24+
25+
# save most recent tweets
26+
alltweets.extend(new_tweets)
27+
28+
# save the id of the oldest tweet less one
29+
oldest = alltweets[-1].id - 1
30+
31+
# keep grabbing tweets until there are no tweets left to grab
32+
while len(new_tweets) > 0:
33+
print(f"getting tweets before {oldest}")
34+
35+
# all subsiquent requests use the max_id param to prevent duplicates
36+
new_tweets = api.user_timeline(
37+
screen_name=screen_name, count=200, max_id=oldest
38+
)
39+
40+
# save most recent tweets
41+
alltweets.extend(new_tweets)
42+
43+
# update the id of the oldest tweet less one
44+
oldest = alltweets[-1].id - 1
45+
46+
print(f"...{len(alltweets)} tweets downloaded so far")
47+
48+
# transform the tweepy tweets into a 2D array that will populate the csv
49+
outtweets = [[tweet.id_str, tweet.created_at, tweet.text] for tweet in alltweets]
50+
51+
# write the csv
52+
with open(f"new_{screen_name}_tweets.csv", "w") as f:
53+
writer = csv.writer(f)
54+
writer.writerow(["id", "created_at", "text"])
55+
writer.writerows(outtweets)
56+
57+
58+
if __name__ == "__main__":
59+
# pass in the username of the account you want to download
60+
get_all_tweets("FirePing32")

0 commit comments

Comments
 (0)