diff --git a/py34/post.py b/py34/post.py index 9fb3c0e..b7adc8a 100644 --- a/py34/post.py +++ b/py34/post.py @@ -1,4 +1,5 @@ from .scraper import scraper +from .url import ImageURL from io import BytesIO from PIL import Image from PIL.ImageFile import ImageFile @@ -30,8 +31,9 @@ class Post: return self._image_data for ext in ["jpeg", "png", "gif", "mp4", "webm", "jpg"]: try: - self._image_data = scraper.get(f"https://wimg.rule34.xxx//images/{self.image_dir}/{self.image_id}.{ext}", retry=False) + self._image_data = scraper.get(ImageURL(self.image_dir, self.image_id, ext), retry=False) self._image_format = ext + break except Exception as ex: exception = ex if self._image_data is None: diff --git a/py34/url.py b/py34/url.py index f95762b..3e1f7e5 100644 --- a/py34/url.py +++ b/py34/url.py @@ -6,7 +6,10 @@ class ImageURL: def __init__(self, image_dir: int, image_id: str, image_format: str): self.dir: int = image_dir self.id: str = image_id - self.format: str = image_format + self.format: str = image_format.lstrip(".") + + def __str__(self) -> str: + return f"https://wimg.rule34.xxx//images/{self.dir}/{self.id}.{self.format}" class ThumbnailURL: @@ -14,6 +17,9 @@ class ThumbnailURL: self.dir: int = image_dir self.id: str = image_id + def __str__(self) -> str: + return f"https://wimg.rule34.xxx//thumbnails/{self.dir}/thumbnail_{self.id}.jpg" + def parse_image_url(url: str) -> ImageURL: url = urlparse(url) diff --git a/py34/view.py b/py34/view.py index c294fa8..e9d2728 100644 --- a/py34/view.py +++ b/py34/view.py @@ -1,3 +1,5 @@ +from .post import Post +from .url import ImageURL, ThumbnailURL, parse_image_url from .scraper import scraper from io import BytesIO from PIL import Image @@ -33,9 +35,12 @@ class ViewTags: class View: def __init__(self, id: int): self.id = id - self._image_data: bytes | None = None - self._image: ImageFile | None = None - self._image_url: str | None = None + self._image_data: bytes | None = None + self._image: ImageFile | None = None + self._image_url: ImageURL | None = None + self._thumb_data: bytes | None = None + self._thumb: ImageFile | None = None + self._thumb_url: ThumbnailURL | None = None document = scraper.get_html(f"https://rule34.xxx/index.php?page=post&s=view&id={id}") tag_bar = document.find_all("ul", attrs={"id": "tag-sidebar"})[0] @@ -66,7 +71,9 @@ class View: label = ent.text.lower().strip() match label: case "original image": - self._image_url = ent.find_all("a")[0]["href"] + self._image_url = parse_image_url(ent.find_all("a")[0]["href"]) + + self._thumb_url = ThumbnailURL(self._image_url.dir, self._image_url.id) def get_image(self) -> ImageFile: @@ -83,6 +90,27 @@ class View: return self._image_data + def get_thumbnail(self) -> ImageFile: + if self._thumb is not None: + return self._thumb + self._thumb = Image.open(BytesIO(self.get_thumbnail_data())) + return self._thumb + + + def get_thumbnail_data(self) -> bytes: + if self._thumb_data is not None: + return self._thumb_data + self._thumb_data = scraper.get(self._thumb_url) + + + def to_post(self) -> Post: + return Post( + self.id, + self._image_url.dir, + self._image_url.id, + self.tags.to_list(), + ) + def __str__(self): return f""