Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,33 +3,68 @@ from selenium import webdriver
|
|
| 3 |
from selenium.common.exceptions import WebDriverException
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def take_screenshot(url):
|
| 8 |
options = webdriver.ChromeOptions()
|
| 9 |
options.add_argument('--headless')
|
| 10 |
options.add_argument('--no-sandbox')
|
| 11 |
options.add_argument('--disable-dev-shm-usage')
|
| 12 |
-
|
|
|
|
| 13 |
try:
|
| 14 |
wd = webdriver.Chrome(options=options)
|
| 15 |
-
wd.
|
| 16 |
wd.get(url)
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except WebDriverException as e:
|
| 20 |
return Image.new('RGB', (1, 1))
|
|
|
|
| 21 |
finally:
|
| 22 |
if wd:
|
| 23 |
wd.quit()
|
| 24 |
|
| 25 |
-
return Image.open(BytesIO(screenshot))
|
| 26 |
-
|
| 27 |
iface = gr.Interface(
|
| 28 |
fn=take_screenshot,
|
| 29 |
-
inputs=
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
title="Website Screenshot",
|
| 32 |
description="Take a screenshot of a website using selenium in a gradio space!"
|
| 33 |
)
|
| 34 |
|
| 35 |
-
iface.launch()
|
|
|
|
| 3 |
from selenium.common.exceptions import WebDriverException
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
def take_screenshot(url, device):
|
| 9 |
+
|
| 10 |
+
presets = {
|
| 11 |
+
"Mobile (375x667)": (375, 667),
|
| 12 |
+
"Tablet (768x1024)": (768, 1024),
|
| 13 |
+
"PC (1366x768)": (1366, 768),
|
| 14 |
+
"Full Screenshot": (1920, 1080)
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
width, height = presets.get(device, (1366, 768))
|
| 18 |
|
|
|
|
| 19 |
options = webdriver.ChromeOptions()
|
| 20 |
options.add_argument('--headless')
|
| 21 |
options.add_argument('--no-sandbox')
|
| 22 |
options.add_argument('--disable-dev-shm-usage')
|
| 23 |
+
|
| 24 |
+
wd = None
|
| 25 |
try:
|
| 26 |
wd = webdriver.Chrome(options=options)
|
| 27 |
+
wd.implicitly_wait(15)
|
| 28 |
wd.get(url)
|
| 29 |
+
|
| 30 |
+
if device == "Full Screenshot":
|
| 31 |
+
|
| 32 |
+
required_width = wd.execute_script("return document.body.parentNode.scrollWidth")
|
| 33 |
+
required_height = wd.execute_script("return document.body.parentNode.scrollHeight")
|
| 34 |
+
|
| 35 |
+
max_height = 10000
|
| 36 |
+
|
| 37 |
+
wd.set_window_size(width, min(required_height, max_height))
|
| 38 |
+
time.sleep(1)
|
| 39 |
+
screenshot = wd.get_screenshot_as_png()
|
| 40 |
+
|
| 41 |
+
else:
|
| 42 |
+
wd.set_window_size(width, height)
|
| 43 |
+
time.sleep(1)
|
| 44 |
+
screenshot = wd.get_screenshot_as_png()
|
| 45 |
+
|
| 46 |
+
return Image.open(BytesIO(screenshot))
|
| 47 |
+
|
| 48 |
except WebDriverException as e:
|
| 49 |
return Image.new('RGB', (1, 1))
|
| 50 |
+
|
| 51 |
finally:
|
| 52 |
if wd:
|
| 53 |
wd.quit()
|
| 54 |
|
|
|
|
|
|
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=take_screenshot,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Textbox(label="Website URL", value="https://asitha.top/"),
|
| 59 |
+
gr.Dropdown(
|
| 60 |
+
choices=["Mobile (375x667)", "Tablet (768x1024)", "PC (1366x768)", "Full Screenshot"],
|
| 61 |
+
label="Device",
|
| 62 |
+
value="PC (1366x768)"
|
| 63 |
+
)
|
| 64 |
+
],
|
| 65 |
+
outputs=gr.Image(type="pil", label="Screenshot", height=360, width=540),
|
| 66 |
title="Website Screenshot",
|
| 67 |
description="Take a screenshot of a website using selenium in a gradio space!"
|
| 68 |
)
|
| 69 |
|
| 70 |
+
iface.launch()
|