Config, cookies
This commit is contained in:
parent
93c5ea3b18
commit
df242e833f
6 changed files with 43 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ __pycache__/
|
||||||
.ruff_cache/
|
.ruff_cache/
|
||||||
|
|
||||||
music/
|
music/
|
||||||
|
cookies/
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -1,7 +1,7 @@
|
||||||
.PHONY: run test build frontend backend
|
.PHONY: run test build frontend backend
|
||||||
|
|
||||||
run:
|
run:
|
||||||
@echo 'Not implemented'
|
HOST=127.0.0.1 PORT=4009 COOKIES_DIR=cookies python3 ./backend/main.py
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@python3 -m unittest discover -vcs ./backend
|
@python3 -m unittest discover -vcs ./backend
|
||||||
|
|
22
backend/config.py
Normal file
22
backend/config.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
|
||||||
|
self.host = os.getenv('HOST') or '0.0.0.0'
|
||||||
|
self.port = int(os.getenv('PORT') or 4009)
|
||||||
|
self.path_length = int(os.getenv('PATH_LENGTH') or 255)
|
||||||
|
self.cookies_dir = Path(os.getenv('COOKIES_DIR') or 'cookies')
|
||||||
|
|
||||||
|
|
||||||
|
_config: Config | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def get() -> Config:
|
||||||
|
global _config
|
||||||
|
if _config is None:
|
||||||
|
_config = Config()
|
||||||
|
return _config
|
|
@ -3,6 +3,7 @@ import json
|
||||||
|
|
||||||
import websockets
|
import websockets
|
||||||
|
|
||||||
|
import config
|
||||||
import response
|
import response
|
||||||
import ydl_pool
|
import ydl_pool
|
||||||
import ydl_wrap
|
import ydl_wrap
|
||||||
|
@ -11,10 +12,6 @@ import ydl_wrap
|
||||||
type SocketT = websockets.WebSocketServerProtocol
|
type SocketT = websockets.WebSocketServerProtocol
|
||||||
|
|
||||||
|
|
||||||
# TODO: config
|
|
||||||
HOST = '127.0.0.1'
|
|
||||||
PORT = 5678
|
|
||||||
|
|
||||||
|
|
||||||
async def handler(socket: SocketT) -> None:
|
async def handler(socket: SocketT) -> None:
|
||||||
|
|
||||||
|
@ -59,7 +56,8 @@ async def handler(socket: SocketT) -> None:
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
async with websockets.serve(handler, HOST, PORT):
|
cfg = config.get()
|
||||||
|
async with websockets.serve(handler, cfg.host, cfg.port):
|
||||||
await asyncio.Future()
|
await asyncio.Future()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from yt_dlp import YoutubeDL
|
from yt_dlp import YoutubeDL
|
||||||
from yt_dlp.postprocessor import FFmpegExtractAudioPP
|
from yt_dlp.postprocessor import FFmpegExtractAudioPP
|
||||||
|
|
||||||
|
import config
|
||||||
import id3pp
|
import id3pp
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ class _CreateYDL:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def yandex() -> YoutubeDL:
|
def yandex() -> YoutubeDL:
|
||||||
return YoutubeDL() # TODO: cookies
|
return YoutubeDL()
|
||||||
|
|
||||||
|
|
||||||
create_ydl_fn = {
|
create_ydl_fn = {
|
||||||
|
@ -45,13 +46,21 @@ class Downloaders:
|
||||||
|
|
||||||
def get_ydl(self, site: str) -> YoutubeDL:
|
def get_ydl(self, site: str) -> YoutubeDL:
|
||||||
|
|
||||||
|
cfg = config.get()
|
||||||
ydl = self.ydls[site]
|
ydl = self.ydls[site]
|
||||||
|
|
||||||
if ydl is None:
|
if ydl is None:
|
||||||
ydl = create_ydl_fn[site]()
|
ydl = create_ydl_fn[site]()
|
||||||
ydl.params['trim_file_name'] = 255 # TODO: config # NOTE: includes path, not only filename
|
|
||||||
|
ydl.params['trim_file_name'] = cfg.path_length # NOTE: includes path, not only filename
|
||||||
# artists.0 instead of artist, because it can contain "feat. ..."
|
# artists.0 instead of artist, because it can contain "feat. ..."
|
||||||
ydl.params['outtmpl']['default'] = 'music/%(artists.0)s/%(album)s/%(track)s.%(ext)s'
|
ydl.params['outtmpl']['default'] = 'music/%(artists.0)s/%(album)s/%(track)s.%(ext)s'
|
||||||
ydl.add_post_processor(id3pp.ID3TagsPP(), when='post_process')
|
ydl.add_post_processor(id3pp.ID3TagsPP(), when='post_process')
|
||||||
|
|
||||||
|
cookies = cfg.cookies_dir / (site + '.txt')
|
||||||
|
if cookies.exists():
|
||||||
|
ydl.params['cookiefile'] = str(cookies)
|
||||||
|
|
||||||
return ydl
|
return ydl
|
||||||
|
|
||||||
def cleanup(self) -> None:
|
def cleanup(self) -> None:
|
||||||
|
|
|
@ -16,10 +16,13 @@ async def get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]:
|
||||||
|
|
||||||
def _target_get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]:
|
def _target_get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]:
|
||||||
|
|
||||||
info = ydl.extract_info(url, download=False, process=False)
|
info = ydl.extract_info(url, download=False, process=True)
|
||||||
if info is None:
|
if info is None:
|
||||||
raise RuntimeError('ydl.extract_info returned None')
|
raise RuntimeError('ydl.extract_info returned None')
|
||||||
return [entry['title'] for entry in info['entries']]
|
return [
|
||||||
|
entry['track'] if 'track' in entry else entry['title']
|
||||||
|
for entry in info['entries']
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def download(
|
async def download(
|
||||||
|
|
Loading…
Add table
Reference in a new issue