-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.py
More file actions
58 lines (46 loc) · 1.74 KB
/
Data.py
File metadata and controls
58 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
###############################################
import tweepy
import pandas as pd
import time
import os
# 🔐 Add your token here
BEARER_TOKEN = 'AAAAAAAAAAAAAAAAAAAAAKuZ2gEAAAAAjXozzWflF%2FYO9FsZ1CuRuNGAeSI%3DS8fg9ON3DifgUCIqIDZ9KMvbWp3gcT7BXrL5i3fxi1DHDu4e5T'
client = tweepy.Client(bearer_token=BEARER_TOKEN)
queries = [
"#AI"
]
all_data = []
for tag in queries:
print(f"📡 Collecting tweets for: {tag}")
query = f"{tag} lang:en -is:retweet"
success = False
while not success:
try:
response = client.search_recent_tweets(query=query, max_results=900)
if response.data:
for tweet in response.data:
text = tweet.text
hashtags = [word for word in text.split() if word.startswith("#")]
if hashtags:
all_data.append({
"text": text,
"hashtags": " ".join(hashtags)
})
success = True
time.sleep(3) # gentle delay between calls
except tweepy.TooManyRequests:
print("⏳ Rate limit hit. Sleeping for 15 minutes...")
time.sleep(15 * 60)
except ConnectionResetError as e:
print(f"❌ Connection error: {e}. Retrying in 1 minute...")
time.sleep(60)
except Exception as e:
print(f"❌ Unknown error: {e}. Skipping to next hashtag.")
success = True # skip to next hashtag to avoid infinite loop
# Save CSV
if all_data:
df = pd.DataFrame(all_data)
df.to_csv("all_tweets.csv", index=False)
print(f"✅ Saved {len(df)} tweets to 'all_tweets.csv'")
else:
print("⚠️ No tweets collected. Try again later.")