This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathworker.py
173 lines (156 loc) · 4.84 KB
/
worker.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 logging
import shlex
import subprocess as sp
import sys
import PySide6.QtCore as qtc
logger = logging.getLogger(__name__)
TITLE = 0
FORMAT = 1
SIZE = 2
PROGRESS = 3
STATUS = 4
SPEED = 5
ETA = 6
class Worker(qtc.QThread):
finished = qtc.Signal(int)
progress = qtc.Signal(object, list)
def __init__(
self,
item,
args,
link,
path,
filename,
fmt,
sponsorblock,
metadata,
thumbnail,
subtitles,
):
super().__init__()
self.item = item
self.args = args
self.link = link
self.path = path
self.filename = filename
self.fmt = fmt
self.sponsorblock = sponsorblock
self.metadata = metadata
self.thumbnail = thumbnail
self.subtitles = subtitles
self.mutex = qtc.QMutex()
self._stop = False
def __str__(self):
s = (
f"(link={self.link}, "
f"args={self.args}, "
f"path={self.path}, "
f"filename={self.filename}, "
f"format={self.fmt}, "
f"sponsorblock={self.sponsorblock}, "
f"metadata={self.metadata}, "
f"thumbnail={self.thumbnail}, "
f"subtitles={self.subtitles})"
)
return s
def build_command(self):
args = [
"yt-dlp",
"--newline",
"--ignore-errors",
"--ignore-config",
"--no-simulate",
"--progress",
"--progress-template",
"%(progress.status)s %(progress._total_bytes_estimate_str)s "
"%(progress._percent_str)s %(progress._speed_str)s %(progress._eta_str)s",
"--dump-json",
"-v",
]
args += self.args if isinstance(self.args, list) else shlex.split(self.args)
if self.metadata:
args += ["--embed-metadata"]
if self.thumbnail:
args += ["--embed-thumbnail"]
if self.subtitles:
args += ["--write-auto-subs"]
if self.sponsorblock == "remove":
args += ["--sponsorblock-remove", "all"]
print("remove")
elif self.sponsorblock == "mark":
args += ["--sponsorblock-mark", "all"]
print("mark")
if self.path:
args += [
"-o",
f"{self.path}/{self.filename}" if self.filename else self.path,
]
args += ["--", self.link]
return args
def stop(self):
with qtc.QMutexLocker(self.mutex):
self._stop = True
def run(self):
create_window = sp.CREATE_NO_WINDOW if sys.platform == "win32" else 0
command = self.build_command()
output = []
logger.info(
f"Download ({self.item.id}) starting with cmd: " + shlex.join(command)
)
with sp.Popen(
command,
stdout=sp.PIPE,
stderr=sp.STDOUT,
text=True,
universal_newlines=True,
creationflags=create_window,
) as p:
for line in p.stdout:
output.append(line)
with qtc.QMutexLocker(self.mutex):
if self._stop:
p.terminate()
break
if line.startswith("{"):
title = json.loads(line)["title"]
logger.debug(f"Download ({self.item.id}) title: {title}")
self.progress.emit(
self.item,
[(TITLE, title), (STATUS, "Processing")],
)
elif line.lower().startswith("downloading"):
data = line.split()
self.progress.emit(
self.item,
[
(SIZE, data[1]),
(PROGRESS, data[2]),
(SPEED, data[3]),
(ETA, data[4]),
(STATUS, "Downloading"),
],
)
elif line.startswith(("[Merger]", "[ExtractAudio]")):
self.progress.emit(self.item, [(STATUS, "Converting")])
if p.returncode != 0:
logger.error(
f'Download ({self.item.id}) returncode: {p.returncode}\n{"".join(output)}'
)
self.progress.emit(
self.item,
[
(SIZE, "ERROR"),
(STATUS, "ERROR"),
(SPEED, "ERROR"),
],
)
else:
self.progress.emit(
self.item,
[
(PROGRESS, "100%"),
(STATUS, "Finished"),
],
)
self.finished.emit(self.item.id)