-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
173 lines (155 loc) · 6.48 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
166
167
168
169
170
171
172
173
import json
import os
import sys
import traceback
import urllib.parse
sys.path.append(os.path.dirname(__file__))
import easywebdav
# easywebdav = __import__('stellar-webdav.easywebdav')
import StellarPlayer
class WebdavPlugin(StellarPlayer.IStellarPlayerPlugin):
def __init__(self, player: StellarPlayer.IStellarPlayer):
super().__init__(player)
self.playurl = []
self.url = ''
self.webdav = None
self.path = '/'
self.dir = None
self.username = ''
self.password = ''
self.host = ''
self.port = ''
self.ssl = False
self.verify = False
self.protocol = 'http'
def isdir(self, item: easywebdav.File):
if item.contenttype == 'httpd/unix-directory':
return True
# 类型为空,并且大小为0
if not item.contenttype and item.size == 0:
return True
return False
def ls(self):
if not self.path.endswith('/'):
self.path = self.path + '/'
self.dir = self.webdav.ls(self.path)[1:]
print(self.dir)
# 文件夹类型排在前面
self.dir.sort(key=lambda item: 0 if self.isdir(item) else 1)
list_val = []
for i in self.dir:
path = i.name
if self.isdir(i) and not path.endswith('/'):
path = path + '/'
path = path.replace(self.path, '', 1)
list_val.append({'title': urllib.parse.unquote(path)})
return list_val
def update_list_view(self):
self.player.loadingAnimation('main')
list_val = self.ls()
self.player.loadingAnimation('main', stop=True)
self.player.updateControlValue('main', 'path', urllib.parse.unquote(self.path))
self.player.updateControlValue('main', 'list', list_val)
def show(self):
if not self.webdav:
self.load_config()
controls = [
{'type': 'space', 'height': 10},
{'type': 'edit', 'name': '主机名', 'height': 30, 'value': self.host},
{'type': 'space', 'height': 10},
{
'group': [
{'type': 'edit', 'name': '端口', 'height': 30, 'width': 0.4, 'value': self.port},
{'type': 'check', 'name': 'SSL', 'height': 30, 'width': 0.1, ':value': 'ssl'},
{'type': 'check', 'name': '验证SSL证书', 'height': 30, 'width': 0.2, ':value': 'verify'},
],
'height': 30
},
{'type': 'space', 'height': 10},
{'type': 'edit', 'name': '用户名', 'height': 30, 'value': self.username},
{'type': 'space', 'height': 10},
{'type': 'edit', 'name': '密码', 'height': 30, 'value': self.password},
{'type': 'space', 'height': 10},
{'type': 'button', 'name': '连接', '@click': 'on_connect_webdav', 'height': 30}
]
self.doModal('login', 600, 400, '', controls)
else:
list_val = self.ls()
list_layout = {'type': 'link', 'name': 'title', '@click': 'on_click_item'}
controls = [
{
'group':
[
{'type': 'link', 'name': '返回', 'width': 30, '@click': 'on_click_back'},
{'type': 'label', 'name': 'path', 'value': self.path},
],
'height': 30
},
{'type': 'list', 'name': 'list', 'itemlayout': {'group': list_layout}, 'value': list_val,
'separator': True, 'itemheight': 40}
]
self.doModal('main', 800, 600, '', controls)
def load_config(self):
try:
f = open('config.json', 'r')
config = json.load(f)
self.host = config.get('host', '')
self.port = config.get('port', '')
self.username = config.get('username', '')
self.password = config.get('password', '')
self.ssl = config.get('ssl', False)
self.verify = config.get('verify', False)
f.close()
except:
traceback.print_exc()
def save_config(self):
try:
f = open('config.json', 'w')
json.dump({'host': self.host, 'port': self.port, 'username': self.username, 'password': self.password,
'ssl': self.ssl, 'verify': self.verify}, f)
f.close()
except:
traceback.print_exc()
def on_connect_webdav(self, *arg):
self.host = self.player.getControlValue('login', '主机名')
self.port = self.player.getControlValue('login', '端口')
self.username = self.player.getControlValue('login', '用户名')
self.password = self.player.getControlValue('login', '密码')
self.ssl = self.player.getControlValue('login', 'SSL')
self.protocol = 'https' if self.ssl else 'http'
self.verify = self.player.getControlValue('login', '验证SSL证书')
self.save_config()
self.player.loadingAnimation('login')
try:
self.webdav = easywebdav.connect(self.host, port=int(self.port), username=self.username,
password=self.password, protocol=self.protocol,
verify_ssl=self.verify)
self.webdav.ls()
self.player.closeModal('login', True)
self.show()
except Exception as e:
print(e)
self.player.loadingAnimation('login', stop=True)
self.player.toast('login', '连接失败\r\n' + str(e))
self.webdav = None
def on_click_item(self, page, control, idx, *arg):
file = self.dir[idx]
if self.isdir(file):
self.path = file.name
self.update_list_view()
else:
self.player.play(f'{self.protocol}://{self.username}:{self.password}@{self.host}:{self.port}' + file.name)
def on_click_back(self, *arg):
if self.path != '/':
temp = self.path.split('/')
paths = []
for i in temp:
if i:
paths.append(i)
self.path = '/' + '/'.join(paths[:-1])
self.update_list_view()
def newPlugin(player: StellarPlayer.IStellarPlayer, *arg):
plugin = WebdavPlugin(player)
return plugin
def destroyPlugin(plugin: StellarPlayer.IStellarPlayerPlugin):
plugin.stop()