[networking] fix watch pages that aren't text/html

This commit is contained in:
nemunaire 2015-07-01 18:15:35 +02:00
parent 487cb13e14
commit 0c960e984a
2 changed files with 26 additions and 11 deletions

View file

@ -74,6 +74,20 @@ def fetch(url, onNone=_onNoneDefault):
raise IRCException(e.strerror)
def _render(cnt):
"""Render the page contained in cnt as HTML page"""
if cnt is None:
return None
with tempfile.NamedTemporaryFile() as fp:
fp.write(cnt.encode())
args = ["w3m", "-T", "text/html", "-dump"]
args.append(fp.name)
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
return proc.stdout.read().decode()
def render(url, onNone=_onNoneDefault):
"""Use w3m to render the given url
@ -81,16 +95,7 @@ def render(url, onNone=_onNoneDefault):
url -- the URL to render
"""
with tempfile.NamedTemporaryFile() as fp:
cnt = fetch(url, onNone)
if cnt is None:
return None
fp.write(cnt.encode())
args = ["w3m", "-T", "text/html", "-dump"]
args.append(fp.name)
with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
return proc.stdout.read().decode()
return _render(fetch(url, onNone))
def traceURL(url, stack=None):