Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from urllib.parse import urlparse, urljoin
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
import subprocess
|
7 |
-
from playwright.async_api import async_playwright
|
8 |
|
9 |
# HTML and JavaScript for the "Copy Code" button
|
10 |
copy_button_html = """
|
@@ -208,35 +207,6 @@ def check_code_for_errors(code):
|
|
208 |
except Exception as e:
|
209 |
return f"Error: {e}", code
|
210 |
|
211 |
-
# Perchance.org to Code Converter
|
212 |
-
async def fetch_perchance_code(perchance_url):
|
213 |
-
"""Fetches the code from a Perchance.org generator using Playwright."""
|
214 |
-
try:
|
215 |
-
# Проверка, что URL принадлежит Perchance.org
|
216 |
-
if not perchance_url.startswith("https://perchance.org/"):
|
217 |
-
return "Error: Please enter a valid Perchance.org URL."
|
218 |
-
|
219 |
-
async with async_playwright() as p:
|
220 |
-
# Запуск браузера
|
221 |
-
browser = await p.chromium.launch(headless=True)
|
222 |
-
page = await browser.new_page()
|
223 |
-
|
224 |
-
# Переход на страницу
|
225 |
-
await page.goto(perchance_url, wait_until="networkidle")
|
226 |
-
|
227 |
-
# Ожидание загрузки текстового поля с кодом
|
228 |
-
await page.wait_for_selector("textarea#editBox", state="visible")
|
229 |
-
|
230 |
-
# Извлечение кода
|
231 |
-
code = await page.evaluate('document.querySelector("textarea#editBox").value')
|
232 |
-
|
233 |
-
# Закрытие браузера
|
234 |
-
await browser.close()
|
235 |
-
|
236 |
-
return code if code else "Error: Could not find the code in the generator."
|
237 |
-
except Exception as e:
|
238 |
-
return f"Error: {e}"
|
239 |
-
|
240 |
# Create the Gradio interface
|
241 |
with gr.Blocks() as demo:
|
242 |
gr.HTML(copy_button_html) # Add the "Copy Code" script
|
@@ -364,26 +334,5 @@ with gr.Blocks() as demo:
|
|
364 |
outputs=[space_content_output]
|
365 |
)
|
366 |
|
367 |
-
# Tab 4: Perchance.org to Code Converter
|
368 |
-
with gr.Tab("Perchance.org to Code Converter"):
|
369 |
-
gr.Markdown("## Perchance.org to Code Converter")
|
370 |
-
gr.Markdown("Enter a link to a Perchance.org generator to extract its code.")
|
371 |
-
|
372 |
-
with gr.Row():
|
373 |
-
perchance_url_input = gr.Textbox(label="Perchance.org URL", placeholder="https://perchance.org/[name]#edit")
|
374 |
-
|
375 |
-
with gr.Row():
|
376 |
-
perchance_code_output = gr.Textbox(label="Generator Code", interactive=True, elem_id="perchance-code-output")
|
377 |
-
|
378 |
-
with gr.Row():
|
379 |
-
gr.HTML("<button onclick='copyCode(\"perchance-code-output\")'>Copy Code</button>") # Add the "Copy Code" button
|
380 |
-
|
381 |
-
submit_perchance_button = gr.Button("Fetch Code")
|
382 |
-
submit_perchance_button.click(
|
383 |
-
fn=fetch_perchance_code,
|
384 |
-
inputs=perchance_url_input,
|
385 |
-
outputs=perchance_code_output
|
386 |
-
)
|
387 |
-
|
388 |
# Launch the interface
|
389 |
demo.launch()
|
|
|
4 |
from bs4 import BeautifulSoup
|
5 |
import asyncio
|
6 |
import subprocess
|
|
|
7 |
|
8 |
# HTML and JavaScript for the "Copy Code" button
|
9 |
copy_button_html = """
|
|
|
207 |
except Exception as e:
|
208 |
return f"Error: {e}", code
|
209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
# Create the Gradio interface
|
211 |
with gr.Blocks() as demo:
|
212 |
gr.HTML(copy_button_html) # Add the "Copy Code" script
|
|
|
334 |
outputs=[space_content_output]
|
335 |
)
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
# Launch the interface
|
338 |
demo.launch()
|