-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontentInacParallel.py
181 lines (157 loc) · 6.29 KB
/
contentInacParallel.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
from random import shuffle
from random import randint
import os
from multiprocessing import Process
import base64
from bs4 import BeautifulSoup
from bs4.element import Comment
import requests
from string import digits, punctuation
remove_digits = str.maketrans('', '', digits + punctuation)
import time
import fcntl
from zipfile import ZipFile
results_file = "/tmp/resultsDetailed/bottom-inacresults-contd.txt"
count_file = "/tmp/resultsDetailed/bottom-inaccount-contd.txt"
# Helper functions
def find_ngrams(s, n):
input_list = s
return [''.join(elem) for elem in list(zip(*[input_list[i:] for i in range(n)]))]
def jaccard_similarity(list1, list2):
s1 = set(list1)
s2 = set(list2)
x = len(s1.union(s2))
if x == 0:
return 1
return len(s1.intersection(s2)) / x
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def text_from_html(body, just_tags = False):
soup = BeautifulSoup(body, 'html.parser')
if just_tags:
return u" ".join([tag.name for tag in soup.find_all()])
texts = soup.findAll(text=True)
visible_texts = filter(tag_visible, texts)
return u" ".join(t.strip() for t in visible_texts)
# Process domain file for content difference results
def process_domain_file(summarylines, dname):
time.sleep(randint(1,10))
for line in summarylines:
line = line.decode()
results = line.strip().split("***")
try:
assert len(results) == 14
except:
continue
url = results[0]
http_success = results[2]
https_success = results[3]
https_error = results[5]
if http_success != https_success and http_success == "True":
#error = https_error
#error_stripped = error[:error.find('(')]
#if error_stripped == "HTTPError":
# error_stripped = error[error.find('(') + 2:error.find(':')]
if True:
success_https = True
success_http = True
try:
again_https_req = requests.get("https://" + url, timeout=45)
again_https_req.raise_for_status()
except Exception as e:
success_https = False
error = repr(e)
time.sleep(20)
try:
again_http_req = requests.get("http://" + url, timeout=45)
again_http_req.raise_for_status()
except Exception as e:
success_http = False
if success_http and not success_https:
error_stripped = error[:error.find('(')]
if error_stripped == "HTTPError":
error_stripped = error[error.find('(') + 2:error.find(':')]
#print(url, error_stripped)
if True:
# if "Timeout" not in error_stripped and "Connection" not in error_stripped:
with open(results_file, 'a') as resultsf:
fcntl.flock(resultsf, fcntl.LOCK_EX)
resultsf.write(dname + " *** " + url + " *** " + error_stripped + "\n")
fcntl.flock(resultsf, fcntl.LOCK_UN)
time.sleep(20)
def process_zip_file(zip_file):
time.sleep(randint(1,10))
empty_count = 0
with ZipFile(zip_file, 'r') as zfile:
slines = []
processes_summary = []
for name in zfile.namelist():
if "summary/summary-" in name:
dname = name[name.find("summary/summary-")+16:name.rfind('.txt')].strip()
if dname.startswith("www."):
dname = dname.replace("www.", "", 1)
#flag = False
#for spd in second_pass_domains:
# if spd.endswith(dname):
# flag = True
# print(spd, dname)
# break
#if not flag:
# continue
try:
with zfile.open(name) as sfile:
sline = sfile.readlines()
if not sline:
empty_count += 1
except:
continue
slines.append(0)
if len(sline) == 0:
#print("empty file...")
continue
sp = Process(target=process_domain_file, args=(sline,dname,))
processes_summary.append(sp)
sp.start()
while len(processes_summary) >= 50:
for xp in processes_summary:
xp.join(0.1)
if not xp.is_alive():
processes_summary.remove(xp)
for xp in processes_summary:
xp.join()
with open(count_file, 'a') as countf:
fcntl.flock(countf, fcntl.LOCK_EX)
countf.write(str(zip_file) + " " + str(len(slines)) + " " + str(empty_count) + "\n")
fcntl.flock(countf, fcntl.LOCK_UN)
return
#second_pass_domains = []
#with open(doms_file, "r") as f:
# second_pass_domains = f.read().splitlines()
# second_pass_domains = [d.strip() for d in second_pass_domains]
#print(len(second_pass_domains))
zip_files = []
for zfile in os.listdir("/"):
if zfile.endswith(".zip"):
zip_files.append(os.path.join("/", zfile))
count = 1000
shuffle(zip_files)
processes = []
for zip_file in zip_files:
# print("Entering zip ", zip_file)
p = Process(target=process_zip_file, args=(zip_file, ))
processes.append(p)
p.start()
while len(processes) >= 30:
for p in processes:
p.join(0.1)
if not p.is_alive():
processes.remove(p)
count -= 1
if count <= 0:
break
for p in processes:
p.join()