Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
✨ feat(screenshot): Save screenshot to 800x600px webp (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot authored Sep 15, 2022
1 parent c61edea commit 16fcdec
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 26 deletions.
2 changes: 2 additions & 0 deletions ecoindex_scraper/scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ScreenShot,
WindowSize,
)
from ecoindex_scraper.utils import convert_screenshot_to_webp


class EcoindexScraper:
Expand Down Expand Up @@ -99,6 +100,7 @@ async def scrap_page(self) -> Tuple[PageMetrics, PageType]:
async def generate_screenshot(self) -> None:
if self.screenshot and self.screenshot.folder and self.screenshot.id:
self.driver.save_screenshot(self.screenshot.__str__())
convert_screenshot_to_webp(self.screenshot)

async def scroll_to_bottom(self) -> None:
try:
Expand Down
18 changes: 18 additions & 0 deletions ecoindex_scraper/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from os import unlink

from PIL import Image

from ecoindex_scraper.models import ScreenShot


def convert_screenshot_to_webp(screenshot: ScreenShot) -> None:
image = Image.open(rf"{screenshot.__str__()}")
width, height = image.size
ratio = 800 / height if width > height else 600 / width

image.convert("RGB").resize(size=(int(width * ratio), int(height * ratio))).save(
rf"{screenshot.folder}/{screenshot.id}.webp",
format="webp",
)

unlink(f"{screenshot.__str__()}")
175 changes: 149 additions & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ include = [
python = "^3.8"
ecoindex = "^5.0.0"
undetected-chromedriver = "^3.1.5"
Pillow = "^9.2.0"

[tool.poetry.dev-dependencies]
pytest = "^7.1"
Expand Down

0 comments on commit 16fcdec

Please sign in to comment.