-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
165 lines (131 loc) · 4.33 KB
/
main.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
import traceback
import StellarPlayer
import requests
import threading
import json
import time
import os
import io
import sys
import socket
plugin_dir = os.path.dirname(__file__)
sys.path.append(plugin_dir)
try:
from bottle import route, run, template, request, response, get, post, static_file, TEMPLATE_PATH
except:
sys.exit(0)
from . import ss
_local_ip = None
_plugin_dir = os.path.dirname(__file__)
_template_dir = os.path.join(_plugin_dir, 'templates')
def get_host_ip():
global _local_ip
s = None
try:
if not _local_ip:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
_local_ip = s.getsockname()[0]
return _local_ip
finally:
if s:
s.close()
class MyPlugin(StellarPlayer.IStellarPlayerPlugin):
def __init__(self,player:StellarPlayer.IStellarPlayer):
super().__init__(player)
self.stoped = False
self.status = 'stop'
self.url = ''
def onPlay(self, *_):
print('---------------onPlay')
self.status = 'play'
def onStopPlay(self, *_):
print('---------------onStopPlay')
self.status = 'stop'
def onPause(self, *args):
play, = args
self.status = 'play' if play else 'pause'
def handleRequest(self, method, args):
if hasattr(self, method) and callable(getattr(self, method)):
getattr(self, method)(*args)
else:
print(f'handleRequest {method=} {args=}')
def show(self):
qrPath = os.path.join(self.player.dataDirectory, f'qr_{os.getpid()}.png')
urlPath = os.path.join(self.player.dataDirectory, f'qr_{os.getpid()}.txt')
self.url = open(urlPath).read()
print(self.url)
controls = [
{'type':'label','name':'手机与电脑接入同一网络内,用手机扫描二维码', 'height': 20, 'hAlign': 'center'},
{'type':'link','name': self.url, 'height': 20, 'hAlign': 'center', '@click': 'onUrlClick'},
{
'type': 'image',
'value': f'{qrPath}'
}
]
self.doModal('main', 400, 440, '', controls)
def start(self):
self.stoped = False
super().start()
t = threading.Thread(target=self.webserverThread, daemon=True)
t.start()
def stop(self):
self.stoped = True
super().stop()
def webserverThread(self):
@get('/')
def index():
f = open(f'{plugin_dir}/templates/index.html', 'rb')
return f.read()
@get('/info')
def progress():
pos, total = self.player.getProgress()
return json.dumps({
'status': self.status,
'pos': pos,
'total': total
})
@post('/progress')
def progress():
pos = request.forms.get('pos')
self.player.setProgress(pos)
return f'{pos}'
@post('/pause')
def pause():
play = request.forms.get('play')
self.player.pause(play)
return f'{play}'
@post('/stop')
def stop():
self.player.stop()
return f''
@post('/prev')
def prev():
self.player.prev()
return f''
@post('/next')
def next():
self.player.next()
return f''
@get('/playlist')
def playlist():
result = self.player.getPlaylist()
return json.dumps(result)
@post('/playlist_play')
def playlist_play():
index = request.forms.get('index')
result = self.player.playPlaylist(int(index))
return json.dumps(result)
@post('/set_playlist_cate')
def set_playlist_cate():
index = request.forms.get('index')
result = self.player.setPlaylistCate(int(index))
return json.dumps(result)
if not _template_dir in TEMPLATE_PATH:
TEMPLATE_PATH.append(_template_dir)
run(host='0.0.0.0', port=0, server=ss.WSGIRefServer)
def newPlugin(player:StellarPlayer.IStellarPlayer,*arg):
plugin = MyPlugin(player)
return plugin
def destroyPlugin(plugin:StellarPlayer.IStellarPlayerPlugin):
plugin.stop()