PrarthiKothari's picture
Update app.py
d23b8c7 verified
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium_stealth import stealth
from webdriver_manager.chrome import ChromeDriverManager
import base64
import json
import requests
from dotenv import load_dotenv
import os
import gradio as gr
def driverSetup():
options = Options()
options.add_argument("start-maximized")
options.add_argument("--headless=new")
options.add_argument("--disable-extensions")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--window-size=1366,768")
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="linux64",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
return driver
def screenshotName(url):
if url.startswith('https://'):
url = url.replace('https://', '')
elif url.startswith('http://'):
url = url.replace('http://', '')
elif url.startswith('http://www.'):
url = url.replace('http://www.', '')
elif url.startswith('https://www.'):
url = url.replace('https://www.', '')
else:
url = url
if url.endswith('/'):
url = url[:-1]
else:
url = url
ssname = f"SS_{url.replace('/', '')}.png"
return ssname
def getScreenshots(driver, url):
driver.get(url)
ssname = screenshotName(url)
driver.save_screenshot(ssname)
return ssname
def saveScreenshot(url):
driver = driverSetup()
screenshot_file = getScreenshots(driver, url)
driver.quit()
return screenshot_file
def uploadandDelSS(file):
load_dotenv()
api = os.getenv("IMGBBAPI")
url = os.getenv("IMGBBURL")
filename = file
with open(f"{filename}", "rb") as file:
payload = {
"key": api,
"image": base64.b64encode(file.read()),
"name": f"SS_{filename}",
"expiration": "15552000"
}
r = requests.post(url, data= payload)
view_url=(json.loads(r.text)["data"]["display_url"])
file.close()
os.remove(filename)
return view_url, view_url
def main(url):
ss = saveScreenshot(url)
img, imgurl = uploadandDelSS(ss)
return img, imgurl
app = gr.Interface(
fn=main,
inputs=[
gr.Textbox(label="Enter URL", placeholder="https://google.com", type="text", interactive=True)
],
outputs=[
gr.Image(label="Website Screenshot"),
gr.Textbox(label="Image URL", type="text", show_copy_button=True, interactive=False)
],
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.",
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>",
api_name="capture"
)
if __name__ == "__main__":
app.launch()