-
Notifications
You must be signed in to change notification settings - Fork 0
/
vocalive.py
executable file
·255 lines (218 loc) · 8.62 KB
/
vocalive.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
#!/usr/bin/env python
import argparse;
import requests;
from bs4 import BeautifulSoup;
import os;
import sys;
# -*- coding: utf-8 -*-
class Manager:
""" Manages bots and labs """
args = '';
bots = list();
def __init__(self):
""" Where everything begins... """
parser = argparse.ArgumentParser( description='Live Vocabulary Observer' );
parser.add_argument( '-m', metavar='Length', default=2, help='Word minimum length. Default = 2' );
parser.add_argument( '-o', '--output', metavar='File', help='File to save learned words' );
parser.add_argument( 'url', metavar='Address', help='Web address to learn from' );
self.args = parser.parse_args();
def request_page( self ):
self.createbot( self.args.url );
def createbot( self, url ):
self.bots.append( Bot( self, url ) );
def write_words( self, text, encoding ):
try:
with open( self.args.output, 'w+' ) as content:
for word in text:
content.write( word.encode( encoding, errors='replace' )+'\n' );
except IOError as e:
end("I/O error({0}): {1}".format(e.errno, e.strerror));
def get_results( self, text, encoding ):
self.write_words( text, encoding );
class Bot:
""" Crawler """
manager = '';
address = '';
answer = '';
text = '';
def __init__(self, owner, url):
self.manager = owner;
self.address = url;
self.fetch();
print(self.address);
def check_url(self):
if ((self.address.find('http://',0,7) == -1) and (self.address.find('https://',0,8) == -1)):
end('ERROR: address not starting with http or https.\n\tCheck your URL and try again.\n');
def fetch(self):
self.check_url();
answer = '';
try:
self.headers = {'Accept-Charset': 'utf-8'}
self.answer = requests.get( self.address, params=self.headers )
if (self.answer.status_code != 200):
raise IOError
self.souped_page = BeautifulSoup(self.answer.text, 'html.parser');
for script in self.souped_page(["script", "style"]):
script.extract()
self.souped_page = BeautifulSoup(str(self.souped_page), 'html.parser');
self.text = self.souped_page.get_text();
self.send_to_lab()
exit();
except IOError:
end('Request returned ' + str(self.answer.status_code) + ' code');
def send_to_lab( self ):
lab = Laboratory( self.manager, self.text, self.answer.encoding );
class Laboratory:
""" Text processing """
manager = str();
text = str();
encoding = str();
wordlist = set();
count = int();
def __init__( self, manager, text, encoding ):
self.manager = manager;
self.encoding = encoding;
self.analyse( text.lower() );
def analyse( self, text ):
separated_words = self.breakdown( text );
clean_words = self.strip_punctuation( separated_words );
unaccented = self.strip_accentuation( clean_words );
unique_words = self.discard_repetition( unaccented );
eligible_words = self.minimum_length( unique_words, self.manager.args.m );
sorted_strings = self.sort_data( eligible_words );
self.wordlist = self.digits_out( sorted_strings );
self.count = self.count_words( self.wordlist );
self.manager.get_results( self.wordlist, self.encoding );
def breakdown( self, text ):
text = self.convert_to_space( text );
return text.split();
def strip_punctuation( self, text ):
treated = list();
for word in text:
treated.append( self.remove_chars( word ) );
return treated;
def convert_to_space( self, string ):
space_chars = { ord('.'): ord(' '),
ord('_'): ord(' '),
ord('/'): ord(' '),
ord('-'): ord(' ')};
return string.translate( space_chars );
def remove_chars(self, string ):
blocked_chars = { ord('!'): None,
ord('\"'): None,
ord('#'): None,
ord('$'): None,
ord('%'): None,
ord('&'): None,
ord('\''): None,
ord('('): None,
ord(')'): None,
ord('*'): None,
ord('+'): None,
ord(','): None,
ord(':'): None,
ord(';'): None,
ord('<'): None,
ord('='): None,
ord('>'): None,
ord('?'): None,
ord('@'): None,
ord('['): None,
ord('\\'): None,
ord(']'): None,
ord('^'): None,
ord('`'): None,
ord('{'): None,
ord('|'): None,
ord('}'): None,
ord('~'): None,
ord('\xa9'): None,
ord('\xaa'): None,
ord('\xab'): None,
ord('\xae'): None,
ord('\xb0'): None,
ord('\xb2'): None,
ord('\xb3'): None,
ord('\xba'): None,
ord('\xbb'): None,
ord('\xb9'): None};
return string.translate( blocked_chars );
def strip_accentuation( self, string ):
treated = list();
for word in string:
treated.append( self.translate_accents( word ) );
return treated;
def translate_accents( self, string ):
accents = {
ord('\xe7'): u'c',
ord('\xc0'): ord('A'),
ord('\xc0'): ord('A'),
ord('\xc1'): ord('A'),
ord('\xc2'): ord('A'),
ord('\xc3'): ord('A'),
ord('\xc8'): ord('E'),
ord('\xc9'): ord('E'),
ord('\xca'): ord('E'),
ord('\xcc'): ord('I'),
ord('\xcd'): ord('I'),
ord('\xce'): ord('I'),
ord('\xd2'): ord('O'),
ord('\xd3'): ord('O'),
ord('\xd4'): ord('O'),
ord('\xd5'): ord('O'),
ord('\xd9'): ord('U'),
ord('\xda'): ord('U'),
ord('\xdb'): ord('U'),
ord('\xdc'): ord('U'),
ord('\xe0'): ord('a'),
ord('\xe1'): ord('a'),
ord('\xe2'): ord('a'),
ord('\xe3'): ord('a'),
ord('\xe8'): ord('e'),
ord('\xe9'): ord('e'),
ord('\xea'): ord('e'),
ord('\xec'): ord('i'),
ord('\xed'): ord('i'),
ord('\xee'): ord('i'),
ord('\xf1'): ord('n'),
ord('\xf2'): ord('o'),
ord('\xf3'): ord('o'),
ord('\xf4'): ord('o'),
ord('\xf5'): ord('o'),
ord('\xf9'): ord('u'),
ord('\xfa'): ord('u'),
ord('\xfb'): ord('u'),
ord('\xfc'): ord('u')};
return string.translate( accents );
def discard_repetition( self, words ):
temp = set();
for word in words:
if( not temp.__contains__( word ) ):
temp.add( word );
return temp;
def minimum_length( self, words, number=2 ):
minimum_length = int( number );
temp = set();
for word in words:
if( len( word ) >= minimum_length ):
temp.add(word);
return temp;
def sort_data( self, unique ):
return sorted( unique );
def digits_out( self, strings ):
treated = list();
for word in strings:
if( not word.isdigit() ):
treated.append( word );
return treated;
def count_words( self, words ):
number = int();
number = len( words );
print('Unique words = ' + str( number ) );
return number;
def end( reason ):
print('\n[-] ' + reason);
exit();
if __name__ == "__main__":
observer = Manager();
observer.request_page();