Fixed some implementation bugs.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# This CLI tool allows for the download and packaging of various internet comics into .cbz files.
|
# This CLI tool allows for the download and packaging of various internet comics into .cbz files.
|
||||||
# Copyright (C) 2025 Markil 3
|
# Copyright (C) 2026 Markil 3
|
||||||
# http://www.singlepilot.net
|
# http://www.singlepilot.net
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
@@ -39,8 +39,9 @@ from tempfile import NamedTemporaryFile
|
|||||||
import requests
|
import requests
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from bs4.element import Tag
|
||||||
|
|
||||||
from comic_download.comic_strip import AssetRepo, ComicBook, register_comic_class
|
from comic_download.comic_strip import AssetRepo, ComicBook, register_comic_class, file_safe_string
|
||||||
|
|
||||||
class RedditComic(ComicBook):
|
class RedditComic(ComicBook):
|
||||||
"""
|
"""
|
||||||
@@ -55,7 +56,7 @@ class RedditComic(ComicBook):
|
|||||||
the subreddit and the post ID.
|
the subreddit and the post ID.
|
||||||
"""
|
"""
|
||||||
super().__init__(identifier)
|
super().__init__(identifier)
|
||||||
self.url = f"https://www.reddit.com/r/{identifier[0]}/{identifier[1]}"
|
self.url = f"https://www.reddit.com/r/{identifier[0]}/comments/{identifier[1]}"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_from_url(cls, url):
|
def create_from_url(cls, url):
|
||||||
@@ -64,8 +65,11 @@ class RedditComic(ComicBook):
|
|||||||
url = "https://" + url
|
url = "https://" + url
|
||||||
url = urlparse(url)
|
url = urlparse(url)
|
||||||
urlpath = url.path.split('/')
|
urlpath = url.path.split('/')
|
||||||
subreddit = urlpath[1]
|
subreddit = urlpath[2]
|
||||||
post = urlpath[2]
|
if urlpath[3] == "comments":
|
||||||
|
post = urlpath[4]
|
||||||
|
else:
|
||||||
|
post = urlpath[3]
|
||||||
return cls((subreddit, post))
|
return cls((subreddit, post))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -78,49 +82,83 @@ class RedditComic(ComicBook):
|
|||||||
"""The ID of the post this comic."""
|
"""The ID of the post this comic."""
|
||||||
return self.identifier[1]
|
return self.identifier[1]
|
||||||
|
|
||||||
|
def get_package_path(self, base_path: Path):
|
||||||
|
return Path(base_path, file_safe_string(self.author), f"{self.get_filename()}.cbz")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_image_url(cls, img_el) -> str:
|
def get_image_url(cls, img_el) -> str:
|
||||||
"""
|
"""
|
||||||
Obtains the highest resolution URL for an image element
|
Obtains the highest resolution URL for an image element
|
||||||
"""
|
"""
|
||||||
if "data-lazy-srcset" in img_el.attrs:
|
if "data-lazy-srcset" in img_el.attrs and img_el.attrs["data-lazy-srcset"]:
|
||||||
srcsets = img_el.attrs["data-lazy-srcset"].split(', ')
|
srcsets = img_el.attrs["data-lazy-srcset"].split(', ')
|
||||||
img_url = None
|
img_url = None
|
||||||
max_res = 0
|
max_res = 0
|
||||||
for srcset in srcsets:
|
for srcset in srcsets:
|
||||||
|
if " " in srcset:
|
||||||
url, resolution = srcset.split(" ")
|
url, resolution = srcset.split(" ")
|
||||||
resolution_i = int(resulution[0:-1])
|
resolution_i = int(resolution[0:-1])
|
||||||
if resolution_i > max_res:
|
if resolution_i > max_res:
|
||||||
max_res = resolution_i
|
max_res = resolution_i
|
||||||
img_url = url
|
img_url = url
|
||||||
return url
|
elif srcset:
|
||||||
|
max_res = 0
|
||||||
|
img_url = srcset
|
||||||
|
return img_url
|
||||||
|
elif "data-lazy-src" in img_el.attrs:
|
||||||
|
return img_el.attrs["data-lazy-src"]
|
||||||
elif "src" in img_el.attrs:
|
elif "src" in img_el.attrs:
|
||||||
return img_el.attrs["src"]
|
return img_el.attrs["src"]
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _load_data(self):
|
def _load_data(self):
|
||||||
result = requests.get(self.url)
|
result = requests.get(self.url, headers={"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0"})
|
||||||
result.raise_for_status()
|
result.raise_for_status()
|
||||||
|
|
||||||
content = BeautifulSoup(result.content, features="lxml")
|
content = BeautifulSoup(result.content, features="lxml")
|
||||||
|
|
||||||
|
new_url = content.find(id="canonical-url-updater")
|
||||||
|
if new_url:
|
||||||
|
result = requests.get(new_url.attrs["value"], headers={"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0"})
|
||||||
|
result.raise_for_status
|
||||||
|
|
||||||
|
content = BeautifulSoup(result.content, features="lxml")
|
||||||
|
|
||||||
|
id=f"post-title-t3_{self.post}"
|
||||||
title = content.find(id=f"post-title-t3_{self.post}")
|
title = content.find(id=f"post-title-t3_{self.post}")
|
||||||
if title and title["content"]:
|
if title and title.string:
|
||||||
self.title = title["content"]
|
self.title = title.string.strip()
|
||||||
post_content = title.next_sibling
|
author = content.find("a", class_="author-name")
|
||||||
|
if author and author.string:
|
||||||
|
self.author = author.string.strip()
|
||||||
|
date_el = content.find("faceplate-timeago")
|
||||||
|
if date_el and "ts" in date_el.attrs:
|
||||||
|
self.date = datetime.fromisoformat(date_el.attrs["ts"])
|
||||||
|
post_content = None
|
||||||
|
for sibling in title.next_siblings:
|
||||||
|
if isinstance(sibling, Tag):
|
||||||
|
post_content = sibling
|
||||||
|
break
|
||||||
post_content_image = post_content.find(id="post-image")
|
post_content_image = post_content.find(id="post-image")
|
||||||
if post_content_image:
|
if post_content_image:
|
||||||
self.image_urls.append(self.get_image_url(post_content_image))
|
img_url = self.get_image_url(post_content_image)
|
||||||
|
self.image_urls.append(img_url)
|
||||||
return
|
return
|
||||||
post_content_list = post_content.find("ul")
|
post_content_list = post_content.find("ul")
|
||||||
if post_content_list:
|
if post_content_list:
|
||||||
for item in post_content_list.find_all("li"):
|
for item in post_content_list.find_all("li"):
|
||||||
img_el = item.contents[0]
|
img_el = item.contents[1]
|
||||||
self.image_urls.append(image_el)
|
img_url = self.get_image_url(img_el)
|
||||||
|
self.image_urls.append(img_url)
|
||||||
|
return
|
||||||
|
preview_image = post_content.find("img", class_="preview-image")
|
||||||
|
if preview_image:
|
||||||
|
img_url = self.get_image_url(preview_image)
|
||||||
|
self.image_urls.append(img_url)
|
||||||
return
|
return
|
||||||
|
|
||||||
def _transform_images(self):
|
def _transform_images(self):
|
||||||
self.transformed_images = self.images
|
self.transformed_images = self.images
|
||||||
|
|
||||||
register_comic_class("reddit.com", RedditComic)
|
register_comic_class("reddit.com", RedditComic)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user