<commit_before><commit_msg>Add task to create Lutris icons from MAME icons<commit_after>"""Generate banners from ProgettoSnap marquees"""
import os
from PIL import Image

from django.core.files.base import ContentFile
from django.core.management.base import BaseCommand
from django.conf import settings
from common.util import crop_banner
from games.models import Game


if settings.DEBUG:
    MAME_ICO_PATH = "/media/strider/Backup/Games/Arcade/icons"
else:
    MAME_ICO_PATH = os.path.join(settings.MEDIA_ROOT, "mame/icons")
ICON_PATH = os.path.join(settings.MEDIA_ROOT, "mame/icons-png")


class Command(BaseCommand):
    """Resize banners and icons"""

    @staticmethod
    def make_icon_from_icon(game):
        """Generate a banner for a game from available marquees"""
        mame_ids = [pgame.slug for pgame in game.provider_games.all()]
        for mame_id in mame_ids:
            mame_ico_path = os.path.join(MAME_ICO_PATH, "%s.ico" % mame_id)
            if not os.path.exists(mame_ico_path):
                continue
            icon_filename = "%s.png" % mame_id
            icon_path = os.path.join(ICON_PATH, icon_filename)
            try:
                ico_file = Image.open(mame_ico_path)
            except ValueError:
                print("Failed to read %s" % mame_ico_path)
                continue
            ico_file.save(icon_path, "PNG")
            with open(icon_path, "rb") as banner_file:
                icon_content = banner_file.read()
            game.icon = ContentFile(icon_content, icon_filename)
            game.save()
            print("Icon created for %s" % game)
            return

    def handle(self, *args, **_kwargs):
        """Run command"""
        if not os.path.exists(ICON_PATH):
            os.makedirs(ICON_PATH)

        for game in Game.objects.filter(provider_games__provider__name="MAME"):
            if game.icon:
                continue
            self.make_icon_from_icon(game)
{'fraction_non_alphanumeric': 0.09589743589743589, 'fraction_numerical': 0.0, 'mean_word_length': 2.496415770609319, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7311398', 'n_tokens_mistral': 611, 'n_tokens_neox': 595, 'n_words': 140}