Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from selenium import webdriver
|
| 2 |
+
from selenium.webdriver.chrome.service import Service
|
| 3 |
+
from selenium.webdriver.chrome.options import Options
|
| 4 |
+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
| 5 |
+
from selenium_stealth import stealth
|
| 6 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 7 |
+
import base64
|
| 8 |
+
import json
|
| 9 |
+
import requests
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
import os
|
| 12 |
+
import gradio as gr
|
| 13 |
+
|
| 14 |
+
def driverSetup():
|
| 15 |
+
options = Options()
|
| 16 |
+
options.add_argument("start-maximized")
|
| 17 |
+
options.add_argument("--headless=new")
|
| 18 |
+
options.add_argument("--disable-extensions")
|
| 19 |
+
options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
| 20 |
+
options.add_experimental_option('useAutomationExtension', False)
|
| 21 |
+
options.add_argument("--window-size=1366,768")
|
| 22 |
+
service = Service(ChromeDriverManager().install())
|
| 23 |
+
driver = webdriver.Chrome(service=service, options=options)
|
| 24 |
+
|
| 25 |
+
stealth(driver,
|
| 26 |
+
languages=["en-US", "en"],
|
| 27 |
+
vendor="Google Inc.",
|
| 28 |
+
platform="Win64",
|
| 29 |
+
webgl_vendor="Intel Inc.",
|
| 30 |
+
renderer="Intel Iris OpenGL Engine",
|
| 31 |
+
fix_hairline=True,
|
| 32 |
+
)
|
| 33 |
+
return driver
|
| 34 |
+
|
| 35 |
+
def screenshotName(url):
|
| 36 |
+
if url.startswith('https://'):
|
| 37 |
+
url = url.replace('https://', '')
|
| 38 |
+
elif url.startswith('http://'):
|
| 39 |
+
url = url.replace('http://', '')
|
| 40 |
+
elif url.startswith('http://www.'):
|
| 41 |
+
url = url.replace('http://www.', '')
|
| 42 |
+
elif url.startswith('https://www.'):
|
| 43 |
+
url = url.replace('https://www.', '')
|
| 44 |
+
else:
|
| 45 |
+
url = url
|
| 46 |
+
if url.endswith('/'):
|
| 47 |
+
url = url[:-1]
|
| 48 |
+
else:
|
| 49 |
+
url = url
|
| 50 |
+
ssname = f"SS_{url.replace('/', '')}.png"
|
| 51 |
+
return ssname
|
| 52 |
+
|
| 53 |
+
def getScreenshots(driver, url):
|
| 54 |
+
driver.get(url)
|
| 55 |
+
ssname = screenshotName(url)
|
| 56 |
+
driver.save_screenshot(ssname)
|
| 57 |
+
return ssname
|
| 58 |
+
|
| 59 |
+
def saveScreenshot(url):
|
| 60 |
+
driver = driverSetup()
|
| 61 |
+
screenshot_file = getScreenshots(driver, url)
|
| 62 |
+
driver.quit()
|
| 63 |
+
return screenshot_file
|
| 64 |
+
|
| 65 |
+
def uploadandDelSS(file):
|
| 66 |
+
load_dotenv()
|
| 67 |
+
api = os.getenv("IMGBBAPI")
|
| 68 |
+
url = os.getenv("IMGBBURL")
|
| 69 |
+
filename = file
|
| 70 |
+
with open(f"{filename}", "rb") as file:
|
| 71 |
+
payload = {
|
| 72 |
+
"key": api,
|
| 73 |
+
"image": base64.b64encode(file.read()),
|
| 74 |
+
"name": f"SS_{filename}",
|
| 75 |
+
"expiration": "15552000"
|
| 76 |
+
}
|
| 77 |
+
r = requests.post(url, data= payload)
|
| 78 |
+
view_url=(json.loads(r.text)["data"]["display_url"])
|
| 79 |
+
file.close()
|
| 80 |
+
os.remove(filename)
|
| 81 |
+
return view_url, view_url
|
| 82 |
+
|
| 83 |
+
def main(url):
|
| 84 |
+
ss = saveScreenshot(url)
|
| 85 |
+
img, imgurl = uploadandDelSS(ss)
|
| 86 |
+
return img, imgurl
|
| 87 |
+
|
| 88 |
+
app = gr.Interface(
|
| 89 |
+
fn=main,
|
| 90 |
+
inputs=[
|
| 91 |
+
gr.Textbox(label="Enter URL", placeholder="https://google.com", type="text", interactive=True)
|
| 92 |
+
],
|
| 93 |
+
outputs=[
|
| 94 |
+
gr.Image(label="Website Screenshot"),
|
| 95 |
+
gr.Textbox(label="Image URL", type="text", show_copy_button=True, interactive=False)
|
| 96 |
+
],
|
| 97 |
+
title="Website Screenshot Capture<br> by <a href='https://nayankasturi.eu.org'>Nayan Kasturi</a> aka Raanna.<br> Checkout the <a href='https://github.com/raannakasturi'>Github</a> for more projects and contact info.",
|
| 98 |
+
description="This app captures a screenshot of the website you enter and displays it.<br> Licenced under <a href='https://creativecommons.org/licenses/by-nc-sa/4.0/'>cc-by-nc-sa-4.0</a>",
|
| 99 |
+
api_name="capture",
|
| 100 |
+
concurrency_limit=10
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
app.launch()
|