Compare commits
No commits in common. "54449690273128bb450a37239c2ebb8faf48c5b7" and "8720ea07fd66a99569a146f7d490d77051c22cbf" have entirely different histories.
5444969027
...
8720ea07fd
5 changed files with 20 additions and 80 deletions
|
@ -13,18 +13,7 @@ type SocketT = websockets.WebSocketServerProtocol
|
|||
|
||||
async def handler(socket: SocketT) -> None:
|
||||
|
||||
async def ydl_log_handler(level: response.YdlLogLevel, msg: str) -> None:
|
||||
try:
|
||||
await socket.send(response.ydl_log(level, msg))
|
||||
except:
|
||||
pass
|
||||
|
||||
ydls = ydl_pool.Downloader(
|
||||
ydl_pool.YdlLogger(
|
||||
ydl_log_handler,
|
||||
asyncio.get_event_loop(),
|
||||
)
|
||||
)
|
||||
ydls = ydl_pool.Downloader(None, None) # type: ignore # TODO
|
||||
|
||||
async for message in socket:
|
||||
|
||||
|
@ -45,7 +34,9 @@ async def handler(socket: SocketT) -> None:
|
|||
data['url'],
|
||||
data.get('items'),
|
||||
)
|
||||
await socket.send(response.ydl_end(ret))
|
||||
await socket.send(response.dl_end(ret))
|
||||
|
||||
# TODO: cancellation
|
||||
|
||||
case _:
|
||||
raise ValueError('invalid "action" field value')
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import json
|
||||
import logging
|
||||
from typing import Literal
|
||||
|
||||
import traceback
|
||||
|
||||
def playlist(items: list[str]) -> str:
|
||||
return json.dumps({
|
||||
|
@ -9,25 +7,20 @@ def playlist(items: list[str]) -> str:
|
|||
"data": items,
|
||||
})
|
||||
|
||||
|
||||
type YdlLogLevel = Literal['debug'] | Literal['warning'] | Literal['error']
|
||||
|
||||
def ydl_log(level: YdlLogLevel, msg: str) -> str:
|
||||
def dl_progress(msg: str) -> str:
|
||||
return json.dumps({
|
||||
"type": "ydl_log",
|
||||
"level": level,
|
||||
"type": "dl_progress",
|
||||
"data": msg,
|
||||
})
|
||||
|
||||
def ydl_end(ret: int) -> str:
|
||||
def dl_end(ret: int) -> str:
|
||||
return json.dumps({
|
||||
"type": "ydl_end",
|
||||
"type": "dl_end",
|
||||
"data": ret,
|
||||
})
|
||||
|
||||
|
||||
def error(ex: Exception) -> str:
|
||||
logging.getLogger('musicdlp').exception(ex)
|
||||
traceback.print_tb(ex.__traceback__)
|
||||
return json.dumps({
|
||||
"type": "error",
|
||||
"data": {
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import asyncio
|
||||
import logging
|
||||
from typing import Callable, Awaitable, Iterable
|
||||
|
||||
from yt_dlp import YoutubeDL
|
||||
from yt_dlp.postprocessor import FFmpegExtractAudioPP
|
||||
|
||||
import config
|
||||
import response
|
||||
import id3pp
|
||||
|
||||
|
||||
|
@ -42,33 +40,12 @@ ydl_fn_keys = create_ydl_fn.keys()
|
|||
NP_YDLS = {'yandex'}
|
||||
|
||||
|
||||
class YdlLogger:
|
||||
class Downloader:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
log_cb: Callable[[response.YdlLogLevel, str], Awaitable],
|
||||
loop: asyncio.AbstractEventLoop) -> None:
|
||||
self.log_cb = log_cb
|
||||
self.loop = loop
|
||||
self.mdlp_logger = logging.getLogger('musicdlp')
|
||||
|
||||
def debug(self, msg: str) -> None:
|
||||
asyncio.run_coroutine_threadsafe(self.log_cb('debug', msg), self.loop)
|
||||
|
||||
def info(self, _: str) -> None: # afaik not used in yt-dlp
|
||||
pass
|
||||
|
||||
def warning(self, msg: str) -> None:
|
||||
asyncio.run_coroutine_threadsafe(self.log_cb('warning', msg), self.loop)
|
||||
|
||||
def error(self, msg: str) -> None:
|
||||
self.mdlp_logger.error(msg)
|
||||
asyncio.run_coroutine_threadsafe(self.log_cb('error', msg), self.loop)
|
||||
|
||||
|
||||
class Downloader:
|
||||
|
||||
def __init__(self, logger: YdlLogger | None = None) -> None:
|
||||
progress_cb: Callable[[str], Awaitable],
|
||||
lyrics_cb: Callable[[list[str]], Awaitable]) -> None:
|
||||
|
||||
self.ydls: dict[str, YoutubeDL | None] = {
|
||||
'youtube': None,
|
||||
|
@ -79,9 +56,8 @@ class Downloader:
|
|||
self.cur_ydl: YoutubeDL | None = None
|
||||
self.cur_site = ''
|
||||
|
||||
self.id3pp_obj = id3pp.ID3TagsPP()
|
||||
|
||||
self.logger = logger
|
||||
self.progress_cb = progress_cb
|
||||
self.lyrics_cb = lyrics_cb
|
||||
|
||||
def choose_ydl(self, site: str) -> None:
|
||||
|
||||
|
@ -91,11 +67,8 @@ class Downloader:
|
|||
if ydl is None:
|
||||
ydl = create_ydl_fn[site]()
|
||||
|
||||
if self.logger is not None:
|
||||
ydl.params['logger'] = self.logger
|
||||
|
||||
ydl.params['outtmpl']['default'] = cfg.tmpl
|
||||
ydl.add_post_processor(self.id3pp_obj, when='post_process')
|
||||
ydl.add_post_processor(id3pp.ID3TagsPP(), when='post_process')
|
||||
|
||||
cookies = cfg.cookies_dir / (site + '.txt')
|
||||
if cookies.exists():
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
<button type="button" id="dl-btn">Download</button>
|
||||
</div>
|
||||
<div>
|
||||
<pre><code id="log"></code></pre>
|
||||
<label>Progress: <span id="progress">not implemented</span></label>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -21,15 +21,12 @@ addEventListener('DOMContentLoaded', () => {
|
|||
const itemsDiv = document.getElementById('items-container')
|
||||
let items = []
|
||||
|
||||
const logField = document.getElementById('log')
|
||||
|
||||
document.getElementById('items-btn').addEventListener('click', () => {
|
||||
socket.send(JSON.stringify({
|
||||
action: 'list',
|
||||
site: site.value,
|
||||
url: urlField.value,
|
||||
}))
|
||||
logField.textContent = '' // clear
|
||||
})
|
||||
|
||||
document.getElementById('dl-btn').addEventListener('click', () => {
|
||||
|
@ -40,14 +37,13 @@ addEventListener('DOMContentLoaded', () => {
|
|||
items: items,
|
||||
}))
|
||||
items = []
|
||||
logField.textContent = ''
|
||||
})
|
||||
|
||||
socket.addEventListener('message', ev => {
|
||||
const msg = JSON.parse(ev.data)
|
||||
switch (msg.type) {
|
||||
case 'items':
|
||||
itemsDiv.textContent = ''
|
||||
itemsDiv.textContent = '' // clear
|
||||
const len = msg.data.length
|
||||
for (let i = 1; i <= len; i++) {
|
||||
const itemElem = document.createElement('label')
|
||||
|
@ -66,22 +62,9 @@ addEventListener('DOMContentLoaded', () => {
|
|||
itemsDiv.append(itemElem)
|
||||
}
|
||||
break
|
||||
case 'ydl_log':
|
||||
const lineElem = document.createElement('span')
|
||||
switch (msg.level) {
|
||||
case 'warning':
|
||||
lineElem.className = 'log-warning'
|
||||
case 'dl_progress':
|
||||
break
|
||||
case 'error':
|
||||
lineElem.className = 'log-error'
|
||||
break
|
||||
default: // add no classes if `debug` or some unknown level
|
||||
break
|
||||
}
|
||||
lineElem.innerText = msg.data + '\n'
|
||||
logField.append(lineElem)
|
||||
break
|
||||
case 'ydl_end':
|
||||
case 'dl_end':
|
||||
break
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Reference in a new issue