-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
435 lines (409 loc) · 17 KB
/
utils.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
from newspaper import Article
from bs4 import BeautifulSoup, Tag
import json
import time
import re
import random
from boilerpipe.extract import Extractor
import ssl
from selenium.common.exceptions import TimeoutException
try:
import urllib.request
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
import urllib2
from langdetect import detect
from seleniumwire import webdriver
from selenium.webdriver.chrome.options import Options
from dateutil.parser import parse
import unicodedata
import pandas as pd
import numpy as np
import urllib.request
import os
import eventlet
import requests
import sys
sys.setrecursionlimit(10000)
cwd = os.path.dirname(os.path.abspath(__file__))
bad_domains = ["youtube.com", ":///", "vimeo.com", "reddit.com", "twitter.com", "youtu.be", "linkedin.com", "whatsapp", "books.google", "spreadsheets.google"]
fc_agencies = ["snopes.com", "emergent.info", "politifact.com", "factcheck.org", "truthorfiction.com"]
base_urls = {"factcheck":"https://www.factcheck.org/fake-news/","snopes":"https://www.snopes.com/fact-check/page/2/", "politifact":"http://www.politifact.com/truth-o-meter/statements/", "tof":"https://www.truthorfiction.com/category/fact-checks/"}
infofile = cwd+"/input/infofile.txt"
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("---disable-setuid-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--dns-prefetch-disable")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--single-proces')
chrome_driver = os.path.abspath(cwd+"/chromedriver")
def removeFile(filename):
try:
os.remove(filename)
print('File Removed: '+filename)
except OSError:
pass
################# methods handling error logs ########################
def contentLogError(url, error, origin):
print("error:"+str(error)+str(origin))
with open(cwd+"/eatiht/logfiles/log_"+origin+"_content.txt","r+") as log:
lines = log.readlines()
print(lines)
for line in lines:
if url not in line:
with open(cwd+"/eatiht/logfiles/log_"+origin+"_content.txt","a+") as log:
log.write(error+url+"\n")
def inputLogError(url, pagenum, error, dataset):
print("ERROR:",error)
with open(cwd+"/input/"+dataset+"/logfile.txt","r+") as log:
lines = log.readlines()
for line in lines:
if url in line:
return
#with open(cwd+"/"+dataset+"/logfile.txt","a+") as log:
log.write(error+"|P>"+str(pagenum)+" "+url+"\n")
################# methods handling page numbers ########################
def update_last_saved_page(page_num,agency):
with open(infofile, "r+") as f:
lines = f.readlines()
for idx,line in enumerate(lines):
if agency+": " in line:
lines[idx] = agency+": "+str(page_num)+"\n"
a = "".join(lines)
# SAVE PROGRESS PAGE
with open(infofile, "w+") as f:
f.write(a)
def get_last_saved_page(agency):
with open(infofile, "r+") as f:
lines = f.readlines()
for line in lines:
if agency+": " in line:
return(int(line.split(" ")[1]))
def get_num_of_pages(agency, name_or_url=True):
if name_or_url:
base_url = base_urls[agency]
else:
base_url = agency
print(base_url)
req = requests.get(base_url)
print(req)
soup = BeautifulSoup(req.text,"html.parser")
if agency == "snopes":
pages = soup.find("title").text.split(" ")[-3]
elif agency == "factcheck":
pagination = soup.find("ul",{"class":"pagination"})
last_child = pagination.find_all(recursive=False)[-1]
pages = last_child.a['href'].split("/")[-2]
elif agency == "politifact":
pages = soup.find("span",{"class":"step-links__current"}).text.split()[-1]
elif agency in ["https://www.truthorfiction.com/category/fact-checks","https://www.truthorfiction.com"]:
pages = soup.findAll("a", {"class":"page-numbers"})[-2].text
return int(pages)
################# methods dealing with origin(s) #######################
#remove unwanted origin candidates from the list
def cleanOrigins(origins, meta = None):
fixed_list = []
for elem in origins:
t = eventlet.Timeout(50)
try:
browser = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
origin_url = elem['href']
if meta is not None:
meta_date = meta.split(",")[-2][1:-2]
origin_text = elem.text
if meta_date not in origin_text:
continue
if (origin_url is not None) and (origin_url[-4:] not in [".pdf",".asp"]):
origin_domain = getDomain(origin_url)
if sum([1 for e in fc_agencies if e in origin_domain]) > 0:
continue
elif sum([1 for e in bad_domains if e in origin_domain]) > 0:
continue
elif ("facebook.com" in origin_domain):
if ("/videos/" in origin_url) or ("/photos/" in origin_url):
continue
browser.get(origin_url)
code = [req.response.status_code for req in browser.requests if req.response]
if any(x in [301, 404] for x in code):
continue
elif ("archive" in origin_domain):
fixed_list.append(origin_url.replace("/image",""))
else:
fixed_list.append(origin_url)
except Exception as e:
input("ERROR ON CLEANING ORIGIN:"+str(e))
pass
finally:
browser.quit()
t.cancel()
return list(set(fixed_list))
################# methods that capture webpage content #######################
def getVerdict(text):
answer = ''.join(text.split("A:")[1:]).strip(" ")
verdict = answer.split(".")[0][:3]
print(answer)
if "No" in verdict:
return "false"
elif "Yes" in verdict:
return "true"
else:
return "unverified"
#returns the domain of given url
def getDomain(url):
domain = '{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url))
domain = ''.join(domain.split())
domain = domain.replace("https://www.","")
domain = domain.replace("https://","")
domain = domain.replace("http://www.","")
domain = domain.replace("http://","")
return domain
#gets the 'body' of an url, also checks if domain is a webarchive to proceed accordingly
#update: now, this method makes use of only one third party lib: newspaper 3k, seems to work really well.
#this method makes use of getManual/getPipe/getEathit and selects the larger of them
def getContent(url,source="mixed",fromArchive=False):
oDomain = getDomain(url)
if sum([1 for e in bad_domains if e in oDomain]) > 0:
return (None, "bad domain")
if (url[-4:] in [".jpg",".pdf",".mp3",".mp4"]) or ("/video/" in url) or ("/image/" in url):
contentLogError(url,"FORMAT\n",source)
return (None, "format")
if "http://archive." in url:
fromArchive = True
if not fromArchive:
print("[utils.py] Getting content: "+url)
try:
a = Article(url)
a.download()
a.parse()
a.nlp()
date = a.publish_date.strftime('%Y/%m/%d')
if len(a.text) > 50:
return ((a.text, a.title, a.authors, a.summary, date, a.keywords), "newspaper")
raise
except:
return(None,"eat")
#try with archive:
else:
print("[utils.py] Getting content from archive: http://archive.fo/"+url)
browser = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
url = "http://archive.fo/"+url
row = None
t=eventlet.Timeout(20)
try:
browser.get(url)
soup = BeautifulSoup(browser.page_source,"lxml")
row = soup.find("div",{"id":"row0"})
except:
pass
finally:
t.cancel()
browser.quit()
if row is None:
return(None,"no log in archive.is")
else:
text = row.find("div",{"class":"TEXT-BLOCK"})
url = text.a['href']
try:
a = Article(url)
a.download()
a.parse()
a.nlp()
date = a.publish_date.strftime('%Y/%m/%d')
return ((a.text, a.title, a.authors, a.summary, date, a.keywords), "newspaper")
except:
return(None,"no log in archive.is")
#########################################################
################# old code below ########################
#########################################################
#def tryContent(data):
# soup = BeautifulSoup(data,"lxml")
# try0 = soup.find("div",{"itemprop":"articleBody"})
# try1 = soup.find("div",{"class":"entry-content article-text"})
# try2 = soup.find("div",{"class":"article-text"})
# try3 = soup.find("div",{"class":"entry-content"})
# catch = next((item for item in [try0,try1,try2,try3] if item is not None),soup.new_tag('div'))
# while catch.find(["applet","code","embed","head","object","script","server"]):
# catch.find(["applet","code","embed","head","object","script","server"]).decompose()
#
# #manual = unicodedata.normalize('NFKD', catch.text.replace("\n","")).encode('ascii','ignore')
# manual = unicodedata.normalize('NFKD', catch.text).encode('ascii','ignore')
#
# if isinstance(manual, bytes):
# manual = manual.decode("ascii")
#
# return manual
##this tries to get the content of a given website by navigating on html tabs with BeautifulSoup
#def getManual(url,source="mixed"):
# #chrome_options = Options()
# #chrome_options.add_argument("--headless")
# #chrome_options.add_argument("--dns-prefetch-disable")
# #chrome_options.add_argument("--window-size=1920x1080")
# #chrome_driver = os.getcwd() +"/chromedriver"
#
# browserManual = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
# error = False
# req = None
#
# #selenium browser requests do not return the HTML response code, so I use requests.get to check if the page is 404
# t=eventlet.Timeout(15)
# try:
# req = requests.get(url)
# if req == "<Response [404]>":
# contentLogError(url, "404\n",source)
# browserManual.quit()
# return(False,"404")
# except:
# error = True
# finally:
# t.cancel()
#
# t = eventlet.Timeout(10)
# try:
# browserManual.get(url)
# source_body = browserManual.page_source
# current_url = browserManual.current_url
# except:
# if error == True:
# contentLogError(url,"ERROR (selenium+requests)\n",source)
# return(False,"domain")
# else:
# source_body = req.text
# current_url = req.url
# finally:
# t.cancel()
# browserManual.quit()
#
# try:
# actual_domain = getDomain(current_url)
# claim_source_domain = getDomain(url)
#
# if actual_domain != claim_source_domain: #check if it's not redirected"
# print(actual_domain,claim_source_domain)
# browserManual.quit()
# return (False, "redirect")
#
# if len(source_body) == 0:
# browserManual.quit()
# return (False, "no content")
#
# if isinstance(source_body, bytes):
# source_body = source_body.decode("ascii")
#
# manual = tryContent(source_body)
# except Exception as e:
# print(e)
# finally:
# browserManual.quit()
#
# return (True,manual)
#
##gets the carbon date(first date of indexing) of a page
#def getDate(url):
# try:
# with eventlet.Timeout(30):
# carbon_date = requests.get("http://cd.cs.odu.edu/cd/"+url)
# data = json.loads(carbon_date.text)
# if data['estimated-creation-date'] is not '':
# date = parse(data['estimated-creation-date']).strftime('%Y/%m/%d')
# return date
# except:
# pass
#
# return 'N/A'
#
##gets the 'body' of an url, also checks if domain is a webarchive to proceed accordingly
##this method makes use of getManual/getPipe/getEathit and selects the larger of them
#def getContent(url,source="mixed",fromArchive=False):
# a = Article(url)
# a.download()
# a.parse()
# a.nlp()
# date = a.publish_date.strftime('%Y/%m/%d')
# return (a.text, a.title, a.authors, a.summary, date, a.keywords, "newspaper")
# if fromArchive:
# print("[utils.py] Getting content from archive: http://archive.fo/"+url)
# else:
# print("[utils.py] Getting content: "+url)
#
# print("check-1")
# browser = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
# print("check-2")
# if fromArchive:
# #try with archive:
# url = "http://archive.fo/"+url
# row = None
# t=eventlet.Timeout(15)
# try:
# browser.get(url)
# soup = BeautifulSoup(browser.page_source,"lxml")
# row = soup.find("div",{"id":"row0"})
# except:
# pass
# finally:
# t.cancel()
# browser.quit()
# if row is None:
# return ("","no log in archive.is")
# else:
# text = row.find("div",{"class":"TEXT-BLOCK"})
# url = text.a['href']
#
# print("check-3")
# eat = ''
# pipe = ''
# manual = ''
#
# if (url[-4:] in [".jpg",".pdf",".mp3",".mp4"]) or ("/video/" in url) or ("/image/" in url):
# contentLogError(url,"FORMAT\n",source)
# return ("","format")
# print("check-4")
# t=eventlet.Timeout(15)
# try:
# #eat = eatiht.extract(url).replace("\n","")
#
# print("check-5")
# eat = eatiht.extract(url)
# print("ACK eat",len(eat))
# except:
# print("NACK eat")
# pass
# finally:
# t.cancel()
#
# t=eventlet.Timeout(15)
# try:
# #pipe = Extractor(extractor='ArticleExtractor', url=url).getText().replace("\n","")
# pipe = Extractor(extractor='ArticleExtractor', url=url).getText()
# print("ACK pipe",len(pipe))
# except:
# print("NACK pipe")
# pass
# finally:
# print("check-6")
# t.cancel()
#
# print("check-7")
# manual = getManual(url,source)
# if manual[0] == False:
# if manual[1] == 'redirect':
# contentLogError(url,"REDIRECT\n",source)
# return("","redirect")
# manual = ''
# else:
# manual = manual[1]
# print("ACK manual",len(manual))
#
# selected = max([(eat,"eat"),(pipe,"pipe"),(manual,"manual")], key=lambda x: len(x[0]))
#
# if len(selected) > 0:
# lang = detect(str(selected))
# if "en" not in lang:
# contentLogError(url,"LANGUAGE\n",source)
# return("","language")
# else:
# contentLogError(url,"NO CONTENT\n",source)
#
# return (selected[0],selected[1])