1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 23:03:00 +00:00
fftcgtool/fftcg/book.py

85 lines
2.6 KiB
Python
Raw Normal View History

2021-08-23 11:43:12 +00:00
import bz2
2021-08-23 12:56:30 +00:00
import logging
import pickle
2021-08-04 16:36:23 +00:00
from PIL import Image
2021-08-09 05:01:25 +00:00
from .cards import Cards
from .imageloader import ImageLoader
from .utils import GRID, RESOLUTION, BOOK_PICKLE_NAME, CARD_BACK_URL
2021-08-04 16:36:23 +00:00
2021-08-04 01:39:19 +00:00
class Book:
2021-08-17 15:37:28 +00:00
def __init__(self, cards: Cards, language: str, num_threads: int):
2021-08-04 16:36:23 +00:00
logger = logging.getLogger(__name__)
# sort cards by element, then alphabetically
cards.sort(key=lambda x: x.name)
cards.sort(key=lambda x: "Multi" if len(x.elements) > 1 else x.elements[0])
# all card face URLs
2021-08-20 22:16:38 +00:00
urls = [
f"https://fftcg.cdn.sewest.net/images/cards/full/{card.code}_{language}.jpg"
for card in cards
]
2021-08-18 11:49:34 +00:00
# card back URL
urls.append(CARD_BACK_URL)
2021-08-09 12:03:45 +00:00
# multi-threaded download
2021-08-20 22:16:38 +00:00
images = ImageLoader.load(urls, num_threads)
# card back Image
back_image = images.pop(-1)
2021-08-04 01:39:19 +00:00
2021-08-04 16:36:23 +00:00
self.__pages = []
2021-08-18 14:52:32 +00:00
page_images: list[Image.Image]
page_cards: Cards
2021-08-18 15:38:58 +00:00
for page_num, (page_images, page_cards) in enumerate(zip(GRID.chunks(images), GRID.chunks(cards))):
# create book page Image
2021-08-17 15:37:28 +00:00
page_image = Image.new("RGB", GRID * RESOLUTION)
2021-08-09 12:03:45 +00:00
logger.info(f"New image: {page_image.size[0]}x{page_image.size[1]}")
2021-08-04 16:36:23 +00:00
# paste card faces onto page
2021-08-09 12:03:45 +00:00
for i, image in enumerate(page_images):
2021-08-17 15:37:28 +00:00
GRID.paste(page_image, i, image)
# paste card back in last position
2021-08-17 15:37:28 +00:00
GRID.paste(page_image, GRID.capacity, back_image)
2021-08-09 12:03:45 +00:00
2021-08-18 14:52:32 +00:00
# set card indices
for i, card in enumerate(page_cards):
card.index = i
2021-08-09 12:03:45 +00:00
# save page
self.__pages.append({
2021-08-18 15:38:58 +00:00
"file_name": f"{cards.file_name}_{page_num}.jpg",
2021-08-09 12:03:45 +00:00
"image": page_image,
"cards": page_cards,
})
2021-08-04 16:36:23 +00:00
2021-08-18 14:52:32 +00:00
def save(self) -> None:
# save images
for i, page in enumerate(self.__pages):
# save page image
2021-08-18 15:38:58 +00:00
page["image"].save(page["file_name"])
2021-08-16 01:46:27 +00:00
2021-08-18 14:52:32 +00:00
book: dict[str, Cards]
2021-08-16 01:46:27 +00:00
try:
2021-08-23 11:43:12 +00:00
with bz2.BZ2File(BOOK_PICKLE_NAME, "r") as file:
book = pickle.load(file)
2021-08-16 01:46:27 +00:00
except FileNotFoundError:
book = {}
2021-08-16 01:46:27 +00:00
# save book
2021-08-04 16:36:23 +00:00
for i, page in enumerate(self.__pages):
2021-08-18 14:52:32 +00:00
# ask for upload
2021-08-18 15:38:58 +00:00
face_url = input(f"Upload '{page['file_name']}' and paste URL: ")
2021-08-18 14:52:32 +00:00
for card in page["cards"]:
card.face_url = face_url
# add contents of that image
2021-08-18 15:38:58 +00:00
book[page["file_name"]] = page["cards"]
# update book.yml file
2021-08-23 11:43:12 +00:00
with bz2.BZ2File(BOOK_PICKLE_NAME, "w") as file:
pickle.dump(book, file)