-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
465 lines (389 loc) · 17 KB
/
web.py
File metadata and controls
465 lines (389 loc) · 17 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import sys
import os
import threading
import time
import http.server
import socketserver
from datetime import datetime
from PyQt5.QtCore import QUrl, Qt, QTimer, QSize
from PyQt5.QtWidgets import (QApplication, QMainWindow, QToolBar,
QLineEdit, QPushButton, QAction, QVBoxLayout,
QHBoxLayout, QWidget, QTabWidget, QMenu, QStatusBar,
QMessageBox)
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtGui import QIcon, QKeySequence, QPixmap, QImage
class WebBrowser(QMainWindow):
def __init__(self):
super().__init__()
self.initialize_ui()
def initialize_ui(self):
# Set window properties
self.setWindowTitle("Python Web Browser")
self.setGeometry(100, 100, 1024, 768)
# Create screenshot directory if it doesn't exist
self.screenshot_dir = os.path.join(os.getcwd(), "screenshots")
if not os.path.exists(self.screenshot_dir):
os.makedirs(self.screenshot_dir)
# Initialize HTTP server for serving screenshots
self.server_port = 8000
self.start_http_server()
# Set up auto-screenshot timer
self.auto_screenshot_enabled = True
self.screenshot_interval = 100 # milliseconds
self.auto_screenshot_timer = QTimer(self)
self.auto_screenshot_timer.timeout.connect(self.take_auto_screenshot)
self.auto_screenshot_timer.start(self.screenshot_interval)
# Track latest screenshot for auto-updates
self.latest_screenshot = None
# Create tab widget to support multiple tabs
self.tabs = QTabWidget()
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.close_tab)
# Create actions
self.create_actions()
# Create toolbar
self.create_toolbar()
# Create status bar
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
# Create first tab
self.add_new_tab()
# Set central widget
self.setCentralWidget(self.tabs)
self.show()
def create_actions(self):
# Back action
self.back_action = QAction("Back", self)
self.back_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_Left))
self.back_action.triggered.connect(self.navigate_back)
# Forward action
self.forward_action = QAction("Forward", self)
self.forward_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_Right))
self.forward_action.triggered.connect(self.navigate_forward)
# Reload action
self.reload_action = QAction("Reload", self)
self.reload_action.setShortcut(QKeySequence(Qt.Key_F5))
self.reload_action.triggered.connect(self.reload_page)
# Home action
self.home_action = QAction("Home", self)
self.home_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_H))
self.home_action.triggered.connect(self.navigate_home)
# New Tab action
self.new_tab_action = QAction("New Tab", self)
self.new_tab_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_T))
self.new_tab_action.triggered.connect(self.add_new_tab)
# Screenshot action
self.screenshot_action = QAction("Take Screenshot", self)
self.screenshot_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_S))
self.screenshot_action.triggered.connect(self.take_screenshot)
# Toggle Auto-Screenshot action
self.auto_screenshot_action = QAction("Toggle Auto-Screenshot", self)
self.auto_screenshot_action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_A))
self.auto_screenshot_action.triggered.connect(self.toggle_auto_screenshot)
self.auto_screenshot_action.setCheckable(True)
self.auto_screenshot_action.setChecked(True)
def create_toolbar(self):
navigation_bar = QToolBar("Navigation")
self.addToolBar(navigation_bar)
# Add actions to toolbar
navigation_bar.addAction(self.back_action)
navigation_bar.addAction(self.forward_action)
navigation_bar.addAction(self.reload_action)
navigation_bar.addAction(self.home_action)
navigation_bar.addAction(self.new_tab_action)
navigation_bar.addAction(self.screenshot_action)
navigation_bar.addAction(self.auto_screenshot_action)
# Add URL bar
self.url_bar = QLineEdit()
self.url_bar.returnPressed.connect(self.navigate_to_url)
navigation_bar.addWidget(self.url_bar)
# Add Go button
go_button = QPushButton("Go")
go_button.clicked.connect(self.navigate_to_url)
navigation_bar.addWidget(go_button)
def add_new_tab(self, url=None):
# Create browser frame
browser = QWebEngineView()
browser.page().loadProgress.connect(self.update_loading_progress)
browser.page().loadFinished.connect(self.update_url)
browser.page().titleChanged.connect(self.update_title)
# Create tab layout
layout = QVBoxLayout()
layout.addWidget(browser)
layout.setContentsMargins(0, 0, 0, 0)
# Create tab widget
tab = QWidget()
tab.setLayout(layout)
# Add tab to tab widget
index = self.tabs.addTab(tab, "New Tab")
self.tabs.setCurrentIndex(index)
# Load URL if provided
if url:
browser.load(QUrl(url))
else:
browser.load(QUrl("https://www.google.com"))
def close_tab(self, index):
if self.tabs.count() > 1:
self.tabs.removeTab(index)
else:
# Don't close the last tab, just clear it
current_browser = self.get_current_browser()
current_browser.load(QUrl("https://www.google.com"))
def get_current_browser(self):
current_tab = self.tabs.currentWidget()
layout = current_tab.layout()
return layout.itemAt(0).widget()
def navigate_to_url(self):
url = self.url_bar.text()
# Add http:// if no protocol specified
if not url.startswith(("http://", "https://")):
url = "http://" + url
current_browser = self.get_current_browser()
current_browser.load(QUrl(url))
def navigate_back(self):
current_browser = self.get_current_browser()
current_browser.back()
def navigate_forward(self):
current_browser = self.get_current_browser()
current_browser.forward()
def reload_page(self):
current_browser = self.get_current_browser()
current_browser.reload()
def navigate_home(self):
current_browser = self.get_current_browser()
current_browser.load(QUrl("https://www.google.com"))
def update_url(self):
current_browser = self.get_current_browser()
self.url_bar.setText(current_browser.url().toString())
def update_title(self, title):
index = self.tabs.currentIndex()
if title:
self.tabs.setTabText(index, title[:15] + "..." if len(title) > 15 else title)
def update_loading_progress(self, progress):
self.status_bar.showMessage(f"Loading: {progress}%")
if progress == 100:
self.status_bar.showMessage("Done", 2000) # Show "Done" for 2 seconds
def take_screenshot(self):
# Get current browser widget
current_browser = self.get_current_browser()
# Create a unique filename based on timestamp and URL
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
url_part = current_browser.url().host().replace(".", "_")
if not url_part:
url_part = "homepage"
filename = f"screenshot_{url_part}_{timestamp}.png"
filepath = os.path.join(self.screenshot_dir, filename)
# Take screenshot after a short delay to ensure page is fully rendered
QTimer.singleShot(500, lambda: self._capture_screenshot(filepath))
def _capture_screenshot(self, filepath):
# Capture the current tab
current_tab = self.tabs.currentWidget()
pixmap = current_tab.grab()
# Save the screenshot
pixmap.save(filepath)
# Update status bar
self.status_bar.showMessage(f"Screenshot saved: {filepath}", 3000)
# Update the index.html file
self.update_screenshot_index()
# Display notification with URL
QMessageBox.information(
self,
"Screenshot Captured",
f"Screenshot saved and available at:\nhttp://localhost:{self.server_port}/{os.path.basename(filepath)}"
)
def update_screenshot_index(self):
# Create an index.html file that lists all screenshots
screenshots = [f for f in os.listdir(self.screenshot_dir) if f.endswith('.png') and f != "live_view.png"]
screenshots.sort(reverse=True) # Most recent first
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Browser Screenshots</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #333; }
.live-view-link {
display: block;
margin: 20px 0;
padding: 10px;
background-color: #4CAF50;
color: white;
text-align: center;
text-decoration: none;
font-weight: bold;
border-radius: 5px;
}
.live-view-link:hover {
background-color: #45a049;
}
.screenshot {
margin-bottom: 30px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
.screenshot img {
max-width: 100%;
border: 1px solid #eee;
}
.timestamp {
color: #666;
font-size: 0.8em;
margin-bottom: 5px;
}
</style>
</head>
<body>
<h1>Browser Screenshots</h1>
<a href="live_view.html" class="live-view-link">View Live Stream (Auto-updating every 100ms)</a>
"""
for screenshot in screenshots:
# Skip the live view screenshot
if screenshot == "live_view.png":
continue
# Extract timestamp from filename
timestamp_str = screenshot.split('_')[-1].split('.')[0]
date_str = screenshot.split('_')[-2]
formatted_time = f"{date_str[:4]}-{date_str[4:6]}-{date_str[6:]} {timestamp_str[:2]}:{timestamp_str[2:4]}:{timestamp_str[4:]}"
html_content += f"""
<div class="screenshot">
<div class="timestamp">Captured: {formatted_time}</div>
<img src="{screenshot}" alt="Screenshot {screenshot}">
</div>
"""
html_content += """
</body>
</html>
"""
# Write to index.html
with open(os.path.join(self.screenshot_dir, "index.html"), "w") as f:
f.write(html_content)
def start_http_server(self):
# Create a custom HTTP request handler
class ScreenshotHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=self.server_directory, **kwargs)
def log_message(self, format, *args):
# Silence server logs
pass
def end_headers(self):
# Add headers to prevent caching for live_view.png
path = self.path.split('?')[0] # Remove query parameters
if path.endswith('/live_view.png'):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
super().end_headers()
# Set the directory attribute for the handler class
ScreenshotHandler.server_directory = self.screenshot_dir
# Create initial live_view.html file
self.update_live_view_page()
# Start server in a separate thread
def run_server():
with socketserver.TCPServer(("", self.server_port), ScreenshotHandler) as httpd:
print(f"Serving screenshots at http://localhost:{self.server_port}")
print(f"Live view available at http://localhost:{self.server_port}/live_view.html")
httpd.serve_forever()
server_thread = threading.Thread(target=run_server, daemon=True)
server_thread.start()
# Wait a bit for the server to start
time.sleep(0.5)
def take_auto_screenshot(self):
"""Take automatic screenshots for live view without saving files"""
if not self.auto_screenshot_enabled:
return
# Capture current tab
current_tab = self.tabs.currentWidget()
pixmap = current_tab.grab()
# Create a unique filename for the live view
live_filename = "live_view.png"
filepath = os.path.join(self.screenshot_dir, live_filename)
# Save the screenshot (overwriting previous live view)
pixmap.save(filepath)
# Update the live view page
self.update_live_view_page()
# Store as latest screenshot
self.latest_screenshot = filepath
def toggle_auto_screenshot(self):
"""Toggle automatic screenshot functionality"""
self.auto_screenshot_enabled = not self.auto_screenshot_enabled
if self.auto_screenshot_enabled:
self.auto_screenshot_timer.start(self.screenshot_interval)
self.status_bar.showMessage("Auto-screenshots enabled", 2000)
else:
self.auto_screenshot_timer.stop()
self.status_bar.showMessage("Auto-screenshots disabled", 2000)
# Update the action's checked state
self.auto_screenshot_action.setChecked(self.auto_screenshot_enabled)
def update_live_view_page(self):
"""Update the live view HTML page"""
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Browser Live View</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
text-align: center;
}
h1 {
color: #333;
padding: 20px;
margin: 0;
background-color: #e0e0e0;
}
.live-view {
margin: 20px auto;
max-width: 95%;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.live-view img {
width: 100%;
border: 1px solid #ddd;
}
.refresh-note {
color: #666;
font-size: 0.9em;
margin-top: 10px;
}
.auto-refresh {
color: green;
font-weight: bold;
}
</style>
<script>
// Auto-refresh the image every 100ms
function refreshImage() {
const img = document.getElementById('live-image');
// Add a timestamp to prevent caching
img.src = 'live_view.png?t=' + new Date().getTime();
}
// Set up auto-refresh
setInterval(refreshImage, 100);
</script>
</head>
<body>
<h1>Browser Live View</h1>
<div class="live-view">
<img id="live-image" src="live_view.png" alt="Live Browser View">
</div>
<p class="refresh-note">
<span class="auto-refresh">Auto-refreshing</span> every 100ms
</p>
</body>
</html>
"""
# Write to live_view.html
with open(os.path.join(self.screenshot_dir, "live_view.html"), "w") as f:
f.write(html_content)
if __name__ == "__main__":
app = QApplication(sys.argv)
browser = WebBrowser()
print(f"Screenshot server running at http://localhost:{browser.server_port}")
print(f"Live view available at http://localhost:{browser.server_port}/live_view.html")
sys.exit(app.exec_())