2021-08-23 12:56:30 +00:00
|
|
|
import logging
|
2021-08-23 14:00:17 +00:00
|
|
|
import os
|
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
|
2021-08-23 14:46:53 +00:00
|
|
|
from .utils import GRID, RESOLUTION, CARD_BACK_URL, IMAGES_DIR_NAME
|
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__)
|
|
|
|
|
2021-08-04 22:33:18 +00:00
|
|
|
# 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])
|
|
|
|
|
2021-08-04 22:17:47 +00:00
|
|
|
# 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-04 22:17:47 +00:00
|
|
|
|
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)
|
2021-08-04 22:17:47 +00:00
|
|
|
# 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
|
|
|
|
2021-08-18 15:38:58 +00:00
|
|
|
for page_num, (page_images, page_cards) in enumerate(zip(GRID.chunks(images), GRID.chunks(cards))):
|
2021-08-04 22:17:47 +00:00
|
|
|
# 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
|
|
|
|
2021-08-04 22:17:47 +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)
|
2021-08-04 22:17:47 +00:00
|
|
|
|
|
|
|
# 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:
|
2021-08-23 14:00:17 +00:00
|
|
|
if not os.path.exists(IMAGES_DIR_NAME):
|
|
|
|
os.mkdir(IMAGES_DIR_NAME)
|
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
# save images
|
2021-08-23 14:46:53 +00:00
|
|
|
for page in self.__pages:
|
|
|
|
page["file_name"] = os.path.join(IMAGES_DIR_NAME, page["file_name"])
|
|
|
|
page["image"].save(page["file_name"])
|
|
|
|
|
|
|
|
# ask for upload
|
|
|
|
for page in self.__pages:
|
2021-08-18 15:38:58 +00:00
|
|
|
face_url = input(f"Upload '{page['file_name']}' and paste URL: ")
|
2021-08-23 14:46:53 +00:00
|
|
|
|
|
|
|
if not face_url:
|
|
|
|
face_url = f"file://{os.path.abspath(page['file_name'])}"
|
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
for card in page["cards"]:
|
|
|
|
card.face_url = face_url
|