AkashKumarave commited on
Commit
0da4396
·
verified ·
1 Parent(s): d6b87fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +236 -226
app.py CHANGED
@@ -1,230 +1,240 @@
1
- figma.showUI(__html__, { width: 340, height: 320 });
2
-
3
- figma.ui.onmessage = async (msg) => {
4
- if (msg.type === 'convert-website') {
5
- try {
6
- const apiUrl = "https://akashkumarave-dp.hf.space/extract";
7
- console.log('Sending request to:', apiUrl, 'with URL:', msg.url);
8
-
9
- const response = await fetch(apiUrl, {
10
- method: 'POST',
11
- headers: { 'Content-Type': 'application/json' },
12
- body: JSON.stringify({ url: msg.url })
13
- });
14
-
15
- if (!response.ok) {
16
- const errorText = await response.text();
17
- console.log('Error response:', errorText);
18
- throw new Error(`Server responded with ${response.status}: ${errorText}`);
19
- }
20
-
21
- const data = await response.json();
22
- console.log('Received data:', data);
23
-
24
- if (!data.elements || data.elements.length === 0) {
25
- figma.ui.postMessage({
26
- type: 'conversion-error',
27
- error: 'No elements found. The site may rely on JavaScript.'
28
- });
29
- return;
30
- }
31
-
32
- const page = figma.createPage();
33
- page.name = `Web Import: ${new URL(msg.url).hostname}`;
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
- if (element.type === 'text') {
52
- try {
53
- const textNode = figma.createText();
54
- const fontFamily = element.fontFamily || 'Inter';
55
- const fontStyle = element.fontWeight === '700' ? 'Bold' : 'Regular';
56
- await figma.loadFontAsync({ family: fontFamily, style: fontStyle }).catch(() =>
57
- figma.loadFontAsync({ family: 'Inter', style: fontStyle })
58
- );
59
- textNode.fontName = { family: fontFamily, style: fontStyle };
60
- textNode.characters = element.content || 'Text Placeholder';
61
- textNode.fontSize = element.fontSize || 16;
62
- textNode.x = element.x || 100;
63
- textNode.y = element.y || 100;
64
- textNode.fills = [{ type: 'SOLID', color: hexToRgb(element.color || '#000000') }];
65
-
66
- switch (element.tag) {
67
- case 'h1': textNode.fontSize = 32; break;
68
- case 'h2': textNode.fontSize = 28; break;
69
- case 'h3': textNode.fontSize = 24; break;
70
- case 'h4': textNode.fontSize = 20; break;
71
- case 'h5': textNode.fontSize = 18; break;
72
- case 'h6': textNode.fontSize = 16; break;
73
- case 'a': textNode.fills = [{ type: 'SOLID', color: { r: 0, g: 0.439, b: 0.866 } }]; break;
74
- case 'button':
75
- const buttonRect = figma.createRectangle();
76
- buttonRect.resize(textNode.width + 40, textNode.height + 20);
77
- buttonRect.x = textNode.x;
78
- buttonRect.y = textNode.y;
79
- buttonRect.cornerRadius = 8;
80
- buttonRect.fills = [{ type: 'SOLID', color: { r: 0.094, g: 0.627, b: 0.984 } }];
81
- textNode.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
82
- textNode.x += 20;
83
- textNode.y += 10;
84
- parentNode.appendChild(buttonRect);
85
- break;
86
- }
87
-
88
- parentNode.appendChild(textNode);
89
- nodeMap.set(element.id, textNode);
90
- console.log('Added text:', element.content);
91
- } catch (error) {
92
- console.error('Error creating text:', error);
93
- }
94
- } else if (element.type === 'image') {
95
- try {
96
- const rect = figma.createRectangle();
97
- rect.name = `Image: ${element.alt || 'Untitled'}`;
98
- rect.resize(element.width || 300, element.height || 200);
99
- rect.x = element.x || 100;
100
- rect.y = element.y || 100;
101
-
102
- if (element.src) {
103
- try {
104
- const imageData = await fetchImage(element.src);
105
- const figmaImage = figma.createImage(imageData);
106
- rect.fills = [{ type: 'IMAGE', imageHash: figmaImage.hash, scaleMode: 'FILL' }];
107
- } catch (e) {
108
- console.error('Failed to load image:', element.src, e);
109
- rect.fills = [{ type: 'SOLID', color: { r: 0.9, g: 0.9, b: 0.9 } }];
110
- const label = figma.createText();
111
- await figma.loadFontAsync({ family: 'Inter', style: 'Regular' });
112
- label.characters = `[Image: ${element.alt || element.src.split('/').pop()}]`;
113
- label.fontSize = 12;
114
- label.x = rect.x + rect.width / 2 - label.width / 2;
115
- label.y = rect.y + rect.height / 2 - label.height / 2;
116
- parentNode.appendChild(label);
117
- }
118
- }
119
-
120
- parentNode.appendChild(rect);
121
- nodeMap.set(element.id, rect);
122
- console.log('Added image:', element.src);
123
- } catch (error) {
124
- console.error('Error creating image:', error);
125
- }
126
- } else if (element.type === 'div') {
127
- try {
128
- const group = figma.createFrame();
129
- group.name = 'Div';
130
- group.resize(element.width || 300, element.height || 100);
131
- group.x = element.x || 100;
132
- group.y = element.y || 100;
133
- group.fills = [{ type: 'SOLID', color: hexToRgb(element.backgroundColor || '#FFFFFF') }];
134
- if (element.borderWidth > 0) {
135
- group.strokes = [{ type: 'SOLID', color: hexToRgb(element.borderColor || '#000000') }];
136
- group.strokeWeight = element.borderWidth;
 
 
 
 
137
  }
138
- parentNode.appendChild(group);
139
- nodeMap.set(element.id, group);
140
- console.log('Added div:', element.width, element.height);
141
- } catch (error) {
142
- console.error('Error creating div:', error);
143
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  }
145
- }
146
-
147
- if (data.colors && data.colors.length > 0) {
148
- const colorSection = figma.createFrame();
149
- colorSection.name = "Color Palette";
150
- colorSection.resize(300, 50 + data.colors.length * 50);
151
- colorSection.x = frame.width - 400;
152
- colorSection.y = 100;
153
-
154
- const colorTitle = figma.createText();
155
- await figma.loadFontAsync({ family: "Inter", style: "Medium" });
156
- colorTitle.fontName = { family: "Inter", style: "Medium" };
157
- colorTitle.characters = "Color Palette";
158
- colorTitle.fontSize = 16;
159
- colorTitle.x = 20;
160
- colorTitle.y = 20;
161
- colorSection.appendChild(colorTitle);
162
-
163
- let colorY = 60;
164
- for (const colorStr of data.colors) {
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
- frame.appendChild(colorSection);
189
- }
190
-
191
- const bounds = frame.children.reduce(
192
- (acc, node) => ({
193
- width: Math.max(acc.width, node.x + node.width),
194
- height: Math.max(acc.height, node.y + node.height)
195
- }),
196
- { width: 1440, height: 900 }
197
- );
198
- frame.resize(bounds.width + 20, bounds.height + 20);
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"}