File size: 9,154 Bytes
550d985
 
 
33af16d
550d985
 
 
753ce1e
550d985
 
 
 
 
 
 
753ce1e
 
 
 
 
 
 
 
 
 
 
eb95849
753ce1e
eb95849
 
 
 
 
 
 
 
753ce1e
 
 
 
214efd8
753ce1e
 
33af16d
753ce1e
550d985
d156b44
550d985
 
 
 
 
753ce1e
550d985
 
1b38c3f
 
753ce1e
b2b8f0b
550d985
a85e152
753ce1e
 
 
 
e0d8633
753ce1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33af16d
550d985
a2b5996
550d985
 
 
753ce1e
550d985
753ce1e
550d985
44cf572
1b38c3f
 
 
 
 
44cf572
2dddd3e
 
1b38c3f
 
 
 
 
 
 
 
d156b44
1b38c3f
 
3c1d120
1b38c3f
8021f6e
 
 
43dd3fa
 
 
5758a89
af6cb01
 
5758a89
254e971
d53d169
5758a89
 
 
 
 
 
 
eb95849
5758a89
 
 
 
1d60cd7
 
eb95849
1d60cd7
 
eb95849
1d60cd7
 
 
 
 
 
1b38c3f
 
 
 
 
43dd3fa
1b38c3f
43dd3fa
3f3518f
 
 
 
 
 
 
 
 
 
 
 
1b38c3f
550d985
b2b8f0b
 
550d985
4e6a7d4
4a34a93
43dd3fa
3f3518f
 
43dd3fa
4e6a7d4
c809540
44cf572
 
 
a415e00
c809540
a85e152
f8f569b
4e6a7d4
753ce1e
 
 
 
 
3f3518f
8c7ea49
44cf572
43dd3fa
eb79942
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#from __future__ import annotations
from selenium import webdriver
import gradio as gr
import uuid
import re
from PIL import Image
from io import BytesIO
from pathlib import Path
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
#from selenium_webdriver import WebElement

driver_type = 'chromedriver'
driver=False

def get_concat_h_cut(in1, in2):
    im1=Image.open(in1)
    im2=Image.open(in2)
    dst = Image.new('RGB', (im1.width + im2.width,
                            min(im1.height, im2.height)))
    dst.paste(im1, (0, 0))
    dst.paste(im2, (im1.width, 0))
    return dst


def get_concat_v_cut(in1, in2, theme):
    im1=Image.open(in1)
    im2=Image.open(in2)
    if theme=='dark':
        color=(31,41,55)
    if theme=='light':
        color=(255,255,255)
    dst = Image.new('RGB', (min(im1.width, im2.width), im1.height + im2.height+30),color=color)
    dst.paste(im1, (0, 10))
    dst.paste(im2, (0, im1.height+20))
    return dst



def run_script1(url: str, height: int, width: int, check_b,check_h):
    mes_box=[]
    out_box=[]
    uid=uuid.uuid4()
    out=None
    is_url=True
    url='https://omnibus-snap.hf.space/?chat=[(123,555),(2342,32344)]&__theme=dark'
    if is_url:
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        mes='<center>operation success'
        try:
            driver = webdriver.Chrome(options=options)
            #driver.current_window_handle
            driver.get(url)
            html=driver.page_source
            #print(html)            
            driver.implicitly_wait(30)
            driver.set_window_size(int(width), int(height))
            #page = driver.find_element(By.TAG_NAME, "html")
            #driver.execute_script("arguments[0].style.background = 'blue';",page)
            main_head = driver.find_element(By.CLASS_NAME, "main-content-header")
            head_shot = main_head.screenshot(f'head-{uid}-tmp.png')
            obj = driver.find_element(By.CLASS_NAME, "main")            
            messages = driver.find_elements(By.CLASS_NAME, "message")
            for i,ea in enumerate(check_b):
                try:    
                    ea = int(ea)
                    ea = (ea*2)-2
                    print (ea)
                    messages[ea].screenshot(f'{i}-{uid}-tmp.png')
                    messages[ea+1].screenshot(f'{i+1}-{uid}-tmp.png')
                    im_roll = get_concat_v_cut(f'{i}-{uid}-tmp.png',f'{i+1}-{uid}-tmp.png')
                    im_roll.save(f'comb{i}-{uid}-tmp.png')
                    out_box.append(f'comb{i}-{uid}-tmp.png')
                except Exception:
                    mes="<center>Some blocks returned an error"
            if out_box:
                if len(out_box)>1:
                    im_roll = get_concat_v_cut(f'{out_box[0]}',f'{out_box[1]}')
                    im_roll.save(f'comb-{uid}-tmp.png')                
                    for i in range(2,len(out_box)):
                        im_roll = get_concat_v_cut(f'comb-{uid}-tmp.png',f'{out_box[i]}')
                        im_roll.save(f'comb-{uid}-tmp.png')
                    out = f'comb-{uid}-tmp.png'
                else:
                    tmp_im = Image.open(out_box[0])
                    tmp_im.save(f'comb-{uid}-tmp.png')
                    out = f'comb-{uid}-tmp.png'
                if check_h:
                    head_paste = get_concat_v_cut(f'head-{uid}-tmp.png',f'comb-{uid}-tmp.png')
                    head_paste.save(f'comb-{uid}-tmp.png')
                    out = f'comb-{uid}-tmp.png'
            screenshot = obj.screenshot(f'{uid}-tmp.png')
        except WebDriverException as e:
            return Image.new('RGB', (1, 1)), f'<center>Please enter a valid URL of a website/host.',out_box,out
        finally:
            if driver:
                driver.quit()
        return Image.open(f'{uid}-tmp.png'), mes,out_box,out
    else:
        return None, '<center>Please enter a valid URL of a website/host.',out_box,out

def run_script(chat: list, height: int, width: int, check_b,check_h,check_t,wait_time):
    mes_box=[]
    out_box=[]
    uid=uuid.uuid4()
    out=None
    is_url=True
    url=f'https://omnibus-snap.hf.space/?chat={chat}&__theme={check_t}'
    if wait_time>10000:
        wait_time=10000
    if is_url:
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        mes='<center>operation success'
        try:
            driver = webdriver.Chrome(options=options)
            driver.get(url)
            html=driver.page_source
            print(html)            
            driver.implicitly_wait(int(wait_time*30))
            driver.set_window_size(int(width), int(height))
            obj = driver.find_element(By.ID, "component-1")            

            screenshot = obj.screenshot(f'{uid}-tmp.png')

            yield Image.open(f'{uid}-tmp.png'), '<center>Finished loading. If loading is incomplete, restart with longer wait time',out_box,out

            
            #main_head = driver.find_element(By.CLASS_NAME, "main-content-header")
            #head_shot = main_head.screenshot(f'head-{uid}-tmp.png')
            obj = driver.find_element(By.CLASS_NAME, "main")            
            #messages = driver.find_elements(By.CLASS_NAME, "panel")
            messages = driver.find_elements(By.CLASS_NAME, "message-row")
            for i,ea in enumerate(check_b):
                try:    
                    ea = int(ea)
                    ea = (ea*2)-2
                    print (ea)
                    messages[ea].screenshot(f'{i}-{uid}-tmp.png')
                    messages[ea+1].screenshot(f'{i+1}-{uid}-tmp.png')
                    im_roll = get_concat_v_cut(f'{i}-{uid}-tmp.png',f'{i+1}-{uid}-tmp.png',check_t)
                    im_roll.save(f'comb{i}-{uid}-tmp.png')
                    out_box.append(f'comb{i}-{uid}-tmp.png')
                except Exception:
                    mes="<center>Some blocks returned an error"
            if out_box:
                if len(out_box)>1:
                    im_roll = get_concat_v_cut(f'{out_box[0]}',f'{out_box[1]}',check_t)
                    im_roll.save(f'comb-{uid}-tmp.png')                
                    for i in range(2,len(out_box)):
                        im_roll = get_concat_v_cut(f'comb-{uid}-tmp.png',f'{out_box[i]}',check_t)
                        im_roll.save(f'comb-{uid}-tmp.png')
                    out = f'comb-{uid}-tmp.png'
                else:
                    tmp_im = Image.open(out_box[0])
                    tmp_im.save(f'comb-{uid}-tmp.png')
                    out = f'comb-{uid}-tmp.png'            
        except WebDriverException as e:
            return Image.new('RGB', (1, 1)), f'<center>Please enter a valid URL of a website/host.',out_box,out
        finally:
            if driver:
                driver.quit()
        yield Image.open(f'{uid}-tmp.png'), mes,out_box,out
    else:
        yield None, '<center>Please enter a valid URL of a website/host.',out_box,out
def load_frame(inp):
        html_mod=f"""
        <div id="my_game" style="height:600px;width:100%">
            <iframe
            id="myIframe"
            src="https://omnibus-snap.hf.space/?chat=[(123,555),(2342,32344)]&__theme=dark"        
        	frameborder="0"
        	width="100%"
            height="100%"
            ></iframe>
        </div>"""
        return html_mod

with gr.Blocks() as app:
    gr.HTML("""<head><meta http-equiv="refresh" content="180"></head>""")

    with gr.Row():
        with gr.Column():
            inp = gr.Textbox(label="Chat: [('user','bot'),('user','bot')]",lines=6,value=[('123','555'),('2342','32344')])
            wait_t=gr.Slider(1, 10000, value=2000, label="Wait time", info="Wait for page to load. More data = Longer wait")
            with gr.Row():
                btn= gr.Button("Snapshot")
                stop_btn=gr.Button("Stop")
            
        with gr.Column():
            with gr.Row():
                check_t=gr.Radio(label="Theme", choices=["light","dark"], value="dark")
                check_h=gr.Checkbox(label="Show Header", value=True)
            check_b=gr.CheckboxGroup(label="Chatblocks", choices=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], value=[1])
            with gr.Row():
                height=gr.Number(label="Height", value=4096)
                width=gr.Number(label="Width",value=800)
    message=gr.HTML('<center>Enter URL')
    with gr.Row():
        with gr.Column():
            out=gr.Image()
            outgal=gr.Gallery()
        outim = gr.Image()
    prev=gr.HTML()
    #load_btn.click(load_frame,inp,prev)
    go = btn.click(run_script,[inp,height,width,check_b,check_h,check_t,wait_t],[outim,message,outgal,out])
    stop_btn.click(None,None,None,cancels=go)
app.queue(default_concurrency_limit=10).launch()