Skip to content

Commit 9cf85ab

Browse files
Add Demo tweepy File
Add Demo tweepy File
1 parent 8a06e31 commit 9cf85ab

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Diff for: Article40 - tweepy/demo_tweepy.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Import libraries
2+
import pandas as pd, tweepy
3+
4+
# Key & access tokens
5+
consumer_key = "YOUR CONSUMER KEY"
6+
consumer_secret = "YOUR CONSUMER SECRET"
7+
access_token = "YOUR ACCESS TOKEN"
8+
access_token_secret = "YOUR ACCESS TOKEN SECRET"
9+
10+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
11+
auth.set_access_token(access_token, access_token_secret)
12+
api = tweepy.API(auth)
13+
14+
# Open your text file/snscrape output
15+
tweet_url = pd.read_csv("twitter-@EmbarcaderoTech.txt", index_col= None, header = None, names = ["links"])
16+
print(tweet_url.head())
17+
18+
# Extract the tweet_id using .split function
19+
af = lambda x: x["links"].split("/")[-1]
20+
tweet_url['id'] = tweet_url.apply(af, axis=1)
21+
print(tweet_url.head())
22+
23+
# Convert our tweet_url Series into a list
24+
ids = tweet_url['id'].tolist()
25+
26+
# Process the ids by batch or chunks.
27+
total_count = len(ids)
28+
chunks = (total_count - 1) // 50 + 1
29+
30+
# Username, date and the tweet themselves, so my code will only include those queries.
31+
def fetch_tw(ids):
32+
list_of_tw_status = api.statuses_lookup(ids, tweet_mode= "extended")
33+
empty_data = pd.DataFrame()
34+
for status in list_of_tw_status:
35+
tweet_elem = {"tweet_id": status.id,
36+
"screen_name": status.user.screen_name,
37+
"tweet":status.full_text,
38+
"date":status.created_at,
39+
"retweet_count": status.retweet_count,
40+
"favorite_count": status.favorite_count}
41+
empty_data = empty_data.append(tweet_elem, ignore_index = True)
42+
empty_data.to_csv("embarcaderoTech_Tweets.csv", mode="a")
43+
44+
# Create another for loop to loop into our batches while processing 50 entries every loop
45+
for i in range(chunks):
46+
batch = ids[i*50:(i+1)*50]
47+
result = fetch_tw(batch)

0 commit comments

Comments
 (0)