Skip to content

Commit

Permalink
Merge pull request #105 from reworkd/resource-responder
Browse files Browse the repository at this point in the history
Added fake image response
  • Loading branch information
kunwar-reworkd authored Jan 2, 2025
2 parents fecb4fd + be9b650 commit b655d27
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "harambe-core"
version = "0.58.0"
version = "0.59.0"
description = "Core types for harambe SDK 🐒🍌"
authors = [
{ name = "Adam Watkins", email = "[email protected]" }
Expand Down
2 changes: 1 addition & 1 deletion core/uv.lock

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

15 changes: 11 additions & 4 deletions sdk/harambe/handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import base64
from abc import ABC
from typing import Any, Literal, Self

Expand All @@ -21,6 +22,10 @@
"*",
]

FAKE_IMAGE_BYTES = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
)


class AbstractHandler(ABC):
async def handle(self, route: Route) -> None:
Expand Down Expand Up @@ -74,13 +79,15 @@ def captured_url(self) -> str | None:
return self._new_pages[0] if self._new_pages else None


class UnnecessaryResourceHandler(AbstractHandler):
class UnnecessaryResourceHandler:
async def handle(self, route: Route) -> None:
resource_type = route.request.resource_type
url = route.request.url

if (
resource_type in ["image", "media", "font"]
if resource_type in ["image", "media"]:
await route.fulfill(body=FAKE_IMAGE_BYTES, content_type="image/png")
return
elif (
resource_type == "font"
or re.match(r"^data:(image|audio|video)", url)
or re.match(r"social-widget|tracking-script|ads", url)
):
Expand Down
4 changes: 2 additions & 2 deletions sdk/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[project]
name = "harambe-sdk"
version = "0.58.0"
version = "0.59.0"
description = "Data extraction SDK for Playwright 🐒🍌"
authors = [
{ name = "Adam Watkins", email = "[email protected]" }
]
requires-python = ">=3.11,<4.0"
readme = "README.md"
dependencies = [
"harambe_core==0.58.0",
"harambe_core==0.59.0",
"playwright==1.47.0",
"beautifulsoup4==4.12.3",
"requests==2.32.3",
Expand Down
4 changes: 2 additions & 2 deletions sdk/uv.lock

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

0 comments on commit b655d27

Please sign in to comment.