-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathtwitter_data_ingestion.py
318 lines (277 loc) · 12 KB
/
twitter_data_ingestion.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from datetime import datetime, timedelta
import re
import json
import time
import pandas as pd
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type
import logging
from config import TWITTER_AUTH_TOKEN
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
class TwitterExtractor:
def __init__(self, headless=True):
self.driver = self._start_chrome(headless)
self.set_token()
def _start_chrome(self, headless):
options = Options()
options.headless = headless
driver = webdriver.Chrome(options=options)
driver.get("https://twitter.com")
return driver
def set_token(self, auth_token=TWITTER_AUTH_TOKEN):
if not auth_token or auth_token == "YOUR_TWITTER_AUTH_TOKEN_HERE":
raise ValueError("Access token is missing. Please configure it properly.")
expiration = (datetime.now() + timedelta(days=7)).strftime("%Y-%m-%d")
cookie_script = f"document.cookie = 'auth_token={auth_token}; expires={expiration}; path=/';"
self.driver.execute_script(cookie_script)
def fetch_tweets(self, page_url, start_date, end_date):
self.driver.get(page_url)
cur_filename = f"data/tweets_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}"
# Convert start_date and end_date from "YYYY-MM-DD" to datetime objects
start_date = datetime.strptime(start_date, "%Y-%m-%d")
end_date = datetime.strptime(end_date, "%Y-%m-%d")
while True:
tweet = self._get_first_tweet()
if not tweet:
continue
row = self._process_tweet(tweet)
if row["date"]:
try:
date = datetime.strptime(row["date"], "%Y-%m-%d")
except ValueError as e:
# infer date format
logger.info(
f"Value error on date format, trying another format.{row['date']}",
e,
)
date = datetime.strptime(row["date"], "%d/%m/%Y")
if date < start_date:
break
elif date > end_date:
self._delete_first_tweet()
continue
self._save_to_json(row, filename=f"{cur_filename}.json")
logger.info(
f"Saving tweets...\n{row['date']}, {row['author_name']} -- {row['text'][:50]}...\n\n"
)
self._delete_first_tweet()
# Save to Excel
self._save_to_excel(
json_filename=f"{cur_filename}.json", output_filename=f"{cur_filename}.xlsx"
)
@retry(
stop=stop_after_attempt(5),
wait=wait_fixed(2),
retry=retry_if_exception_type(TimeoutException),
)
def _get_first_tweet(
self, timeout=10, use_hacky_workaround_for_reloading_issue=True
):
try:
# Wait for either a tweet or the error message to appear
WebDriverWait(self.driver, timeout).until(
lambda d: d.find_elements(By.XPATH, "//article[@data-testid='tweet']")
or d.find_elements(By.XPATH, "//span[contains(text(),'Try reloading')]")
)
# Check for error message and try to click "Retry" if it's present
error_message = self.driver.find_elements(
By.XPATH, "//span[contains(text(),'Try reloading')]"
)
if error_message and use_hacky_workaround_for_reloading_issue:
logger.info(
"Encountered 'Something went wrong. Try reloading.' error.\nTrying to resolve with a hacky workaround (click on another tab and switch back). Note that this is not optimal.\n"
)
logger.info(
"You do not have to worry about data duplication though. The save to excel part does the dedup."
)
self._navigate_tabs()
WebDriverWait(self.driver, timeout).until(
lambda d: d.find_elements(
By.XPATH, "//article[@data-testid='tweet']"
)
)
elif error_message and not use_hacky_workaround_for_reloading_issue:
raise TimeoutException(
"Error message present. Not using hacky workaround."
)
else:
# If no error message, assume tweet is present
return self.driver.find_element(
By.XPATH, "//article[@data-testid='tweet']"
)
except TimeoutException:
logger.error("Timeout waiting for tweet or after clicking 'Retry'")
raise
except NoSuchElementException:
logger.error("Could not find tweet or 'Retry' button")
raise
def _navigate_tabs(self, target_tab="Likes"):
# Deal with the 'Retry' issue. Not optimal.
try:
# Click on the 'Media' tab
self.driver.find_element(By.XPATH, "//span[text()='Media']").click()
time.sleep(2) # Wait for the Media tab to load
# Click back on the Target tab. If you are fetching posts, you can click on 'Posts' tab
self.driver.find_element(By.XPATH, f"//span[text()='{target_tab}']").click()
time.sleep(2) # Wait for the Likes tab to reload
except NoSuchElementException as e:
logger.error("Error navigating tabs: " + str(e))
@retry(stop=stop_after_attempt(2), wait=wait_fixed(1))
def _process_tweet(self, tweet):
author_name, author_handle = self._extract_author_details(tweet)
try:
data = {
"text": self._get_element_text(
tweet, ".//div[@data-testid='tweetText']"
),
"author_name": author_name,
"author_handle": author_handle,
"date": self._get_element_attribute(tweet, "time", "datetime")[:10],
"lang": self._get_element_attribute(
tweet, "div[data-testid='tweetText']", "lang"
),
"url": self._get_tweet_url(tweet),
"mentioned_urls": self._get_mentioned_urls(tweet),
"is_retweet": self.is_retweet(tweet),
"media_type": self._get_media_type(tweet),
"images_urls": (
self._get_images_urls(tweet)
if self._get_media_type(tweet) == "Image"
else None
),
}
except Exception as e:
logger.error(f"Error processing tweet: {e}")
logger.info(f"Tweet: {tweet}")
raise
# Convert date format
if data["date"]:
data["date"] = datetime.strptime(data["date"], "%Y-%m-%d").strftime(
"%Y-%m-%d"
)
# Extract numbers from aria-labels
data.update(
{
"num_reply": self._extract_number_from_aria_label(tweet, "reply"),
"num_retweet": self._extract_number_from_aria_label(tweet, "retweet"),
"num_like": self._extract_number_from_aria_label(tweet, "like"),
}
)
return data
def _get_element_text(self, parent, selector):
try:
return parent.find_element(By.XPATH, selector).text
except NoSuchElementException:
return ""
def _get_element_attribute(self, parent, selector, attribute):
try:
return parent.find_element(By.CSS_SELECTOR, selector).get_attribute(
attribute
)
except NoSuchElementException:
return ""
def _get_mentioned_urls(self, tweet):
try:
# Find all 'a' tags that could contain links. You might need to adjust the selector based on actual structure.
link_elements = tweet.find_elements(
By.XPATH, ".//a[contains(@href, 'http')]"
)
urls = [elem.get_attribute("href") for elem in link_elements]
return urls
except NoSuchElementException:
return []
def is_retweet(self, tweet):
try:
# This is an example; the actual structure might differ.
retweet_indicator = tweet.find_element(
By.XPATH, ".//div[contains(text(), 'Retweeted')]"
)
if retweet_indicator:
return True
except NoSuchElementException:
return False
def _get_tweet_url(self, tweet):
try:
link_element = tweet.find_element(
By.XPATH, ".//a[contains(@href, '/status/')]"
)
return link_element.get_attribute("href")
except NoSuchElementException:
return ""
def _extract_author_details(self, tweet):
author_details = self._get_element_text(
tweet, ".//div[@data-testid='User-Name']"
)
# Splitting the string by newline character
parts = author_details.split("\n")
if len(parts) >= 2:
author_name = parts[0]
author_handle = parts[1]
else:
# Fallback in case the format is not as expected
author_name = author_details
author_handle = ""
return author_name, author_handle
def _get_media_type(self, tweet):
if tweet.find_elements(By.CSS_SELECTOR, "div[data-testid='videoPlayer']"):
return "Video"
if tweet.find_elements(By.CSS_SELECTOR, "div[data-testid='tweetPhoto']"):
return "Image"
return "No media"
def _get_images_urls(self, tweet):
images_urls = []
images_elements = tweet.find_elements(
By.XPATH, ".//div[@data-testid='tweetPhoto']//img"
)
for image_element in images_elements:
images_urls.append(image_element.get_attribute("src"))
return images_urls
def _extract_number_from_aria_label(self, tweet, testid):
try:
text = tweet.find_element(
By.CSS_SELECTOR, f"div[data-testid='{testid}']"
).get_attribute("aria-label")
numbers = [int(s) for s in re.findall(r"\b\d+\b", text)]
return numbers[0] if numbers else 0
except NoSuchElementException:
return 0
def _delete_first_tweet(self, sleep_time_range_ms=(0, 1000)):
try:
tweet = self.driver.find_element(
By.XPATH, "//article[@data-testid='tweet'][1]"
)
self.driver.execute_script("arguments[0].remove();", tweet)
except NoSuchElementException:
logger.info("Could not find the first tweet to delete.")
@staticmethod
def _save_to_json(data, filename="data.json"):
with open(filename, "a", encoding="utf-8") as file:
json.dump(data, file)
file.write("\n")
@staticmethod
def _save_to_excel(json_filename, output_filename="data/data.xlsx"):
# Read JSON data
cur_df = pd.read_json(json_filename, lines=True)
# Drop duplicates & save to Excel
cur_df.drop_duplicates(subset=["url"], inplace=True)
cur_df.to_excel(output_filename, index=False)
logger.info(
f"\n\nDone saving to {output_filename}. Total of {len(cur_df)} unique tweets."
)
if __name__ == "__main__":
scraper = TwitterExtractor()
scraper.fetch_tweets(
"https://twitter.com/elonmusk/likes",
start_date="2024-03-01",
end_date="2024-03-02",
) # YYYY-MM-DD format
# If you just want to export to Excel, you can use the following line
# scraper._save_to_excel(json_filename="tweets_2024-02-01_14-30-00.json", output_filename="tweets_2024-02-01_14-30-00.xlsx")