Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make domlogo more robust when images are missing or folders not created #126

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion domlogo/domlogo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
import platform
import shlex
import yaml
from PIL import Image, ImageDraw, ImageFont


def generate_placeholder(text: str, path: str, background_color, width: int, height: int):
image = Image.new("RGB", (width, height), background_color)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 72)
text_bbox = draw.textbbox((0, 0), text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
text_x = (width - text_width) / 2
text_y = (height - text_height) / 2
draw.text((text_x, text_y), text, fill=(255,255,255), font=font)

# Add some hint how to replace the image.
small_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 16)
placeholder_text = f'(Replace this placeholder image at {path})'
text_bbox = draw.textbbox((0, 0), placeholder_text, font=small_font)
text_width = text_bbox[2] - text_bbox[0]
text_x = (width - text_width) / 2
text_y = text_y + 1.4*text_height
draw.text((text_x, text_y), placeholder_text, fill=(195,196,199), font=small_font)
image.save(path)


def download_image(image_type: str, entity_id: str, file: dict):
Expand All @@ -26,6 +49,7 @@ def download_image(image_type: str, entity_id: str, file: dict):

if existing_etag != etag:
print(f'Downloading and converting {image_type} for entity with ID {entity_id}...')
os.makedirs(os.path.dirname(temp_file), exist_ok=True)
with open(temp_file, 'wb') as f:
f.write(requests.get(f'{api_url}/{href}', auth=(user, passwd)).content)

Expand All @@ -39,6 +63,11 @@ def download_image(image_type: str, entity_id: str, file: dict):
host = platform.node()
host_bg_color = 'black'

idle_image = 'domlogo-files/photos/idle.png'
if not os.path.isfile(idle_image):
os.makedirs(os.path.dirname(idle_image), exist_ok=True)
generate_placeholder('judgehost idle', idle_image, (10,75,120), 1024, 768)

config_file = 'domlogo-files/config.yaml'
if os.path.isfile(config_file) and os.access(config_file, os.R_OK):
with open(config_file, 'r') as f:
Expand Down Expand Up @@ -172,7 +201,7 @@ def download_image(image_type: str, entity_id: str, file: dict):
last_seen = (submission_id, judging_id, team_id)
new_filename = f'domlogo-files/photos/{team_id}.png'
if not os.path.isfile(new_filename):
new_filename = f'domlogo-files/photos/crew.png'
generate_placeholder(f'team {team_id}', new_filename, (137,28,28), 1024, 768)
team_image.update(filename=new_filename)
metadata_text.update(f's{submission_id} / {submission_data["problem_id"]} / {submission_data["language_id"]}')
results_text.update('Busy compiling.')
Expand Down
Loading