Created
November 17, 2022 11:32
-
-
Save DuqueDeTuring/ea7023f56416ba619190725709f3d13d to your computer and use it in GitHub Desktop.
small http server, receives an html string and returns it converted into text (without: tags, links, tables, images, etc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from http.server import BaseHTTPRequestHandler, HTTPServer | |
import html2text | |
class Handler(BaseHTTPRequestHandler): | |
def do_POST(self): | |
try: | |
data = self.rfile.read(int(self.headers['Content-Length'])).decode("utf-8") | |
content = h.handle(data.strip()).strip() | |
print(content) | |
self.send_response(200) | |
except Exception as exc: | |
print(exc) | |
self.send_response(500) | |
content = "ERROR converter" | |
self.send_header("Content-Length", str(len(content))) | |
self.send_header("Content-Type", "text/html") | |
self.end_headers() | |
self.wfile.write(bytes(content, "utf-8")) | |
h = html2text.HTML2Text() | |
h.ignore_links = True | |
h.bypass_tables = True | |
h.ignore_images = True | |
h.ignore_anchors = True | |
h.ignore_emphasis = True | |
with HTTPServer(('', 8007), Handler) as server: | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
exploring using it as a little service to convert Mastodon's API html content to plain text and showing the result on a small LCD screen connected to a Raspberry Pi Pico W.