Skip to content

Commit f93c7d4

Browse files
FirePing32github-actionscclauss
authored
Get user tweets (#5593)
* updating DIRECTORY.md * Create get_user_tweets.py * updating DIRECTORY.md * Reformat code with black * Add argument type * Add return type * Add tweepy * Fix isort issues * Fix flake8 issues * WIP: doctest * Doctest setup and format with pre-commit * Remove doctests * Update web_programming/get_user_tweets.py Co-authored-by: Christian Clauss <[email protected]> * Update get_user_tweets.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 8e857e8 commit f93c7d4

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

DIRECTORY.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
* [Newton Raphson](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/newton_raphson.py)
1111
* [Secant Method](https://github.com/TheAlgorithms/Python/blob/master/arithmetic_analysis/secant_method.py)
1212

13+
## Audio Filters
14+
* [Butterworth Filter](https://github.com/TheAlgorithms/Python/blob/master/audio_filters/butterworth_filter.py)
15+
* [Iir Filter](https://github.com/TheAlgorithms/Python/blob/master/audio_filters/iir_filter.py)
16+
* [Show Response](https://github.com/TheAlgorithms/Python/blob/master/audio_filters/show_response.py)
17+
1318
## Backtracking
1419
* [All Combinations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_combinations.py)
1520
* [All Permutations](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_permutations.py)
@@ -60,6 +65,7 @@
6065
* [Base64 Encoding](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base64_encoding.py)
6166
* [Base85](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base85.py)
6267
* [Beaufort Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/beaufort_cipher.py)
68+
* [Bifid](https://github.com/TheAlgorithms/Python/blob/master/ciphers/bifid.py)
6369
* [Brute Force Caesar Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/brute_force_caesar_cipher.py)
6470
* [Caesar Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/caesar_cipher.py)
6571
* [Cryptomath Module](https://github.com/TheAlgorithms/Python/blob/master/ciphers/cryptomath_module.py)
@@ -106,8 +112,8 @@
106112

107113
## Conversions
108114
* [Binary To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_decimal.py)
109-
* [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py)
110115
* [Binary To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_hexadecimal.py)
116+
* [Binary To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_octal.py)
111117
* [Decimal To Any](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_any.py)
112118
* [Decimal To Binary](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary.py)
113119
* [Decimal To Binary Recursion](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_binary_recursion.py)
@@ -352,6 +358,7 @@
352358
* [Multi Heuristic Astar](https://github.com/TheAlgorithms/Python/blob/master/graphs/multi_heuristic_astar.py)
353359
* [Page Rank](https://github.com/TheAlgorithms/Python/blob/master/graphs/page_rank.py)
354360
* [Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/prim.py)
361+
* [Random Graph Generator](https://github.com/TheAlgorithms/Python/blob/master/graphs/random_graph_generator.py)
355362
* [Scc Kosaraju](https://github.com/TheAlgorithms/Python/blob/master/graphs/scc_kosaraju.py)
356363
* [Strongly Connected Components](https://github.com/TheAlgorithms/Python/blob/master/graphs/strongly_connected_components.py)
357364
* [Tarjans Scc](https://github.com/TheAlgorithms/Python/blob/master/graphs/tarjans_scc.py)
@@ -978,6 +985,7 @@
978985
* [Fetch Jobs](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_jobs.py)
979986
* [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py)
980987
* [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py)
988+
* [Get User Tweets](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_user_tweets.py)
981989
* [Giphy](https://github.com/TheAlgorithms/Python/blob/master/web_programming/giphy.py)
982990
* [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py)
983991
* [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py)

requirements.txt

+1
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

+60
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)