Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,230 +1,240 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
figma.currentPage = page;
|
| 35 |
-
|
| 36 |
-
const frame = figma.createFrame();
|
| 37 |
-
frame.name = "Website Frame";
|
| 38 |
-
frame.resize(1440, 900);
|
| 39 |
-
frame.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
|
| 40 |
-
|
| 41 |
-
const nodeMap = new Map();
|
| 42 |
-
|
| 43 |
-
for (const element of data.elements) {
|
| 44 |
-
let parentNode = element.parentId ? nodeMap.get(element.parentId) : frame;
|
| 45 |
-
|
| 46 |
-
if (!parentNode) {
|
| 47 |
-
console.log('Parent not found for:', element.id, 'using main frame');
|
| 48 |
-
parentNode = frame;
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
}
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
try {
|
| 166 |
-
const { r, g, b } = hexToRgb(colorStr);
|
| 167 |
-
const colorRect = figma.createRectangle();
|
| 168 |
-
colorRect.resize(30, 30);
|
| 169 |
-
colorRect.x = 20;
|
| 170 |
-
colorRect.y = colorY;
|
| 171 |
-
colorRect.fills = [{ type: 'SOLID', color: { r, g, b } }];
|
| 172 |
-
|
| 173 |
-
const colorLabel = figma.createText();
|
| 174 |
-
await figma.loadFontAsync({ family: "Inter", style: "Regular" });
|
| 175 |
-
label.fontName = { family: "Inter", style: "Regular" };
|
| 176 |
-
label.characters = colorStr;
|
| 177 |
-
label.fontSize = 12;
|
| 178 |
-
label.x = 60;
|
| 179 |
-
label.y = colorY + 8;
|
| 180 |
-
|
| 181 |
-
colorSection.appendChild(colorRect);
|
| 182 |
-
colorSection.appendChild(colorLabel);
|
| 183 |
-
colorY += 40;
|
| 184 |
-
} catch (error) {
|
| 185 |
-
console.error("Error creating color swatch:", error);
|
| 186 |
-
}
|
| 187 |
}
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
figma.viewport.scrollAndZoomIntoView([frame]);
|
| 200 |
-
|
| 201 |
-
figma.ui.postMessage({ type: 'conversion-complete' });
|
| 202 |
-
} catch (error) {
|
| 203 |
-
console.error("Conversion error:", error);
|
| 204 |
-
figma.ui.postMessage({
|
| 205 |
-
type: 'conversion-error',
|
| 206 |
-
error: error.message
|
| 207 |
-
});
|
| 208 |
-
}
|
| 209 |
-
}
|
| 210 |
-
};
|
| 211 |
-
|
| 212 |
-
function hexToRgb(hex) {
|
| 213 |
-
hex = hex.replace('#', '');
|
| 214 |
-
const r = parseInt(hex.length === 3 ? hex[0] + hex[0] : hex.slice(0, 2), 16) / 255;
|
| 215 |
-
const g = parseInt(hex.length === 3 ? hex[1] + hex[1] : hex.slice(2, 4), 16) / 255;
|
| 216 |
-
const b = parseInt(hex.length === 3 ? hex[2] + hex[2] : hex.slice(4, 6), 16) / 255;
|
| 217 |
-
return { r, g, b };
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
-
async function fetchImage(url) {
|
| 221 |
-
try {
|
| 222 |
-
const response = await fetch(url);
|
| 223 |
-
if (!response.ok) throw new Error(`Failed to fetch image: ${response.status}`);
|
| 224 |
-
const arrayBuffer = await response.arrayBuffer();
|
| 225 |
-
return new Uint8Array(arrayBuffer);
|
| 226 |
-
} catch (e) {
|
| 227 |
-
console.error('Image fetch error:', e);
|
| 228 |
-
throw e;
|
| 229 |
-
}
|
| 230 |
-
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from urllib.parse import urljoin
|
| 7 |
+
import logging
|
| 8 |
+
import re
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Enable CORS
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"],
|
| 16 |
+
allow_credentials=True,
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Configure logging
|
| 22 |
+
logging.basicConfig(level=logging.INFO)
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
class WebsiteURL(BaseModel):
|
| 26 |
+
url: str
|
| 27 |
+
|
| 28 |
+
@app.post("/extract")
|
| 29 |
+
async def extract_website(website_url: WebsiteURL):
|
| 30 |
+
try:
|
| 31 |
+
# Fetch the website content
|
| 32 |
+
headers = {
|
| 33 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
+
response = requests.get(website_url.url, headers=headers, timeout=15)
|
| 36 |
+
response.raise_for_status()
|
| 37 |
+
|
| 38 |
+
# Parse the HTML
|
| 39 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 40 |
+
|
| 41 |
+
# Fetch external stylesheets
|
| 42 |
+
for link in soup.find_all('link', rel='stylesheet'):
|
| 43 |
+
href = link.get('href')
|
| 44 |
+
if href:
|
| 45 |
+
css_url = urljoin(website_url.url, href)
|
| 46 |
+
try:
|
| 47 |
+
css_response = requests.get(css_url, headers=headers, timeout=5)
|
| 48 |
+
css_response.raise_for_status()
|
| 49 |
+
css_soup = BeautifulSoup(f"<style>{css_response.text}</style>", 'html.parser')
|
| 50 |
+
soup.append(css_soup)
|
| 51 |
+
logger.info(f"Fetched stylesheet: {css_url}")
|
| 52 |
+
except requests.RequestException as e:
|
| 53 |
+
logger.warning(f"Failed to fetch stylesheet {css_url}: {e}")
|
| 54 |
+
|
| 55 |
+
elements = []
|
| 56 |
+
y_offset = 20
|
| 57 |
+
element_index = 0
|
| 58 |
+
|
| 59 |
+
def parse_styles(style_text):
|
| 60 |
+
styles = {}
|
| 61 |
+
try:
|
| 62 |
+
for style_item in style_text.split(';'):
|
| 63 |
+
if ':' in style_item:
|
| 64 |
+
prop, val = style_item.split(':', 1)
|
| 65 |
+
styles[prop.strip()] = val.strip()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logger.warning(f"Error parsing styles: {e}")
|
| 68 |
+
return styles
|
| 69 |
+
|
| 70 |
+
def get_styles(element):
|
| 71 |
+
styles = parse_styles(element.get('style', ''))
|
| 72 |
+
# Process <style> tags
|
| 73 |
+
for style_tag in soup.find_all('style'):
|
| 74 |
+
try:
|
| 75 |
+
css = style_tag.string or ''
|
| 76 |
+
for rule in css.split('}'):
|
| 77 |
+
if '{' in rule:
|
| 78 |
+
selector, props = rule.split('{', 1)
|
| 79 |
+
selector = selector.strip()
|
| 80 |
+
if selector.startswith('.') and 'class' in element.attrs:
|
| 81 |
+
classes = element.get('class', [])
|
| 82 |
+
if selector[1:] in classes:
|
| 83 |
+
styles.update(parse_styles(props))
|
| 84 |
+
elif selector.startswith('#') and element.get('id') == selector[1:]:
|
| 85 |
+
styles.update(parse_styles(props))
|
| 86 |
+
elif selector.lower() == element.name:
|
| 87 |
+
styles.update(parse_styles(props))
|
| 88 |
+
except Exception as e:
|
| 89 |
+
logger.warning(f"Error parsing <style> tag: {e}")
|
| 90 |
+
return styles
|
| 91 |
+
|
| 92 |
+
def safe_int(value, default=100):
|
| 93 |
+
if not value:
|
| 94 |
+
return default
|
| 95 |
+
value = re.sub(r'[^\d]', '', str(value))
|
| 96 |
+
try:
|
| 97 |
+
return int(value) if value else default
|
| 98 |
+
except ValueError:
|
| 99 |
+
logger.warning(f"Invalid value for int conversion: {value}")
|
| 100 |
+
return default
|
| 101 |
+
|
| 102 |
+
def parse_color(color):
|
| 103 |
+
if not color:
|
| 104 |
+
return '#FFFFFF'
|
| 105 |
+
color = color.lower()
|
| 106 |
+
if color.startswith('#'):
|
| 107 |
+
return color
|
| 108 |
+
if color.startswith('rgb'):
|
| 109 |
+
try:
|
| 110 |
+
nums = re.findall(r'\d+', color)
|
| 111 |
+
r, g, b = map(int, nums[:3])
|
| 112 |
+
return f'#{r:02x}{g:02x}{b:02x}'
|
| 113 |
+
except:
|
| 114 |
+
return '#FFFFFF'
|
| 115 |
+
return '#FFFFFF'
|
| 116 |
+
|
| 117 |
+
def process_element(tag, parent_id=None, depth=0):
|
| 118 |
+
nonlocal elements, y_offset, element_index
|
| 119 |
+
styles = get_styles(tag)
|
| 120 |
+
logger.info(f"Processing tag: {tag.name}, text: {tag.get_text(strip=True)[:50]}")
|
| 121 |
+
element = {
|
| 122 |
+
'id': f'element-{element_index}',
|
| 123 |
+
'x': safe_int(styles.get('left', styles.get('margin-left', '0px')), 20 + depth * 20),
|
| 124 |
+
'y': safe_int(styles.get('top', styles.get('margin-top', '0px')), y_offset + depth * 20),
|
| 125 |
+
'parentId': parent_id
|
| 126 |
}
|
| 127 |
+
element_index += 1
|
| 128 |
+
|
| 129 |
+
if tag.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button', 'li', 'strong', 'em']:
|
| 130 |
+
text = tag.get_text(strip=True)
|
| 131 |
+
if text:
|
| 132 |
+
font_size = safe_int(styles.get('font-size', '16px'), 16)
|
| 133 |
+
element.update({
|
| 134 |
+
'type': 'text',
|
| 135 |
+
'tag': tag.name,
|
| 136 |
+
'content': text[:1000] or 'Text Placeholder',
|
| 137 |
+
'fontSize': font_size,
|
| 138 |
+
'color': parse_color(styles.get('color', '#000000')),
|
| 139 |
+
'fontFamily': styles.get('font-family', 'Inter'),
|
| 140 |
+
'fontWeight': styles.get('font-weight', '400')
|
| 141 |
+
})
|
| 142 |
+
y_offset += font_size + 20
|
| 143 |
+
elements.append(element)
|
| 144 |
+
logger.info(f"Added text element: {text[:50]}")
|
| 145 |
+
|
| 146 |
+
elif tag.name == 'img':
|
| 147 |
+
src = tag.get('src', '')
|
| 148 |
+
if src:
|
| 149 |
+
src = urljoin(website_url.url, src)
|
| 150 |
+
width = safe_int(tag.get('width', styles.get('width', '200px')), 200)
|
| 151 |
+
height = safe_int(tag.get('height', styles.get('height', '200px')), 200)
|
| 152 |
+
element.update({
|
| 153 |
+
'type': 'image',
|
| 154 |
+
'src': src,
|
| 155 |
+
'alt': tag.get('alt', ''),
|
| 156 |
+
'width': max(width, 20),
|
| 157 |
+
'height': max(height, 20)
|
| 158 |
+
})
|
| 159 |
+
y_offset += height + 20
|
| 160 |
+
elements.append(element)
|
| 161 |
+
logger.info(f"Added image element: {src[:50]}")
|
| 162 |
+
|
| 163 |
+
elif tag.name == 'div':
|
| 164 |
+
width = safe_int(styles.get('width', '300px'), 300)
|
| 165 |
+
height = safe_int(styles.get('height', '100px'), 100)
|
| 166 |
+
element.update({
|
| 167 |
+
'type': 'div',
|
| 168 |
+
'width': max(width, 20),
|
| 169 |
+
'height': max(height, 20),
|
| 170 |
+
'backgroundColor': parse_color(styles.get('background-color', '#FFFFFF')),
|
| 171 |
+
'borderWidth': safe_int(styles.get('border-width', '0px'), 0),
|
| 172 |
+
'borderColor': parse_color(styles.get('border-color', '#000000'))
|
| 173 |
+
})
|
| 174 |
+
y_offset += height + 20
|
| 175 |
+
elements.append(element)
|
| 176 |
+
logger.info(f"Added div element: {width}x{height}")
|
| 177 |
+
|
| 178 |
+
# Process child elements
|
| 179 |
+
for child in tag.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button', 'li', 'strong', 'em', 'img', 'div'], recursive=False):
|
| 180 |
+
process_element(child, element['id'], depth + 1)
|
| 181 |
+
|
| 182 |
+
# Process body elements
|
| 183 |
+
body = soup.find('body')
|
| 184 |
+
if body:
|
| 185 |
+
for tag in body.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button', 'li', 'strong', 'em', 'img', 'div'], recursive=False):
|
| 186 |
+
process_element(tag)
|
| 187 |
+
else:
|
| 188 |
+
logger.warning("No <body> tag found, processing all elements")
|
| 189 |
+
for tag in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button', 'li', 'strong', 'em', 'img', 'div']):
|
| 190 |
+
process_element(tag)
|
| 191 |
+
|
| 192 |
+
# Extract color palette
|
| 193 |
+
colors = set()
|
| 194 |
+
for element in soup.find_all(style=True):
|
| 195 |
+
style = element.get('style')
|
| 196 |
+
if 'color' in style:
|
| 197 |
+
color_part = style.split('color:')[1].split(';')[0].strip()
|
| 198 |
+
colors.add(parse_color(color_part))
|
| 199 |
+
if 'background-color' in style:
|
| 200 |
+
bg_color = style.split('background-color:')[1].split(';')[0].strip()
|
| 201 |
+
colors.add(parse_color(bg_color))
|
| 202 |
+
|
| 203 |
+
# Extract basic layout
|
| 204 |
+
layout = {
|
| 205 |
+
'title': soup.title.string if soup.title else '',
|
| 206 |
+
'bodyWidth': '100%',
|
| 207 |
+
'bodyHeight': '100%'
|
| 208 |
}
|
| 209 |
+
|
| 210 |
+
if not elements:
|
| 211 |
+
logger.warning("No elements found, adding fallback")
|
| 212 |
+
elements.append({
|
| 213 |
+
'id': 'element-0',
|
| 214 |
+
'type': 'text',
|
| 215 |
+
'x': 20,
|
| 216 |
+
'y': 20,
|
| 217 |
+
'content': 'Unable to parse dynamic content. This site may rely on JavaScript.',
|
| 218 |
+
'fontSize': 16,
|
| 219 |
+
'color': '#FF0000',
|
| 220 |
+
'fontFamily': 'Inter',
|
| 221 |
+
'fontWeight': '400'
|
| 222 |
+
})
|
| 223 |
+
|
| 224 |
+
logger.info(f"Returning {len(elements)} elements")
|
| 225 |
+
return {
|
| 226 |
+
"elements": elements,
|
| 227 |
+
"colors": list(colors),
|
| 228 |
+
"layout": layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
+
|
| 231 |
+
except requests.RequestException as e:
|
| 232 |
+
logger.error(f"Error fetching website: {str(e)}")
|
| 233 |
+
raise HTTPException(status_code=400, detail=f"Error fetching website: {str(e)}")
|
| 234 |
+
except Exception as e:
|
| 235 |
+
logger.error(f"Error processing website: {str(e)}")
|
| 236 |
+
raise HTTPException(status_code=500, detail=f"Error processing website: {str(e)}")
|
| 237 |
+
|
| 238 |
+
@app.get("/")
|
| 239 |
+
async def root():
|
| 240 |
+
return {"message": "Web to Figma Converter API is running"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|