-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetMediaCtrl.py
166 lines (130 loc) · 4.31 KB
/
NetMediaCtrl.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
162
163
164
165
166
'''
Environmental requirements: python3 pypiwin32 attrs qrcode
'''
from http.server import HTTPServer, BaseHTTPRequestHandler
from typing import Dict, Type
import socket
import win32api
import win32con
import qrcode
from attr import dataclass
def CtrlAltKeyDown():
win32api.keybd_event(17, 0, 0, 0) # ctrl键位码是17
win32api.keybd_event(18, 0, 0, 0) # alt键位码是18
def CtrlAltKeyUp():
win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
# p键位码是80,right键位码是39,left键位码是37,D键位码是68,L是76,↑是38,↓是40
def ActionKey(KCode):
win32api.keybd_event(KCode, 0, 0, 0)
win32api.keybd_event(KCode, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键
def MediaControl(action):
CtrlAltKeyDown()
if action == 'pause':
ActionKey(80)
if action == 'next':
ActionKey(39)
if action == 'previous':
ActionKey(37)
if action == 'like':
ActionKey(76)
if action == 'vup':
ActionKey(38)
if action == 'vdown':
ActionKey(40)
CtrlAltKeyUp()
@dataclass
class Response:
code: int
header: Dict[str, str]
content: str
@dataclass
class Request:
path: str
data: str
class Page:
def do_get(self, request: Request) -> Response:
pass
def do_post(self, request: Request) -> Response:
pass
class HomePage(Page):
def do_get(self, request: Request) -> Response:
return HomePage._get_home_page()
@staticmethod
def _get_home_page():
return Response(code=200, header={'Content-Type': 'text/html'},
content=open('home.html').read())
def do_post(self, request: Request) -> Response:
action = request.data
if action == 'act=pause':
MediaControl('pause')
elif action == 'act=previous':
MediaControl('previous')
elif action == 'act=next':
MediaControl('next')
elif action == 'act=like':
MediaControl('like')
elif action == 'act=vup':
MediaControl('vup')
elif action == 'act=vdown':
MediaControl('vdown')
return HomePage._get_home_page()
router: Dict[str, Type[Page]] = {
'/': HomePage
}
class WebCommandRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path not in router.keys():
self.return_page_not_found()
return
res = router.get(self.path)().do_get(Request(path=self.path, data=''))
self._send_response(res)
def return_page_not_found(self):
self.send_error(404, 'are you ok?')
def _send_response(self, res: Response):
self.send_response(res.code)
for k, v in res.header.items():
self.send_header(k, v)
self.end_headers()
self.wfile.write(res.content.encode(encoding='utf-8'))
def do_POST(self):
if self.path not in router.keys():
self.return_page_not_found()
return
length = int(self.headers['content-length'])
res = router.get(self.path)().do_post(Request(path=self.path, data=self.rfile.read(length).decode()))
self._send_response(res)
pass
def get_host_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def qrcode_gen(url):
qr = qrcode.QRCode()
qr.border = 1
qr.add_data(url)
qr.make()
qr.print_ascii(out=None, tty=False, invert=False)
def NetControl():
print("Starting")
try:
port = int(input('Input server port,default [10086]:'))
except Exception:
port = 10086
web_host = ('0.0.0.0', port)
server = HTTPServer(web_host, WebCommandRequestHandler)
print("Web server started, listening at: %s" % str(web_host))
link_addr = "http://" + str(get_host_ip()) + ":" + str(port)
print(link_addr)
qrcode_gen(link_addr)
try:
server.serve_forever()
except KeyboardInterrupt:
print("Exiting")
pass
if __name__ == "__main__":
NetControl()