Bladeren bron

Minor refactor in HTML parser.

tags/v0.3
Ben Kurtovic 8 jaren geleden
bovenliggende
commit
f2099df5d5
1 gewijzigde bestanden met toevoegingen van 15 en 6 verwijderingen
  1. +15
    -6
      earwigbot/wiki/copyvios/parsers.py

+ 15
- 6
earwigbot/wiki/copyvios/parsers.py Bestand weergeven

@@ -232,6 +232,20 @@ class _HTMLParser(_BaseTextParser):
"script", "style"
]

def _fail_if_mirror(self, soup):
"""Look for obvious signs that the given soup is a wiki mirror.

If so, raise ParserExclusionError, which is caught in the workers and
causes this source to excluded.
"""
if "mirror_hints" not in self._args:
return

func = lambda attr: attr and any(
hint in attr for hint in self._args["mirror_hints"])
if soup.find_all(href=func) or soup.find_all(src=func):
raise ParserExclusionError()

def parse(self):
"""Return the actual text contained within an HTML document.

@@ -248,12 +262,7 @@ class _HTMLParser(_BaseTextParser):
# no scrapable content (possibly JS or <frame> magic):
return ""

if "mirror_hints" in self._args:
# Look for obvious signs that this is a mirror:
func = lambda attr: attr and any(
hint in attr for hint in self._args["mirror_hints"])
if soup.find_all(href=func) or soup.find_all(src=func):
raise ParserExclusionError()
self._fail_if_mirror(soup)

soup = soup.body
is_comment = lambda text: isinstance(text, bs4.element.Comment)


Laden…
Annuleren
Opslaan