2021-08-04 01:39:19 +00:00
|
|
|
import io
|
|
|
|
import logging
|
|
|
|
import queue
|
2021-08-04 01:41:14 +00:00
|
|
|
import threading
|
2021-08-04 01:39:19 +00:00
|
|
|
|
|
|
|
import requests
|
2021-08-04 01:41:14 +00:00
|
|
|
from PIL import Image
|
2021-08-04 01:39:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImageLoader(threading.Thread):
|
|
|
|
# Card faces by Square API
|
|
|
|
__FACE_URL = "https://fftcg.cdn.sewest.net/images/cards/full/{}_{}.jpg"
|
|
|
|
|
|
|
|
# Card back image by Aurik
|
|
|
|
__BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/"
|
|
|
|
|
|
|
|
def __init__(self, card_queue, resolution, language):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
self.__queue = card_queue
|
|
|
|
self.__resolution = resolution
|
|
|
|
self.__language = language
|
|
|
|
self.__images = {}
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
logger = logging.getLogger(__name__)
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-08-04 01:39:19 +00:00
|
|
|
while not self.__queue.empty():
|
|
|
|
# take next card
|
2021-08-04 16:36:23 +00:00
|
|
|
card = self.__queue.get()
|
2021-08-04 01:39:19 +00:00
|
|
|
|
|
|
|
# fetch card image (retry on fail)
|
|
|
|
while True:
|
2021-08-04 16:36:23 +00:00
|
|
|
logger.info(f"get image for card {card}")
|
2021-08-04 01:39:19 +00:00
|
|
|
try:
|
2021-08-04 16:36:23 +00:00
|
|
|
res = requests.get(ImageLoader.__FACE_URL.format(card.code, self.__language))
|
2021-08-04 01:39:19 +00:00
|
|
|
image = Image.open(io.BytesIO(res.content))
|
2021-08-04 18:15:38 +00:00
|
|
|
|
|
|
|
# unify images
|
2021-08-04 01:39:19 +00:00
|
|
|
image.convert("RGB")
|
|
|
|
image = image.resize(self.__resolution, Image.BICUBIC)
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# put image in correct position
|
2021-08-04 16:36:23 +00:00
|
|
|
self.__images[card] = image
|
2021-08-04 01:39:19 +00:00
|
|
|
|
|
|
|
# image is processed
|
|
|
|
self.__queue.task_done()
|
|
|
|
|
|
|
|
@classmethod
|
2021-08-04 16:36:23 +00:00
|
|
|
def load(cls, cards, resolution, language, num_threads):
|
2021-08-04 01:39:19 +00:00
|
|
|
card_queue = queue.Queue()
|
2021-08-04 16:36:23 +00:00
|
|
|
for card in cards:
|
|
|
|
card_queue.put(card)
|
2021-08-04 01:39:19 +00:00
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
loaders = []
|
2021-08-04 01:39:19 +00:00
|
|
|
for _ in range(num_threads):
|
2021-08-04 16:36:23 +00:00
|
|
|
loader = cls(card_queue, resolution, language)
|
|
|
|
loaders.append(loader)
|
|
|
|
loader.start()
|
|
|
|
|
|
|
|
card_queue.join()
|
|
|
|
|
|
|
|
images = {}
|
|
|
|
for loader in loaders:
|
|
|
|
images = {**images, **loader.images}
|
2021-08-04 01:39:19 +00:00
|
|
|
|
2021-08-04 18:15:38 +00:00
|
|
|
# sort images to match the initial "cards" list
|
|
|
|
images = [images[card] for card in cards]
|
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
return images
|
2021-08-04 01:39:19 +00:00
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
@property
|
|
|
|
def images(self):
|
2021-08-04 01:39:19 +00:00
|
|
|
return self.__images
|