From 21fe5c419ace5cca51b43ecf3189803968e3ac18 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Mon, 4 Nov 2024 23:17:07 -0600 Subject: [PATCH] Flip image libraries due to white bar bug --- Dockerfile | 60 ++++++++++++++++++++------------------- app/db_engine.py | 2 +- app/routers_v2/players.py | 37 +++++++++++++++--------- app/routers_v2/teams.py | 4 +-- requirements.txt | 1 + 5 files changed, 58 insertions(+), 46 deletions(-) diff --git a/Dockerfile b/Dockerfile index 392f99a..0a2fcfa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,37 +3,39 @@ FROM tiangolo/uvicorn-gunicorn-fastapi:latest WORKDIR /usr/src/app # Chrome dependency Instalation -RUN apt-get update && apt-get install -y \ - fonts-liberation \ - libasound2 \ - libatk-bridge2.0-0 \ - libatk1.0-0 \ - libatspi2.0-0 \ - libcups2 \ - libdbus-1-3 \ - libdrm2 \ - libgbm1 \ - libgtk-3-0 \ -# libgtk-4-1 \ - libnspr4 \ - libnss3 \ - libwayland-client0 \ - libxcomposite1 \ - libxdamage1 \ - libxfixes3 \ - libxkbcommon0 \ - libxrandr2 \ - xdg-utils \ - libu2f-udev \ - libvulkan1 - # Chrome instalation -RUN curl -LO https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -RUN apt-get install -y ./google-chrome-stable_current_amd64.deb -RUN rm google-chrome-stable_current_amd64.deb -# Check chrome version -RUN echo "Chrome: " && google-chrome --version +# RUN apt-get update && apt-get install -y \ +# fonts-liberation \ +# libasound2 \ +# libatk-bridge2.0-0 \ +# libatk1.0-0 \ +# libatspi2.0-0 \ +# libcups2 \ +# libdbus-1-3 \ +# libdrm2 \ +# libgbm1 \ +# libgtk-3-0 \ +# # libgtk-4-1 \ +# libnspr4 \ +# libnss3 \ +# libwayland-client0 \ +# libxcomposite1 \ +# libxdamage1 \ +# libxfixes3 \ +# libxkbcommon0 \ +# libxrandr2 \ +# xdg-utils \ +# libu2f-udev \ +# libvulkan1 +# # Chrome instalation +# RUN curl -LO https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb +# RUN apt-get install -y ./google-chrome-stable_current_amd64.deb +# RUN rm google-chrome-stable_current_amd64.deb +# # Check chrome version +# RUN echo "Chrome: " && google-chrome --version COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt +RUN playwright install +RUN playwright install-deps COPY ./app /app/app \ No newline at end of file diff --git a/app/db_engine.py b/app/db_engine.py index d78d928..ec22d91 100644 --- a/app/db_engine.py +++ b/app/db_engine.py @@ -37,7 +37,7 @@ CARDSETS = { }, 'minor-league': { 'primary': [20, 8], # 1998, Mario - 'secondary': [6], # 2008 + 'secondary': [6], # 2013 'human': [x for x in range(1, 30)] }, 'major-league': { diff --git a/app/routers_v2/players.py b/app/routers_v2/players.py index fd70c53..41d45e2 100644 --- a/app/routers_v2/players.py +++ b/app/routers_v2/players.py @@ -11,6 +11,7 @@ from typing import Optional, List, Literal import logging import pydantic from pandas import DataFrame +from playwright.async_api import async_playwright from ..card_creation import get_batter_card_data, get_pitcher_card_data from ..db_engine import db, Player, model_to_dict, fn, chunked, Paperdex, Cardset, Rarity, BattingCard, \ @@ -451,23 +452,31 @@ async def get_batter_card( ).execute() logging.debug(f'Rating updates: {updates}') - - hti = Html2Image( - browser='chromium', - size=(1200, 600), - output_path=f'storage/cards/cardset-{this_player.cardset.id}/{card_type}/', - custom_flags=['--no-sandbox', '--disable-remote-debugging', '--headless', '--disable-gpu', - '--disable-software-rasterizer', '--disable-dev-shm-usage'] - ) - logging.debug(f'body:\n{html_response.body.decode("UTF-8")}') - x = hti.screenshot( - html_str=str(html_response.body.decode("UTF-8")), - save_as=f'{player_id}-{d}-v{variant}.png' - ) + + file_path = f'storage/cards/cardset-{this_player.cardset.id}/{card_type}/{player_id}-{d}-v{variant}.png' + async with async_playwright() as p: + browser = await p.chromium.launch() + page = await browser.new_page() + await page.set_content(html_response.body.decode("UTF-8")) + await page.screenshot(path=file_path, type='png', clip={'x': 0.0, 'y': 0, 'width': 1200, 'height': 600}) + await browser.close() + + # hti = Html2Image( + # browser='chrome', + # size=(1200, 600), + # output_path=f'storage/cards/cardset-{this_player.cardset.id}/{card_type}/', + # custom_flags=['--no-sandbox', '--disable-remote-debugging', '--headless', '--disable-gpu', + # '--disable-software-rasterizer', '--disable-dev-shm-usage'] + # ) + + # x = hti.screenshot( + # html_str=str(html_response.body.decode("UTF-8")), + # save_as=f'{player_id}-{d}-v{variant}.png' + # ) db.close() - return FileResponse(path=x[0], media_type='image/png', headers=headers) + return FileResponse(path=file_path, media_type='image/png', headers=headers) # @router.get('/{player_id}/pitchingcard') diff --git a/app/routers_v2/teams.py b/app/routers_v2/teams.py index 3466dc0..46533d4 100644 --- a/app/routers_v2/teams.py +++ b/app/routers_v2/teams.py @@ -519,7 +519,7 @@ async def get_team_sp( db.close() raise HTTPException(status_code=400, detail=f'Difficulty name {difficulty_name} not a valid check') - all_players = Player.select().where(Player.mlbclub == this_team.lname) + all_players = Player.select().where(Player.franchise == this_team.lname) if difficulty_name == 'exhibition': logging.info(f'pulling an exhibition lineup') @@ -619,7 +619,7 @@ async def get_team_rp( raise HTTPException(status_code=400, detail=f'Difficulty name {difficulty_name} not a valid check') all_players = Player.select().where( - (Player.mlbclub == this_team.lname) & (Player.player_id.not_in(used_pitcher_ids)) + (Player.franchise == this_team.lname) & (Player.player_id.not_in(used_pitcher_ids)) ) if difficulty_name == 'exhibition': diff --git a/requirements.txt b/requirements.txt index 73764e6..c8fb835 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ python-multipart requests html2image jinja2 +playwright