-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_documents.py
334 lines (312 loc) · 12.8 KB
/
copy_documents.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import time
import requests
import io
import os
import sys
from PyPDF2 import PdfReader
import olefile
import docx
from urllib.parse import urlparse
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from webdriver_manager.chrome import ChromeDriverManager
import pandas as pd
from tqdm import tqdm
import random
# List of user agents to cycle through
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/95.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/95.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/96.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/96.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0",
# Add more user agents as needed
]
def initialize_remote_browser():
# Initialize WebDriver with a random user agent
user_agent = random.choice(user_agents)
options = Options()
options.add_argument(f"user-agent={user_agent}")
options.add_argument('--window-size=1920,1080')
options.add_argument('--start-maximized')
# options.add_argument('--headless=new')
# options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_experimental_option('prefs', {
'download.default_directory': '/tmp/',
'download.prompt_for_download': False,
'download.directory_upgrade': True,
'safebrowsing_for_trusted_sources_enabled': False,
'safebrowsing.enabled': False
})
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
return driver, user_agent
def find_pdf_links(driver):
# Find all anchor elements (hyperlinks) on the page
all_links = driver.find_elements(By.TAG_NAME, 'a')
# Initialize a list to store the URLs
pdf_links = []
# Loop through all the links and check if the href attribute ends with 'pdf' (case-insensitive)
for link in all_links:
href = link.get_attribute('href')
link_text = link.text.lower()
if href and href.lower().endswith('.pdf'):
if 'pdf' in link_text or 'read' in link_text or 'download' in link_text or 'report' in link_text:
pdf_links.append(href)
return pdf_links
def fetch_twitter(driver, user_agent, url):
driver.get(url)
time.sleep(2)
try:
article = driver.find_element(By.XPATH, '//article')
except:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
try:
hyperlink = article.find_element(By.XPATH, './/a[@target="_blank"]')
except NoSuchElementException:
title = driver.title
return {
'title': title,
'extension': 'txt',
'full_text': article.text,
'bytes': None
}
return fetch_nonsocial(driver, user_agent, hyperlink.get_attribute('href'))
def fetch_nonsocial(driver, user_agent, url):
headers = {'User-Agent': user_agent}
requests_failure = False
try:
get_request = requests.get(url, headers=headers, allow_redirects=True)
time.sleep(2)
except:
requests_failure = True
if requests_failure == True:
try:
driver.get(url)
time.sleep(2)
title = driver.title
body_text = driver.find_element(By.XPATH, '/html/body').text
if 'not a robot' in body_text:
input('Press enter to continue...')
body_text = driver.find_element(By.XPATH, '/html/body').text
try:
meta_description = driver.find_element(By.XPATH,"//meta[@name='description']").get_attribute("content")
except:
meta_description = ''
pdf_links = find_pdf_links(driver)
if len(pdf_links) > 0:
time.sleep(2)
pdf_link_results = fetch_nonsocial(driver, user_agent, pdf_links[0])
if pdf_link_results['full_text'] is not None and pdf_link_results['full_text'] != '':
return {
'title': title,
'extension': 'txt',
'full_text': title + '\n' + meta_description + '\n' + body_text + '\n' + pdf_link_results['full_text'],
'bytes': pdf_link_results['bytes']
}
return {
'title': title,
'extension': 'txt',
'full_text': title + '\n' + meta_description + '\n' + body_text,
'bytes': None
}
except:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
if get_request.ok:
try:
content_type = get_request.headers['Content-Type']
except KeyError:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
if content_type.startswith('text'):
driver.get(url)
time.sleep(2)
title = driver.title
body_text = driver.find_element(By.XPATH, '/html/body').text
if 'not a robot' in body_text:
input('Press enter to continue...')
body_text = driver.find_element(By.XPATH, '/html/body').text
try:
meta_description = driver.find_element(By.XPATH,"//meta[@name='description']").get_attribute("content")
except:
meta_description = ''
pdf_links = find_pdf_links(driver)
if len(pdf_links) > 0:
time.sleep(2)
pdf_link_results = fetch_nonsocial(driver, user_agent, pdf_links[0])
if pdf_link_results['full_text'] is not None and pdf_link_results['full_text'] != '':
return {
'title': title,
'extension': 'txt',
'full_text': title + '\n' + meta_description + '\n' + body_text + '\n' + pdf_link_results['full_text'],
'bytes': pdf_link_results['bytes']
}
return {
'title': title,
'extension': 'txt',
'full_text': title + '\n' + meta_description + '\n' + body_text,
'bytes': None
}
elif content_type.startswith('application/pdf'):
try:
text_content = []
bytes_content = get_request.content
pdf_content = io.BytesIO(bytes_content)
pdf_reader = PdfReader(pdf_content)
for page in pdf_reader.pages:
text_content.append(page.extract_text())
return {
'title': None,
'extension': 'pdf',
'full_text': '\n'.join(text_content).replace('\x00',''),
'bytes': bytes_content
}
except Exception as e:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
elif content_type == 'application/msword':
try:
bytes_content = get_request.content
doc_content = io.BytesIO(bytes_content)
with olefile.OleFileIO(doc_content) as ole:
stream = ole.openstream('WordDocument')
text_content = stream.read().decode('utf-8', errors='ignore')
return {
'title': None,
'extension': 'doc',
'full_text': text_content.replace('\x00',''),
'bytes': bytes_content
}
except Exception as e:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
elif content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
try:
text_content = []
bytes_content = get_request.content
doc_content = io.BytesIO(bytes_content)
doc = docx.Document(doc_content)
for para in doc.paragraphs:
text_content.append(para.text)
return {
'title': None,
'extension': 'docx',
'full_text': '\n'.join(text_content).replace('\x00',''),
'bytes': bytes_content
}
except Exception as e:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
else:
try:
driver.get(url)
time.sleep(2)
title = driver.title
body_text = driver.find_element(By.XPATH, '/html/body').text
if 'not a robot' in body_text:
input('Press enter to continue...')
body_text = driver.find_element(By.XPATH, '/html/body').text
try:
meta_description = driver.find_element(By.XPATH,"//meta[@name='description']").get_attribute("content")
except:
meta_description = ''
pdf_links = find_pdf_links(driver)
if len(pdf_links) > 0:
time.sleep(2)
pdf_link_results = fetch_nonsocial(driver, user_agent, pdf_links[0])
if pdf_link_results['full_text'] is not None and pdf_link_results['full_text'] != '':
return {
'title': title,
'extension': 'txt',
'full_text': title + '\n' + meta_description + '\n' + body_text + '\n' + pdf_link_results['full_text'],
'bytes': pdf_link_results['bytes']
}
return {
'title': title,
'extension': 'txt',
'full_text': title + '\n' + meta_description + '\n' + body_text,
'bytes': None
}
except:
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
return {
'title': None,
'extension': None,
'full_text': None,
'bytes': None
}
def main(metadata_path):
uncopied_documents = pd.read_csv(metadata_path)
metadata_filename = os.path.basename(metadata_path)
metadata_basename, _ = os.path.splitext(metadata_filename)
textdata_folder = os.path.join('textdata', metadata_basename)
os.makedirs(textdata_folder, exist_ok=True)
problematic_ids = []
short_ids = []
for document in tqdm(uncopied_documents.to_dict('records')):
driver, user_agent = initialize_remote_browser()
domain = urlparse(document['url']).netloc
if domain in ['twitter.com']:
new_title, extension, full_text, bytes = fetch_twitter(driver, user_agent, document['url']).values()
else:
new_title, extension, full_text, bytes = fetch_nonsocial(driver, user_agent, document['url']).values()
filename = '{}/{}.txt'.format(textdata_folder, document['id'])
if full_text is None:
full_text = ''
with open(filename, 'w') as txt_file:
txt_file.write(full_text)
if full_text == '':
problematic_ids.append(document['id'])
elif len(full_text.split(' ')) < 100:
short_ids.append(document['id'])
driver.close()
print(
"Problematic IDs: {}".format(
", ".join([str(id) for id in problematic_ids])
)
)
print(
"Short IDs: {}".format(
", ".join([str(id) for id in short_ids])
)
)
if __name__ == '__main__':
main(sys.argv[1])