diff --git a/syng/result.py b/syng/result.py index 4936c80..39a0bc7 100644 --- a/syng/result.py +++ b/syng/result.py @@ -3,6 +3,7 @@ from __future__ import annotations from dataclasses import dataclass import os.path +from typing import Optional @dataclass @@ -27,6 +28,7 @@ class Result: title: str artist: str album: str + duration: Optional[str] = None @classmethod def from_filename(cls, filename: str, source: str) -> Result: @@ -71,6 +73,7 @@ class Result: - title (str) - artist (str) - album (str) + - duration (int, optional) :param values: The dictionary with the values :type values: dict[str, str] @@ -83,6 +86,7 @@ class Result: title=values["title"], artist=values["artist"], album=values["album"], + duration=values.get("duration", None), ) def to_dict(self) -> dict[str, str]: @@ -95,14 +99,18 @@ class Result: - title (str) - artist (str) - album (str) + - duration (str, if available) :return: The dictionary with the values :rtype: dict[str, str] """ - return { + output = { "ident": self.ident, "source": self.source, "title": self.title, "artist": self.artist, "album": self.album, } + if self.duration is not None: + output["duration"] = self.duration + return output diff --git a/syng/server.py b/syng/server.py index 5d04e19..3eeff94 100644 --- a/syng/server.py +++ b/syng/server.py @@ -1126,10 +1126,19 @@ async def handle_search_results(sid: str, data: dict[str, Any]) -> None: web_sid = data["sid"] results = [Result.from_dict(result) for result in data["results"]] - # TODO this handles YouTubes anti-bot measures - + # TODO: we convert the results to YouTube objects. This + # adds them to the cache to prevent YouTube from blocking us. __unused_yt_list = [ - YouTube.from_result(result) for result in data["results"] if "youtube" in "ident" + YouTube.from_result( + { + "duration": result.duration, + "title": result.title, + "channel": result.artist, + "url": result.ident, + } + ) + for result in results + if "youtube" == result.source ] await send_search_results(web_sid, results) diff --git a/syng/sources/youtube.py b/syng/sources/youtube.py index 5f90ae3..88ccac7 100644 --- a/syng/sources/youtube.py +++ b/syng/sources/youtube.py @@ -105,7 +105,7 @@ class YouTube: """ url = search_result["url"] cls.__cache__[url] = { - "duration": search_result["duration"], + "duration": int(search_result["duration"]), "title": search_result["title"], "channel": search_result["channel"], "url": url, @@ -352,6 +352,7 @@ class YoutubeSource(Source): title=result.title, artist=result.author, album="YouTube", + duration=result.length, ) for result in results ]