File size: 4,416 Bytes
1787bdb
 
 
 
 
 
 
 
 
 
 
7c0296c
1787bdb
10ea6ab
1787bdb
 
 
356ba6d
 
 
1787bdb
 
 
 
 
 
 
 
 
 
 
356ba6d
 
 
1787bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c0296c
1787bdb
 
 
 
 
 
 
 
 
 
7c0296c
1787bdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5337987
 
1787bdb
 
5c41f4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1787bdb
5c41f4e
10be315
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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 time
import gradio as gr
import subprocess

def driverSetup():
    options = Options()
    options.add_argument("--no-sandbox")  # Required for Linux container environments
    options.add_argument("--disable-dev-shm-usage")  # Required for Linux container environments
    options.add_argument("--disable-gpu")  # Optional, can be enabled if needed
    options.add_argument("start-maximized")
    options.add_argument("--headless=new")
    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="Linux",
            webgl_vendor="MesaLib",
            renderer="Mesa DRI Intel(R) HD Graphics 5500",
            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"{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()
    time.sleep(15)
    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",
    concurrency_limit=10
)

def installChrome(): 
    # Update package list
    subprocess.run(['apt-get', 'update'])
    
    # Install wget and unzip
    subprocess.run(['apt-get', 'install', '-y', 'wget', 'unzip'])
    
    # Download Google Chrome
    subprocess.run(['wget', 'https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb'])
    
    # Install Google Chrome
    subprocess.run(['apt-get', 'install', '-y', './google-chrome-stable_current_amd64.deb'])
    
    # Remove the downloaded package
    subprocess.run(['rm', 'google-chrome-stable_current_amd64.deb'])
    
    # Clean up
    subprocess.run(['apt-get', 'clean'])

if __name__ == "__main__":
    installChrome()
    print("App Starting...")
    app.launch()