-
Notifications
You must be signed in to change notification settings - Fork 1
/
emonitor.py
161 lines (113 loc) · 3.6 KB
/
emonitor.py
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
#!/usr/bin/python
#eMonitor by Kranu
#modified by DarkTrick
#Version 3
#Tested on Ubuntu 21.14, Python 3.9
#BEGIN SETUP
#HTTP Server
port=8080 #port of http server (http://127.0.0.1:8000/)
#capture region
#l,t=(1680,1050-800)
l,t=(10,120) #left and right offset from primary monitor
#rootWindow,h=(595,701) #width and height of capture region
rootWindow,h=(701,595) #width and height of capture region
# "ss" = screenshot
ssX = l
ssY = t
ssWidth = rootWindow
ssHeight = h
#fn ='shot.jpg' #file name of screenshot
fn ='shot.png' #file name of screenshot
#//END SETUP
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
# import GTK
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk as gdk
from gi.repository import GObject as gobject
# Windows only?
# import gtk.gdk as gdk
class GdkPixbufAdapterLinux:
"""
I'm not sure if the code here is same on win and linux;
thereore I created this adapter already - just in case.
"""
def __init__(self, rootWindow, ssX,ssY,ssWidth,ssHeight):
self.buffer = gdk.pixbuf_get_from_window(rootWindow, ssX,ssY,ssWidth,ssHeight)
def save(self, filename, encoding = "png"):
if (not self.hasBuffer()):
return False
return self.buffer.savev (filename, encoding)
def hasBuffer(self):
return (self.buffer != None)
def rotate_simple(self, degrees):
self.buffer = self.buffer.rotate_simple(degrees)
def gtkScreenshot():
rootWindow = gdk.get_default_root_window()
#ssWidth,ssHeight = rootWindow.get_size()
#print "The size of the window is %d x %d" % sz
image = GdkPixbufAdapterLinux (rootWindow, ssX,ssY,ssWidth,ssHeight)
image.rotate_simple(90)
ts = time.time()
filename = fn
if (not image.save(filename,"png")):
print("Unable to get the screenshot.")
class serv(BaseHTTPRequestHandler):
def deliverSite(self):
#print("deliverSite()")
self.send_header('Content-type','text/html')
self.end_headers()
reloadFunction = 'function() {'\
' document.getElementById("pic").src="'+fn+'?"+(new Date()).getTime();'\
'}'
website = """
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>eMonitor by Kranu</title>
</head>
<body id="bod" style="margin:0px;">
<img id="pic" src='shot.png' >
<script type="text/javascript">
document.getElementById("pic").onclick="""+reloadFunction+"""
</script>
</body>
</html>"""
with open("debug_website.html","w") as file:
file.write(website)
websiteBytes = bytes(website,"utf-8")
self.wfile.write(websiteBytes)
def do_GET(self):
self.send_response(200)
if self.path.startswith('/'+fn): #check, if the request was about the image
print("deliverImage()")
self.send_header('Content-type','image/jpeg')
self.end_headers()
gtkScreenshot()
with open(fn,'rb') as f:
self.wfile.write(f.read())
else:
self.deliverSite()
def main():
try:
print( 'eMonitor by Kranu')
#Amazon's website is used here for its reliablity. Feel free to change it.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("amazon.com",80))
print ('Starting.. '),
server=HTTPServer(('',port),serv)
print ('Press Ctrl+C to stop')
print ("")
print ('On your Kindle, visit http://'+s.getsockname()[0]+':'+str(port)+'/')
# take initial screenshot
gtkScreenshot()
server.serve_forever()
except KeyboardInterrupt:
print ('Stopping.. '),
server.socket.close()
if (__name__ == "__main__"):
main()