Adds a comic format registry
There is the limitation that you still need to manually import all modules that register a new comic format, but this does give us a lot of flexibility
This commit is contained in:
@@ -16,15 +16,20 @@ import requests
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from comic_download.comic_strip import Comic, ComicStrip
|
||||
from comic_download.comic_strip import Comic, ComicStrip, register_comic_class
|
||||
|
||||
class XKCDComic(Comic):
|
||||
"""
|
||||
The XKCD Comic.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("xkcd")
|
||||
|
||||
@classmethod
|
||||
def create_from_url(cls, url):
|
||||
return cls()
|
||||
|
||||
def _load_data(self):
|
||||
self.title = "XKCD"
|
||||
self.author = "Randal Munroe"
|
||||
@@ -37,6 +42,15 @@ class XKCDComic(Comic):
|
||||
self.data = result.json()
|
||||
self.latest_identifier = self.data["num"]
|
||||
|
||||
def get_first_strip(self):
|
||||
return 1
|
||||
|
||||
def get_latest_strip(self):
|
||||
return self.latest_identifier
|
||||
|
||||
def get_all_strips(self) -> list:
|
||||
return list(range(self.get_first_strip(), self.get_latest_strip() + 1))
|
||||
|
||||
def __new__(cls):
|
||||
if not hasattr(cls, 'instance'):
|
||||
cls.instance = super(XKCDComic, cls).__new__(cls)
|
||||
@@ -46,13 +60,14 @@ class XKCDComicStrip(ComicStrip):
|
||||
"""
|
||||
A comic strip object represents a single XKCD strip.
|
||||
"""
|
||||
def __init__(self, index: int):
|
||||
def __init__(self, comic, index: int):
|
||||
"""
|
||||
Creates a comic strip object.
|
||||
|
||||
:param comic: The XKCD comic.
|
||||
:param index: The index of the strip to load.
|
||||
"""
|
||||
super().__init__(XKCDComic(), index)
|
||||
super().__init__(comic, index)
|
||||
self.url = f"https://xkcd.com/{index}"
|
||||
self.data_url = f"{self.url}/info.0.json"
|
||||
self.transcript = None
|
||||
@@ -120,3 +135,7 @@ class XKCDComicStrip(ComicStrip):
|
||||
f_img.save(f_image)
|
||||
f_image.close()
|
||||
self.transformed_images.append(f_image)
|
||||
|
||||
|
||||
XKCDComic.strip_cls = XKCDComicStrip
|
||||
register_comic_class("xkcd.com", XKCDComic)
|
||||
|
||||
Reference in New Issue
Block a user