Allow the suffix for youtube searches to be configurable. Fixes #3

This commit is contained in:
Christoph Stahl 2025-02-02 23:48:45 +01:00
parent 0df93ac758
commit 391562fd3a
3 changed files with 18 additions and 6 deletions

View file

@ -83,6 +83,7 @@ Configuration is done under `sources` → `youtube` with the following settings:
* `tmp_dir`: YouTube videos will be downloaded before playback. This sets the directory, where YouTube videos are stored.
* `max_res`: Maximum resolution of a video.
* `start_streaming`: `true` or `false`. If `true`, videos will be streamed directly using `mpv`, if the video is not cached beforehand. Otherwise, Syng waits for the video to be downloaded.
* `seach_suffix`: A string that is appended to each search query. Default is "karaoke".
### S3
@ -145,6 +146,7 @@ sources:
start_streaming: false
max_res: 720
tmp_dir: ${XDG_CACHE_DIR}/syng
search_suffix: karaoke
```
# Server

View file

@ -1,2 +1,2 @@
SYNG_VERSION = (2, 1, 1)
SYNG_PROTOCOL_VERSION = (2, 1, 0)
SYNG_PROTOCOL_VERSION = (2, 1, 1)

View file

@ -22,7 +22,7 @@ from platformdirs import user_cache_dir
from ..entry import Entry
from ..result import Result
from .source import Source, available_sources
from ..config import BoolOption, ChoiceOption, FolderOption, ListStrOption, ConfigOption
from ..config import BoolOption, ChoiceOption, FolderOption, ListStrOption, ConfigOption, StrOption
class YouTube:
@ -170,6 +170,8 @@ class YoutubeSource(Source):
- ``start_streaming``: If set to ``True``, the client starts streaming
the video, if buffering was not completed. Needs ``youtube-dl`` or
``yt-dlp``. Default is False.
- ``search_suffix``: A string that is appended to the search query.
Default is "karaoke".
"""
source_name = "youtube"
@ -191,6 +193,12 @@ class YoutubeSource(Source):
"Start streaming if\ndownload is not complete",
False,
),
"search_suffix": ConfigOption(
StrOption(),
"A string that is appended to each search query",
"karaoke",
send_to_server=True,
),
}
def apply_config(self, config: dict[str, Any]) -> None:
@ -206,6 +214,7 @@ class YoutubeSource(Source):
self.formatstring = (
f"bestvideo[height<={self.max_res}]+" f"bestaudio/best[height<={self.max_res}]"
)
self.search_suffix = config.get("search_suffix", "karaoke")
self.extra_mpv_options = {"ytdl-format": self.formatstring}
self._yt_dlp = YoutubeDL(
params={
@ -279,8 +288,8 @@ class YoutubeSource(Source):
Search YouTube and the configured channels for the query.
The first results are the results of the configured channels. The next
results are the results from youtube as a whole, but the term "Karaoke"
is appended to the search query.
results are the results from youtube as a whole, a configurable suffix
is appended to the search query (default is "karaoke").
All results are sorted by how good they match to the search query,
respecting their original source (channel or YouTube as a whole).
@ -338,9 +347,10 @@ class YoutubeSource(Source):
def _yt_search(self, query: str) -> list[YouTube]:
"""Search youtube as a whole.
Adds "karaoke" to the query.
Adds a configurable suffix to the query. Default is "karaoke".
"""
return Search(f"{query} karaoke").results
suffix = f" {self.search_suffix}" if self.search_suffix else ""
return Search(f"{query}{suffix}").results
def _channel_search(self, query: str, channel: str) -> list[YouTube]:
"""