Improved URL Handling, Added View to Post convertion

This commit is contained in:
2025-08-01 23:38:20 +02:00
parent 317d81f995
commit 7ad8b14fdc
3 changed files with 42 additions and 6 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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"<View {self.id}>"