-
Notifications
You must be signed in to change notification settings - Fork 0
/
agentxml.py
285 lines (251 loc) · 9.11 KB
/
agentxml.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2004, 2013 Vishwas Bhat, Apurva Pangam, Tarun Makhija, Vineet Jalali
hbcse-crawler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hbcse-crawler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with hbcse-crawler. If not, see <http://www.gnu.org/licenses/>.
"""
import urllib, urlparse, re, hashlib, os, sgmllib, string, datetime
site_q = "Enter the start site: "
ply_q = "Enter the ply length: "
class StrippingParser(sgmllib.SGMLParser):
# These are the HTML tags that we will leave intact
valid_tags = ()
from htmlentitydefs import entitydefs # replace entitydefs from sgmllib
def __init__(self):
sgmllib.SGMLParser.__init__(self)
self.result = ""
self.endTagList = []
def handle_data(self, data):
if data:
self.result = self.result + data
def handle_charref(self, name):
self.result = "%s&#%s;" % (self.result, name)
def handle_entityref(self, name):
if self.entitydefs.has_key(name):
x = ';'
else:
# this breaks unstandard entities that end with ';'
x = ''
self.result = "%s&%s%s" % (self.result, name, x)
def unknown_starttag(self, tag, attrs):
""" Delete all tags except for legal ones """
if tag in self.valid_tags:
self.result = self.result + '<' + tag
for k, v in attrs:
if string.lower(k[0:2]) != 'on' and string.lower(v[0:10]) != 'javascript':
self.result = '%s %s="%s"' % (self.result, k, v)
endTag = '</%s>' % tag
self.endTagList.insert(0,endTag)
self.result = self.result + '>'
def unknown_endtag(self, tag):
if tag in self.valid_tags:
self.result = "%s</%s>" % (self.result, tag)
remTag = '</%s>' % tag
self.endTagList.remove(remTag)
def cleanup(self):
""" Append missing closing tags """
for j in range(len(self.endTagList)):
self.result = self.result + self.endTagList[j]
def strip(s):
""" Strip illegal HTML tags from string s """
parser = StrippingParser()
parser.feed(s)
parser.close()
parser.cleanup()
return parser.result
class URLLister(sgmllib.SGMLParser):
"""needs sgmllib from SGMLParser
"""
def reset(self):
sgmllib.SGMLParser.reset(self)
self.urls = []
def start_a(self, attrs):
href = [v for k, v in attrs if k=='href']
if href:
self.urls.extend(href)
def validurl(site):
"""Code to identify valid URL to find
"""
try:
url = urlparse.urlparse(site)
if url[0]=='http' or url[0]=='https' :
site = site
elif url[0]=='':
site = 'http://'+site
else:
print 'Invalid URL'
return [None, 0]
except IOError:
print 'The Server is not found or is unreachable'
return [None,0]
except SystemExit:
print 'Exit'
return [None, 0]
site_contents = urllib.urlopen(site)
return [site_contents ,1]
class Summary:
def __init__(self,site,i,config):
"""initialises the object
site site url
x[] site url as an instance,flag
y site content
ply depth of search
sign MD5 of the site data
npara number of paragraphs
para list of paragraphs
title title of the page
weights weighted vector
"""
self.site = site
self.config=config
self.sign = hashlib.md5()
self.ply=i
self.npara=0
self.x = validurl(site)
if(self.x[0]!= 0):
self.y=self.x[0].read()
self.sign.update(repr(self.y))
c=self.checkmd5()
if self.y==None or c==0:
self.x[1]=0
else:
self.x[1]=1;
self.x[0].close()
self.para=['','']
self.title=''
self.weights= []
def checkmd5(self):
file1 = open(os.path.join(path,'md5.txt'),'w+')
x=file1.readline()
while x:
x=x.replace('\n','')
if x==repr(self.sign.digest()):
return 0
x=file1.readline()
file1.close()
return 1
def get2tag(self,tag):
#currently - removes data between p and title tags
#goal - removing data between any tag
w={' ':' ','&':'&','•':'','©':'©','•':'',''':''}
l=len(tag)+2
z=re.compile('<'+tag+'>.*?<\/'+tag+'>',re.DOTALL).findall(self.y)
if(len(z)==0):
z=re.compile('<'+tag+'>.*?<'+tag+'>',re.DOTALL).findall(self.y)
if tag=='p':
if(len(z)==0):
length=0
self.npara=0
print 'The site does not contain any paragraphs'
elif(len(z)>1):
length = 2
self.npara=2
print 'The first 2 paragraphs of the site are: '
else:
length = len(z)
self.npara=len(z)
for i in range(0,length):
m=l-1
while z[i][m]<>'>':
m=m+1
y= z[i][m+1:len(z[i])-l-1]
y=y.strip()
y=urllib.unquote(y)
for a in w.keys():
y= y.replace(a,w[a])
print
print 'Paragraph No.',i+1,':'
print y
self.para[i]=y
elif tag=='title':
if len(z)!=0:
self.title=strip(z[0])
return strip(z[0])
def getvector(self):
#provides the weighted vector based on the lists in three.py
#goal - need to iterate over various lists in the file (bio, chem,...)
flag=0
for x in self.config["others"]:
z= re.compile(x).findall(self.y)
if len(z)>=self.config["threshold"]:
flag=1
self.weights.append((x,len(z)))
print 'Number of matches for',x,'=', len(z)
return flag
def loopurl(self):
#finidng all URLs in the site and creating new objects of those URLs
#uses the class URLLister
parser = URLLister()
parser.feed(self.y)
for url in parser.urls:
if url != '':
if url != self.x[0]:
if url[0:4] != 'http' and url[0:4] != 'www.' :
if url[0] != '/':
site2=self.site+'/'+url
else:
site2=self.site+url
print site2
z=Summary(site2,self.ply-1, self.config)
else:
print url
z=Summary(url,self.ply-1, self.config)
if self.sign.digest() != z.sign.digest():
z.run()
parser.close()
def run(self):
if self.x[1]==1:
print
print '----------------------------'
print 'TITLE: ', self.get2tag('title')
self.get2tag('p')
file2 = open(os.path.join(path,'md5.txt'),'a')
file2.write(repr(self.sign.digest())+'\n')
file2.close()
flag=self.getvector()
if(flag==1):
self.write2file()
print '----------------------------'
print
if(self.ply>=1):
self.loopurl()
def write2file(self):
""" prints the class instance to the file """
part_file_name = self.config["path"]+'/page'+str(datetime.datetime.now());
file = open(part_file_name+'.html','w')
file.write(self.y)
file.close()
file = open(part_file_name+'.xml','w')
file.write('<?xml version="1.0" encoding="ISO-8859-1"?>')
file.write('<summary>\n')
file.write('<siteurl>'+self.site+'</siteurl>\n')
file.write('<title>'+self.title+'</title>\n')
file.write('<md5>'+repr(self.sign.digest())+'</md5>\n')
file.write('<ply>'+repr(self.ply)+'</ply>\n')
for i in range(self.npara):
file.write('<paragraph'+repr(i)+'>'+self.para[i]+'</paragraph'+repr(i)+'>\n')
for i in range(len(self.weights)):
file.write('<weight'+repr(i)+'>'+repr(self.weights[i])+'</weight'+repr(i)+'>\n')
file.write('</summary>')
file.close()
if __name__ == "__main__":
# Initialization
config = {"threshold":1, "others":['cnn', 'india']}
path = os.path.join(os.getcwd(), "data")
if not os.path.exists(path):
os.mkdir(path)
config["path"] = path
# User input
site = raw_input(site_q)
length = raw_input(ply_q)
# Run the program
Summary(site, int(length), config).run()