Add yt search results from client search to YouTube Cache

This commit is contained in:
Christoph Stahl 2024-09-22 22:02:34 +02:00
parent bf104362ea
commit 1f1c2c4f1e
3 changed files with 23 additions and 5 deletions

View file

@ -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

View file

@ -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)

View file

@ -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
]