This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
command.py
257 lines (218 loc) · 5.88 KB
/
command.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import logging
import subprocess
import pathlib
import os
import requests
import shutil
from mbot.core.params import ArgSchema, ArgType
from mbot.core.plugins import (
plugin,
PluginCommandContext,
PluginCommandResponse,
)
from mbot.openapi import mbot_api
from . import config
server = mbot_api
_LOGGER = logging.getLogger(__name__)
bin_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "mdc_ng")
bin_path_new = bin_path + ".new"
backend_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backend")
backend_path_new = backend_path + ".new"
mdc_server = None
@plugin.command(
name="update",
title="更新MDC",
desc="拉取最新MDC核心库和后端服务",
icon="AlarmOn",
run_in_background=True,
)
def update(ctx: PluginCommandContext):
update_bin()
update_server()
_LOGGER.info("MDC更新成功")
return PluginCommandResponse(True, "更新成功")
@plugin.command(
name="start_mdc_server",
title="启动/停止MDC服务端",
desc="启动/停止MDC服务端",
icon="AlarmOn",
run_in_background=True,
)
def start_mdc_server(ctx: PluginCommandContext):
global mdc_server
if not mdc_server:
start_server()
else:
stop_server()
return PluginCommandResponse(True, "启动成功,请通过9208端口访问Web UI")
@plugin.command(
name="mdc_manual",
title="MDC手动执行",
desc="指定目录或视频文件进行刮削",
icon="",
run_in_background=True,
)
def mdc_test(
ctx: PluginCommandContext, path: ArgSchema(ArgType.String, "刮削路径", "")
):
try:
mdc_dir(path)
except Exception as e:
_LOGGER.error(e, exc_info=True)
return PluginCommandResponse(False, f"一键刮削成功")
return PluginCommandResponse(True, f"一键刮削失败")
def update_bin():
download_file(
"https://github.com/mdc-ng/mdc-ng/releases/download/latest/mdc_ng",
bin_path_new,
)
try:
os.remove(bin_path)
except:
pass
os.rename(bin_path_new, bin_path)
run_command(
[
"chmod",
"+x",
"mdc_ng",
],
False,
cwd=pathlib.Path(__file__).parent.absolute(),
)
run_command(
[
"./mdc_ng",
"-v",
],
True,
cwd=pathlib.Path(__file__).parent.absolute(),
)
def update_server():
download_file(
"https://github.com/mdc-ng/mdc-ng/releases/download/latest/backend",
backend_path_new,
)
try:
os.remove(backend_path)
except:
pass
os.rename(backend_path_new, backend_path)
run_command(
[
"chmod",
"+x",
"backend",
],
False,
cwd=pathlib.Path(__file__).parent.absolute(),
)
run_command(
[
"./backend",
"-v",
],
True,
cwd=pathlib.Path(__file__).parent.absolute(),
)
def mdc_main(
path: str,
config_ini=None,
target_folder=None,
link_mode=None,
):
if not target_folder:
target_folder = config.target_folder
if not link_mode:
link_mode = config.link_mode
proxy = config.proxies["http"]
command = ["./mdc_ng", "-p", path]
if config_ini:
command.extend(["-c", config_ini])
if target_folder:
command.extend(["-t", target_folder])
if proxy:
command.extend(["--proxy", proxy])
if link_mode:
command.extend(["-l", link_mode])
_LOGGER.info(command)
run_command(
command,
True,
cwd=pathlib.Path(__file__).parent.absolute(),
env={"RUST_LOG": "mdc_ng=info"},
)
def mdc_dir(path: str, target_folder=None):
videos = collect_videos(path)
if len(videos) > 0:
_LOGGER.info("[MDC] 视频文件检测到: %s" % videos)
if len(videos) > 10:
_LOGGER.info("[MDC] 视频文件数量多于10个,不处理")
for video in videos:
_LOGGER.info("[MDC] 开始处理视频文件: %s" % video)
try:
mdc_main(video, target_folder=target_folder)
except Exception as e:
_LOGGER.error("[MDC] 处理视频文件出错: %s" % video)
_LOGGER.error(e)
continue
_LOGGER.info("[MDC] 处理视频文件完成: %s" % video)
def collect_videos(path: str):
if not path:
return []
videos = []
if os.path.isdir(path):
for file in os.listdir(path):
videos.extend(collect_videos(os.path.join(path, file)))
return videos
elif os.path.splitext(path)[1].lower() in [
".mp4",
".avi",
".rmvb",
".wmv",
".mov",
".mkv",
".webm",
".iso",
".mpg",
".m4v",
".ts",
]:
return [path]
else:
return []
def download_file(url, name):
with requests.get(url, proxies=config.proxies, stream=True) as r:
with open(name, "wb") as f:
shutil.copyfileobj(r.raw, f)
return name
def run_command(command, capture, **kwargs):
"""Run a command while printing the live output"""
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
**kwargs,
)
while True: # Could be more pythonic with := in Python3.8+
line = process.stdout.readline()
if not line and process.poll() is not None:
break
if capture:
_LOGGER.info(line.decode().strip())
def start_server():
global mdc_server
if not mdc_server:
mdc_server = subprocess.Popen(
[
"./backend",
"-p",
"9208",
],
cwd=pathlib.Path(__file__).parent.absolute(),
)
def stop_server():
global mdc_server
if mdc_server:
mdc_server.terminate()
mdc_server = None