|
| 1 | +import dataclasses |
| 2 | +import logging |
| 3 | +import typing as t |
| 4 | +from copy import deepcopy |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from bs4 import BeautifulSoup |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +@dataclasses.dataclass |
| 13 | +class HTMLImage: |
| 14 | + alt: str |
| 15 | + src: str |
| 16 | + |
| 17 | + |
| 18 | +class HTMLImageTranslator: |
| 19 | + """ |
| 20 | + Translate local image references into remote ones, by uploading them. |
| 21 | + After that, replace URLs in HTML document. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, html: str, source_path: t.Union[str, Path], uploader: t.Optional[t.Callable] = None): |
| 25 | + self.html_in: str = html |
| 26 | + self.html_out: t.Optional[str] = None |
| 27 | + self.source_path = source_path |
| 28 | + self.uploader = uploader |
| 29 | + self.images_in: t.List[HTMLImage] = [] |
| 30 | + self.images_local: t.List[HTMLImage] = [] |
| 31 | + self.images_remote: t.List[HTMLImage] = [] |
| 32 | + |
| 33 | + def __str__(self): |
| 34 | + return ( |
| 35 | + f"HTMLImageTranslator:\nin: {self.images_in}\nlocal: {self.images_local}\nremote: {self.images_remote}" |
| 36 | + ) |
| 37 | + |
| 38 | + def discover(self): |
| 39 | + self.scan().resolve() |
| 40 | + return self |
| 41 | + |
| 42 | + def process(self): |
| 43 | + self.upload() |
| 44 | + self.produce() |
| 45 | + return self |
| 46 | + |
| 47 | + def scan(self) -> "HTMLImageTranslator": |
| 48 | + """ |
| 49 | + Scan input HTML for all <img ...> tags. |
| 50 | + """ |
| 51 | + soup = BeautifulSoup(self.html_in, features="html.parser") |
| 52 | + images = soup.find_all(name="img") |
| 53 | + self.images_in = [] |
| 54 | + for image in images: |
| 55 | + self.images_in.append(HTMLImage(src=image.get("src"), alt=image.get("alt"))) |
| 56 | + return self |
| 57 | + |
| 58 | + def resolve(self) -> "HTMLImageTranslator": |
| 59 | + """ |
| 60 | + Process discovered image elements, computing effective paths. |
| 61 | + """ |
| 62 | + if self.source_path is None: |
| 63 | + return self |
| 64 | + parent_path = Path(self.source_path) |
| 65 | + if parent_path.is_file(): |
| 66 | + parent_path = parent_path.parent |
| 67 | + self.images_local = [] |
| 68 | + for image in self.images_in: |
| 69 | + image_new = deepcopy(image) |
| 70 | + if not image.src.startswith("http://") and not image.src.startswith("https://"): |
| 71 | + # Use absolute paths 1:1. |
| 72 | + if image.src.startswith("/"): |
| 73 | + pass |
| 74 | + |
| 75 | + # Relative paths are relative to the original document. |
| 76 | + else: |
| 77 | + image_new.src = str(Path(parent_path) / image.src) |
| 78 | + self.images_local.append(image_new) |
| 79 | + return self |
| 80 | + |
| 81 | + def upload(self) -> "HTMLImageTranslator": |
| 82 | + """ |
| 83 | + Upload images to HubSpot API, and store URLs. |
| 84 | + """ |
| 85 | + if self.uploader is None: |
| 86 | + logger.warning("No upload without uploader") |
| 87 | + return self |
| 88 | + for image_local in self.images_local: |
| 89 | + hs_file = self.uploader(source=image_local.src, name=Path(image_local.src).name) |
| 90 | + image_url = hs_file.url |
| 91 | + image_remote: HTMLImage = deepcopy(image_local) |
| 92 | + image_remote.src = image_url |
| 93 | + self.images_remote.append(image_remote) |
| 94 | + return self |
| 95 | + |
| 96 | + def produce(self) -> "HTMLImageTranslator": |
| 97 | + """ |
| 98 | + Produce HTML output, with all image references replaced by their remote targets. |
| 99 | + """ |
| 100 | + html = self.html_in |
| 101 | + for image_in, image_remote in zip(self.images_in, self.images_remote): |
| 102 | + html = html.replace(image_in.src, image_remote.src) |
| 103 | + self.html_out = html |
| 104 | + return self |
0 commit comments