-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (90 loc) · 3.11 KB
/
main.py
File metadata and controls
117 lines (90 loc) · 3.11 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
import asyncio
import uvicorn
import socket
from starlette.applications import Starlette
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from starlette.responses import JSONResponse
import pyautogui
app_run_port = 8088
app_run_host = "0.0.0.0"
templates = Jinja2Templates(directory='templates')
app = Starlette()
app.mount('/static', StaticFiles(directory='statics'), name='static')
async def press_key_board(key_code):
def _press_keyboard():
pyautogui.press(key_code)
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(
None, _press_keyboard)
return result
async def get_local_ip():
def _func():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip_addr = s.getsockname()[0]
finally:
s.close()
return ip_addr
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, _func)
@app.on_event('startup')
async def startup_do():
local_ip = await get_local_ip()
print(f"{'*' * 10} | {'*' * 10}")
print(f"{'*' * 10} | {'*' * 10}")
print(" Web Control PC Music")
print(f"{'*' * 10} | {'*' * 10}")
print(f"{'*' * 10} | {'*' * 10}")
print(f" 请使用手机浏览器访问\n {local_ip}:{app_run_port}")
print(f"{'*' * 10} | {'*' * 10}")
print(f"{'*' * 10} | {'*' * 10}")
print(f"{'*' * 10} | {'*' * 10}")
@app.route('/')
async def index(request):
template = "index.html"
context = {"request": request}
return templates.TemplateResponse(template, context)
@app.route('/api/music')
async def music_control_api(request):
action = request.query_params.get('action')
if action not in ['playpause', 'next', 'prev']:
return JSONResponse({'status': 1, 'msg': f'控制指令: {action} 非法!'})
action_key_code_map = {
'playpause': 'playpause',
'next': 'nexttrack',
'prev': 'prevtrack',
}
await press_key_board(action_key_code_map[action])
return JSONResponse({'status': 0})
@app.route('/api/volume')
async def volume_control_api(request):
action = request.query_params.get('action')
if action not in ['up', 'down', 'mute']:
return JSONResponse({'status': 1, 'msg': f'音量控制指令: {action} 非法!'})
action_key_code_map = {
'up': 'volumeup',
'down': 'volumedown',
'mute': 'volumemute'
}
await press_key_board(action_key_code_map[action])
return JSONResponse({'status': 0})
@app.exception_handler(404)
async def not_found(request, exc):
"""
Return an HTTP 404 page.
"""
template = "404.html"
context = {"request": request}
return templates.TemplateResponse(template, context, status_code=404)
@app.exception_handler(500)
async def server_error(request, exc):
"""
Return an HTTP 500 page.
"""
template = "500.html"
context = {"request": request}
return templates.TemplateResponse(template, context, status_code=500)
if __name__ == "__main__":
uvicorn.run(app, host=app_run_host, port=app_run_port, access_log=False)