Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,17 +8,23 @@ from supabase import create_client
|
|
| 8 |
from html2image import Html2Image
|
| 9 |
from datetime import datetime
|
| 10 |
|
| 11 |
-
# --- Helper: Convert Image to Base64
|
| 12 |
def get_base64_image(image_path):
|
| 13 |
-
if os.path.exists(image_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
with open(image_path, "rb") as img_file:
|
| 15 |
-
|
|
|
|
| 16 |
return ""
|
| 17 |
|
| 18 |
# --- The HTML/CSS Template ---
|
| 19 |
-
def get_html_template(player, team, stat_val, stat_name, hook,
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
return f"""
|
| 24 |
<!DOCTYPE html>
|
|
@@ -97,6 +103,9 @@ def generate_and_zip(supabase_url, supabase_key, num_carousels):
|
|
| 97 |
b_id = post.get('batch_id', 'Unbatched')
|
| 98 |
batches[b_id].append(post)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
for batch_id, posts in batches.items():
|
| 101 |
logs += f"\n📦 Assembling Folder: {batch_id}\n"
|
| 102 |
|
|
@@ -123,19 +132,24 @@ def generate_and_zip(supabase_url, supabase_key, num_carousels):
|
|
| 123 |
hook = post['image_hook']
|
| 124 |
caption = post['instagram_caption']
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
# safe_player is ONLY used for saving the final PNG filename safely
|
| 128 |
safe_player = player.replace(" ", "_").replace("/", "")
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
player_img_path = os.path.join(
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
if os.path.exists(player_img_path):
|
| 135 |
-
|
| 136 |
slide_prefix = "Cover" if slide_num == 1 else "Slide"
|
| 137 |
else:
|
| 138 |
-
|
| 139 |
slide_prefix = "Slide"
|
| 140 |
|
| 141 |
filename = f"{slide_num}_{slide_prefix}_{safe_player}.png"
|
|
@@ -143,7 +157,7 @@ def generate_and_zip(supabase_url, supabase_key, num_carousels):
|
|
| 143 |
|
| 144 |
temp_html = f"{safe_player}_temp.html"
|
| 145 |
with open(temp_html, "w", encoding="utf-8") as f:
|
| 146 |
-
f.write(get_html_template(player, team, stat_val, stat_name, hook,
|
| 147 |
|
| 148 |
try:
|
| 149 |
hti.screenshot(html_file=temp_html, save_as=filename)
|
|
|
|
| 8 |
from html2image import Html2Image
|
| 9 |
from datetime import datetime
|
| 10 |
|
| 11 |
+
# --- Helper: Convert Image to Base64 (Now supports JPG and PNG) ---
|
| 12 |
def get_base64_image(image_path):
|
| 13 |
+
if image_path and os.path.exists(image_path):
|
| 14 |
+
# Detect if it's a jpeg or png to set the correct HTML data type
|
| 15 |
+
ext = os.path.splitext(image_path)[1].lower().replace('.', '')
|
| 16 |
+
mime_type = 'jpeg' if ext in ['jpg', 'jpeg'] else 'png'
|
| 17 |
+
|
| 18 |
with open(image_path, "rb") as img_file:
|
| 19 |
+
b64_str = base64.b64encode(img_file.read()).decode('utf-8')
|
| 20 |
+
return f"data:image/{mime_type};base64,{b64_str}"
|
| 21 |
return ""
|
| 22 |
|
| 23 |
# --- The HTML/CSS Template ---
|
| 24 |
+
def get_html_template(player, team, stat_val, stat_name, hook, bg_data_uri):
|
| 25 |
|
| 26 |
+
# Inject the exact Base64 Data URI
|
| 27 |
+
bg_img_element = f'<img class="bg-image" src="{bg_data_uri}" />' if bg_data_uri else ''
|
| 28 |
|
| 29 |
return f"""
|
| 30 |
<!DOCTYPE html>
|
|
|
|
| 103 |
b_id = post.get('batch_id', 'Unbatched')
|
| 104 |
batches[b_id].append(post)
|
| 105 |
|
| 106 |
+
# Auto-detect if folder is named 'assests' or 'assets'
|
| 107 |
+
folder_name = "assests" if os.path.exists("assests") else "assets"
|
| 108 |
+
|
| 109 |
for batch_id, posts in batches.items():
|
| 110 |
logs += f"\n📦 Assembling Folder: {batch_id}\n"
|
| 111 |
|
|
|
|
| 132 |
hook = post['image_hook']
|
| 133 |
caption = post['instagram_caption']
|
| 134 |
|
| 135 |
+
# Use safe_player for BOTH the search and the output filename
|
|
|
|
| 136 |
safe_player = player.replace(" ", "_").replace("/", "")
|
| 137 |
|
| 138 |
+
# Check for player image
|
| 139 |
+
player_img_path = os.path.join(folder_name, f"{safe_player}.png")
|
| 140 |
+
|
| 141 |
+
# Check for dummy_bg (supports .jpg, .jpeg, and .png)
|
| 142 |
+
dummy_img_path = None
|
| 143 |
+
for ext in ['.jpg', '.jpeg', '.png']:
|
| 144 |
+
if os.path.exists(os.path.join(folder_name, f"dummy_bg{ext}")):
|
| 145 |
+
dummy_img_path = os.path.join(folder_name, f"dummy_bg{ext}")
|
| 146 |
+
break
|
| 147 |
|
| 148 |
if os.path.exists(player_img_path):
|
| 149 |
+
bg_data_uri = get_base64_image(player_img_path)
|
| 150 |
slide_prefix = "Cover" if slide_num == 1 else "Slide"
|
| 151 |
else:
|
| 152 |
+
bg_data_uri = get_base64_image(dummy_img_path) if dummy_img_path else ""
|
| 153 |
slide_prefix = "Slide"
|
| 154 |
|
| 155 |
filename = f"{slide_num}_{slide_prefix}_{safe_player}.png"
|
|
|
|
| 157 |
|
| 158 |
temp_html = f"{safe_player}_temp.html"
|
| 159 |
with open(temp_html, "w", encoding="utf-8") as f:
|
| 160 |
+
f.write(get_html_template(player, team, stat_val, stat_name, hook, bg_data_uri))
|
| 161 |
|
| 162 |
try:
|
| 163 |
hti.screenshot(html_file=temp_html, save_as=filename)
|