File size: 3,448 Bytes
1787bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d23b8c7
1787bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ac9969
1787bdb
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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()