Files
zulip/zerver/lib/url_preview/parsers/base.py
Anders Kaseorg 531b34cb4c ruff: Fix UP007 Use X | Y for type annotations.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-13 22:28:22 -07:00

20 lines
694 B
Python

import cgi
from zerver.lib.url_preview.types import UrlEmbedData
class BaseParser:
def __init__(self, html_source: bytes, content_type: str | None) -> None:
# We import BeautifulSoup here, because it's not used by most
# processes in production, and bs4 is big enough that
# importing it adds 10s of milliseconds to manage.py startup.
from bs4 import BeautifulSoup
charset = None
if content_type is not None:
charset = cgi.parse_header(content_type)[1].get("charset")
self._soup = BeautifulSoup(html_source, "lxml", from_encoding=charset)
def extract_data(self) -> UrlEmbedData:
raise NotImplementedError