ayanokoji1331 commited on
Commit
252adc9
·
verified ·
1 Parent(s): 754a4ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
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 for Headless Browser ---
12
  def get_base64_image(image_path):
13
- if os.path.exists(image_path):
 
 
 
 
14
  with open(image_path, "rb") as img_file:
15
- return base64.b64encode(img_file.read()).decode('utf-8')
 
16
  return ""
17
 
18
  # --- The HTML/CSS Template ---
19
- def get_html_template(player, team, stat_val, stat_name, hook, bg_base64):
20
 
21
- bg_img_element = f'<img class="bg-image" src="data:image/png;base64,{bg_base64}" />' if bg_base64 else ''
 
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
- # --- THE FIX ---
127
- # safe_player is ONLY used for saving the final PNG filename safely
128
  safe_player = player.replace(" ", "_").replace("/", "")
129
 
130
- # player_img_path searches for the exact database name WITH spaces in your assets folder
131
- player_img_path = os.path.join("assets", f"{player}.png")
132
- dummy_img_path = os.path.join("assets", "dummy_bg.png")
 
 
 
 
 
 
133
 
134
  if os.path.exists(player_img_path):
135
- bg_base64 = get_base64_image(player_img_path)
136
  slide_prefix = "Cover" if slide_num == 1 else "Slide"
137
  else:
138
- bg_base64 = get_base64_image(dummy_img_path)
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, bg_base64))
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)