2020-12-08 04:26:30 +01:00
|
|
|
import cgi
|
2022-04-14 21:52:41 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from zerver.lib.url_preview.types import UrlEmbedData
|
2016-10-27 12:06:44 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-11-05 11:37:41 +01:00
|
|
|
class BaseParser:
|
2020-12-08 04:26:30 +01:00
|
|
|
def __init__(self, html_source: bytes, content_type: Optional[str]) -> None:
|
2018-08-08 22:24:20 +02:00
|
|
|
# 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
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-12-08 04:26:30 +01:00
|
|
|
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)
|
2016-10-27 12:06:44 +02:00
|
|
|
|
2022-04-14 21:52:41 +02:00
|
|
|
def extract_data(self) -> UrlEmbedData:
|
2023-02-04 02:07:20 +01:00
|
|
|
raise NotImplementedError
|