-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.py
More file actions
320 lines (271 loc) · 10.2 KB
/
readability.py
File metadata and controls
320 lines (271 loc) · 10.2 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
320
from bs4 import BeautifulSoup, NavigableString, Comment
import re
import copy
class Readability:
div_to_p_elems=[ "a", "blockquote", "dl", "div", "img", "ol", "p", "pre", "table", "ul", "select" ]
REGEXES = {
'unlikelyCandidatesRe': 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),
'okMaybeItsACandidateRe': re.compile('and|article|body|column|main|shadow', re.I),
'positiveRe': re.compile('article|body|content|entry|hentry|main|page|pagination|post|text|blog|story', re.I),
'negativeRe': 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),
'divToPElementsRe': re.compile('<(a|blockquote|dl|div|img|ol|p|pre|table|ul)', re.I),
#'replaceBrsRe': re.compile('(<br[^>]*>[ \n\r\t]*){2,}',re.I),
#'replaceFontsRe': re.compile('<(\/?)font[^>]*>',re.I),
#'trimRe': re.compile('^\s+|\s+$/'),
#'normalizeRe': re.compile('\s{2,}/'),
#'killBreaksRe': re.compile('(<br\s*\/?>(\s| ?)*){1,}/'),
'videoRe': re.compile('https?:\/\/(www\.)?(youtube|vimeo)\.com', re.I),
'attributeRe': re.compile('blog|post|article', re.I),
#skipFootnoteLink: /^\s*(\[?[a-z0-9]{1,2}\]?|^|edit|citation needed)\s*$/i,
}
def __init__(self, html, preserveUnlikelyCandidates = False):
self.html = html
self.preserveUnlikelyCandidates = preserveUnlikelyCandidates
self.title = ''
self.article = ''
self.soup = None
def minify(self, html):
# minified = re.sub('\n+','\n',re.sub('\n+','\n',re.sub(' +',' ',re.sub('\t','',html))))
minified = re.sub('\n+','\n',re.sub('<!--.*?-->','',re.sub('<script.*?</script>','',re.sub('<script.*?[^>]*</script>','',re.sub('<!--[^>]*-->','',re.sub(' <','<',re.sub('> ','>',re.sub('> <','><',re.sub(' +',' ',re.sub('\t','',html)))))))).strip()))
return minified
def RepresentsInt(self, s):
try:
int(s)
return True
except ValueError:
return False
def getClassWeight(self, node):
weight = 0
if node.has_attr('class'):
classNames = ' '.join(node['class'])
if self.REGEXES['negativeRe'].search(classNames):
weight -= 25
if self.REGEXES['positiveRe'].search(classNames):
weight += 25
if node.has_attr('id') and not self.RepresentsInt(node['id']):
if self.REGEXES['negativeRe'].search(node['id']):
weight -= 25
if self.REGEXES['positiveRe'].search(node['id']):
weight += 25
return weight
def initializeNode(self, node):
node['readability-score']=0
score_weight = {
'article': 10,
'section': 8,
'div': 5,
'pre': 3,
'td': 3,
'blockquote': 3,
'address': -3,
'ol': -3,
'ul': -3,
'dl': -3,
'dd': -3,
'dt': -3,
'li': -3,
'form': -3,
'h1': -5,
'h2': -5,
'h3': -5,
'h4': -5,
'h5': -5,
'h6': -5,
'th': -5,
}
node['readability-score'] += score_weight.get(node.name, 0)
if node.has_attr('itemscope'):
node['readability-score'] += 5
if node.has_attr('itemtype') and self.REGEXES['attributeRe'].search(node['itemtype']):
node['readability-score'] += 30
node['readability-score'] += self.getClassWeight(node)
def getInnerText(self, elem):
textContent = str(elem.text).strip()
textContent = textContent.replace('\s{2,}/g',' ')
return textContent.strip()
def getLinkDensity(self, elem):
textLength = len(self.getInnerText(elem))
if textLength == 0:
return 0
linkLength=0
for atag in elem.findAll("a"):
try:
if atag['href'][0] == "#":
continue
linkLength += len(self.getInnerText(atag))
except KeyError:
continue
return linkLength / textLength
def getArticleMetadata(self):
metadata = {}
values = {}
# property is a space-separated list of values
propertyPattern = "\s*(dc|dcterm|og|twitter)\s*\:\s*(author|creator|description|title|site_name|type)\s*"
# name is a single value
namePattern = "^\s*(?:(dc|dcterm|og|twitter|weibo:(article|webpage))\s*[\.:]\s*)?(author|creator|description|title|site_name|type)\s*$"
for meta in self.soup.find_all('meta'):
elementName = meta.get("name", "")
elementProperty = meta.get("property", "")
content = meta.get("content", "")
matches = None
name = None
matches = re.findall(propertyPattern, elementProperty)
if elementProperty:
if matches:
for match in matches:
if isinstance(match, tuple):
name = ":".join(list(match)).replace("\s", '')
else:
name = match.lower().replace('\s', '')
values[name] = content.strip()
if not matches and elementName and re.search(namePattern, elementName):
name = elementName
if content:
name = name.lower().replace('\s', '').replace('.', ':')
values[name] = content.strip()
metadata = {
# get title
"title": values.get("dc:title") or
values.get("dcterm:title") or
values.get("og:title") or
values.get("weibo:article:title") or
values.get("weibo:webpage:title") or
values.get("title") or
values.get("twitter:title"),
# get author
"byline": values.get("dc:creator") or
values.get("dcterm:creator") or
values.get("author"),
# get description
"excerpt": values.get("dc:description") or
values.get("dcterm:description") or
values.get("og:description") or
values.get("weibo:article:description") or
values.get("weibo:webpage:description") or
values.get("description") or
values.get("twitter:description"),
# get type
"type": values.get("og:type", "default"),
# get site name
"siteName": values.get("og:site_name")
}
return metadata
# def get_own_textContent(element):
# textContent=''
# if not isinstance(element, NavigableString):
# if hasattr(element, 'children'):
# for child in element.children:
# if isinstance(child, NavigableString):
# textContent+=str(child);
# if textContent == '':
# return None
# else:
# return re.sub('\n+','\n', textContent.strip())
@staticmethod
def removeScripts(soup):
[s.extract() for s in soup('script')]
return soup
@staticmethod
def removeComments(soup):
for element in soup.find_all():
if isinstance(element, Comment):
element.extract()
return soup
@staticmethod
def removeElements(soup):
for element in soup:
element.extract()
return soup
def grabArticle(self):
for node in self.soup.findAll():
continueFlag = False
if not self.preserveUnlikelyCandidates:
classNames = ''
if node.has_attr('class'):
classNames = ' '.join(node['class'])
idName = ''
if node.has_attr('id'):
idName=node['id']
unlikelyMatchString = classNames + idName
if self.REGEXES['unlikelyCandidatesRe'].search(unlikelyMatchString) and not self.REGEXES['okMaybeItsACandidateRe'].search(unlikelyMatchString) and node.name != 'html' and node.name != 'body':
node.extract()
continueFlag = True
if not continueFlag and node.name == "div":
if not self.REGEXES['divToPElementsRe'].search(str(node)):
node.name = "p"
else:
for child in node.children:
if isinstance(child, NavigableString) and str(child).strip() != "":
new_p = self.soup.new_tag('p')
new_p.append(str(child))
child.insert_before(new_p)
child.extract()
candidates = []
for paragraph in self.soup.findAll('p'):
parentNode = paragraph.parent
grandParentNode = parentNode.parent
InnerText = self.getInnerText(paragraph)
if len(InnerText) < 25:
continue
if not parentNode.has_attr('readability-score'):
self.initializeNode(parentNode)
candidates.append(parentNode)
if not grandParentNode.has_attr('readability-score'):
self.initializeNode(grandParentNode)
candidates.append(grandParentNode)
contentScore = 0
# Add a point for the paragraph itself as a base.
contentScore += 1
# For every 100 characters in this paragraph, add another point. Up to 3 points.
contentScore += min(len(InnerText) / 100, 3)
# Add the score to the parent. The grandparent gets half.
parentNode['readability-score'] += contentScore;
grandParentNode['readability-score'] += contentScore / 2;
topCandidate = {}
# get top candidate
for candidate in candidates:
candidate['readability-score'] = candidate['readability-score'] * (1 - self.getLinkDensity(candidate))
if not topCandidate or candidate['readability-score'] > topCandidate['readability-score']:
topCandidate = candidate
'''
Now that we have the top candidate, look through its siblings for content that might also be related.
Things like preambles, content split by ads that we removed, etc.
'''
if topCandidate:
articleContent = self.soup.new_tag('div')
articleContent['id'] = 'readability-content'
siblingsScoreThreshold = max(10, topCandidate['readability-score'] * 0.2);
siblingNodes = topCandidate.parent.children
for siblingNode in siblingNodes:
if not isinstance(siblingNode, NavigableString):
append = False
if siblingNode == topCandidate:
append = True
if siblingNode.has_attr('readability-score') and siblingNode['readability-score'] >= siblingsScoreThreshold:
append = True
if siblingNode.name == "p":
linkDensity = self.getLinkDensity(siblingNode)
nodeContent = self.getInnerText(siblingNode)
nodeLength = len(nodeContent)
if nodeLength > 80 and linkDensity == 0 and re.search('/\.( |$)/', nodeContent):
append = True
if append:
articleContent.append(copy.copy(siblingNode))
return articleContent
else:
return topCandidate
def parse(self):
### MINIFY HTML
html = self.minify(self.html)
self.soup = BeautifulSoup(html, 'html5lib')
self.soup = self.removeScripts(self.soup)
self.soup = self.removeComments(self.soup)
self.removeElements(self.soup.find_all('style'))
metadata = self.getArticleMetadata()
article = self.grabArticle()
return {
**metadata,
# "textContent": self.minify(article.get("text", "")),
"textContent": self.minify(article.text),
"content": str(article)
}