forked from kingwkb/readability
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.py
More file actions
319 lines (244 loc) · 10.7 KB
/
readability.py
File metadata and controls
319 lines (244 loc) · 10.7 KB
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
# -*- coding:utf-8 -*-
# project url: https://github.com/mickeyandkaka/readability
# fork from https://github.com/kingwkb/readability
# author: kingwkb
# blog : http://yanghao.org/blog/
# modified by: mickeyandkaka
# blog : http://www.mickeyandkaka.cn
import re
import sys
import math
import urllib.request, urllib.parse, urllib.error
import html.parser
import posixpath
from bs4 import BeautifulSoup
class Readability:
# regex rules
regexps = {
'unlikelyCandidates': re.compile("combx|comment|community|disqus|extra|foot|header|menu|"
"remark|rss|shoutbox|sidebar|sponsor|ad-break|agegate|"
"pagination|pager|popup|tweet|twitter",re.I),
'okMaybeItsACandidate': re.compile("and|article|body|column|main|shadow", re.I),
'positive': re.compile("article|body|content|entry|hentry|main|page|pagination|post|text|"
"blog|story",re.I),
'negative': re.compile("combx|comment|com|contact|foot|footer|footnote|masthead|media|"
"meta|outbrain|promo|related|scroll|shoutbox|sidebar|sponsor|"
"shopping|tags|tool|widget", re.I),
'extraneous': re.compile("print|archive|comment|discuss|e[\-]?mail|share|reply|all|login|"
"sign|single",re.I),
'divToPElements': re.compile("<(a|blockquote|dl|div|img|ol|p|pre|table|ul)",re.I),
'replaceBrs': re.compile("(<br[^>]*>[ \n\r\t]*){2,}",re.I),
'replaceFonts': re.compile("<(/?)font[^>]*>",re.I),
'trim': re.compile("^\s+|\s+$",re.I),
'normalize': re.compile("\s{2,}",re.I),
'killBreaks': re.compile("(<br\s*/?>(\s| ?)*)+",re.I),
'videos': re.compile("http://(www\.)?(youtube|vimeo)\.com",re.I),
'skipFootnoteLink': re.compile("^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$",re.I),
'nextLink': re.compile("(next|weiter|continue|>([^\|]|$)|»([^\|]|$))",re.I),
'prevLink': re.compile("(prev|earl|old|new|<|«)",re.I)
}
def __init__(self, input, url, charset):
self.candidates = {}
self.charset = charset
self.url = url
# input is bytes of raw html code
# self.input was decode by the origin charset
self.input = input.decode(charset)
# at first replace brs and fonts in html
self.input = self.regexps['replaceBrs'].sub("</p><p>",self.input)
self.input = self.regexps['replaceFonts'].sub("<\g<1>span>",self.input)
# self.html is a beautifulsoup object from self.input using charset
self.html = BeautifulSoup(self.input)
# remove unnecessary tags
self.removeScript()
self.removeStyle()
self.removeLink()
self.title = self.getArticleTitle()
self.content = self.grabArticle()
def removeScript(self):
scripts = self.html.find_all("script")
[script.extract() for script in scripts]
def removeStyle(self):
styles = self.html.find_all("style")
[style.extract() for style in styles]
def removeLink(self):
links = self.html.find_all("link")
[link.extract() for link in links]
def grabArticle(self):
for elem in self.html.find_all(True):
unlikelyMatchString = elem.get('id','')
class_list = elem.get('class','')
for class_str in class_list:
unlikelyMatchString += class_str
if self.regexps['unlikelyCandidates'].search(unlikelyMatchString) and \
not self.regexps['okMaybeItsACandidate'].search(unlikelyMatchString) and \
elem.name != 'body':
elem.extract()
continue
if elem.name == 'div':
# change elem(beautifulsoup obj) -> str to execute the regex
s = elem.decode_contents(eventual_encoding = self.charset)
if not self.regexps['divToPElements'].search(s):
elem.name = 'p'
for node in self.html.find_all('p'):
parentNode = node.parent
grandParentNode = parentNode.parent
innerText = node.text
# threshold = 20
if not parentNode or len(innerText) < 20:
continue
parentHash = hash(str(parentNode))
grandParentHash = hash(str(grandParentNode))
if parentHash not in self.candidates:
self.candidates[parentHash] = self.initializeNode(parentNode)
if grandParentNode and grandParentHash not in self.candidates:
self.candidates[grandParentHash] = self.initializeNode(grandParentNode)
contentScore = 1
contentScore += innerText.count(',')
contentScore += innerText.count(',')
contentScore += min(math.floor(len(innerText) / 100), 3)
self.candidates[parentHash]['score'] += contentScore
if grandParentNode:
self.candidates[grandParentHash]['score'] += contentScore / 2
topCandidate = None
for key in self.candidates:
self.candidates[key]['score'] = self.candidates[key]['score'] * \
(1 - self.getLinkDensity(self.candidates[key]['node']) )
if not topCandidate or self.candidates[key]['score'] > topCandidate['score']:
topCandidate = self.candidates[key]
content = ''
if topCandidate:
content = topCandidate['node']
content = self.cleanArticle(content)
return content
def cleanArticle(self, content):
self.cleanStyle(content)
self.clean(content, 'h1')
self.clean(content, 'object')
self.cleanConditionally(content, "form")
if len(content.find_all('h2')) == 1:
self.clean(content, 'h2')
self.clean(content, 'iframe')
self.cleanConditionally(content, "table")
self.cleanConditionally(content, "ul")
self.cleanConditionally(content, "div")
self.fixImagesPath(content)
# convert beautifulsoup obj -> str
content = content.decode_contents(eventual_encoding = self.charset)
content = self.regexps['killBreaks'].sub("<br />", content)
return content
def clean(self, e ,tag):
targetList = e.find_all(tag)
isEmbed = 0
if tag == 'object' or tag == 'embed':
isEmbed = 1
for target in targetList:
attributeValues = ""
for attribute in target.attrs:
attributeValues += target[attribute[0]]
if isEmbed and self.regexps['videos'].search(attributeValues):
continue
if isEmbed and self.regexps['videos'].search(target.decode_contents(eventual_encoding = self.charset) ):
continue
target.extract()
def cleanStyle(self, e):
for elem in e.find_all(True):
del elem['class']
del elem['id']
del elem['style']
def cleanConditionally(self, e, tag):
tagsList = e.find_all(tag)
for node in tagsList:
weight = self.getClassWeight(node)
hashNode = hash(str(node))
if hashNode in self.candidates:
contentScore = self.candidates[hashNode]['score']
else:
contentScore = 0
if weight + contentScore < 0:
node.extract()
else:
p = len(node.find_all("p"))
img = len(node.find_all("img"))
li = len(node.find_all("li"))-100
input = len(node.find_all("input"))
embedCount = 0
embeds = node.find_all("embed")
for embed in embeds:
if not self.regexps['videos'].search(embed['src']):
embedCount += 1
linkDensity = self.getLinkDensity(node)
contentLength = len(node.text)
toRemove = False
if img > p:
toRemove = True
elif li > p and tag != "ul" and tag != "ol":
toRemove = True
elif input > math.floor(p/3):
toRemove = True
elif contentLength < 25 and (img==0 or img>2):
toRemove = True
elif weight < 25 and linkDensity > 0.2:
toRemove = True
elif weight >= 25 and linkDensity > 0.5:
toRemove = True
elif (embedCount == 1 and contentLength < 35) or embedCount > 1:
toRemove = True
if toRemove:
node.extract()
def getArticleTitle(self):
# here just get the <title>
title = ''
try:
title = self.html.find('title').string
except:
pass
return str(title)
def initializeNode(self, node):
contentScore = 0
if node.name == 'div':
contentScore += 5;
elif node.name == 'blockquote':
contentScore += 3;
elif node.name == 'form':
contentScore -= 3;
elif node.name == 'th':
contentScore -= 5;
contentScore += self.getClassWeight(node)
return {'score':contentScore, 'node': node}
def getClassWeight(self, node):
weight = 0
if 'class' in node:
if self.regexps['negative'].search(node['class']):
weight -= 25
if self.regexps['positive'].search(node['class']):
weight += 25
if 'id' in node:
if self.regexps['negative'].search(node['id']):
weight -= 25
if self.regexps['positive'].search(node['id']):
weight += 25
return weight
def getLinkDensity(self, node):
links = node.find_all('a')
textLength = len(node.text)
if textLength == 0:
return 0
linkLength = 0
for link in links:
linkLength += len(link.text)
return linkLength / textLength
def fixImagesPath(self, node):
imgs = node.find_all('img')
for img in imgs:
src = img.get('src',None)
if not src:
img.extract()
continue
if 'http://' != src[:7] and 'https://' != src[:8]:
newSrc = urllib.parse.urljoin(self.url, src)
newSrcArr = urllib.parse.urlparse(newSrc)
newPath = posixpath.normpath(newSrcArr[2])
newSrc = urllib.parse.urlunparse((newSrcArr.scheme, newSrcArr.netloc, newPath,
newSrcArr.params, newSrcArr.query, newSrcArr.fragment))
img['src'] = newSrc