1 parent
d146f11f02
commit
5c036d91fb
72 files changed
+5875
-1243
No files matched your search
+135
-11
@@ -5,6 +5,7 @@
|
||||
#include <Void/Text/Url.hpp>
|
||||
#include <Void/Time/Format.hpp>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
@@ -12,6 +13,7 @@
|
||||
#include <future>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
@@ -155,7 +157,11 @@ namespace Echoion::Core
|
||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"yt-dlp manager not initialized");
|
||||
}
|
||||
return ytDlpManager_->update();
|
||||
auto result = ytDlpManager_->update();
|
||||
if (!result) {
|
||||
return std::unexpected(result.error());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Api::Result<std::string>
|
||||
@@ -275,6 +281,7 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
std::string url = result.output;
|
||||
Void::Log::debug("YouTubeClient", "{}", url);
|
||||
// -g may print multiple lines (video+audio); take the first.
|
||||
if (auto nl = url.find('\n'); nl != std::string::npos) {
|
||||
url.resize(nl);
|
||||
@@ -485,8 +492,9 @@ namespace Echoion::Core
|
||||
return results;
|
||||
}
|
||||
|
||||
Api::Result<Api::SearchResult> YouTubeClient::search(
|
||||
std::string_view query, std::optional<std::string> pageToken)
|
||||
Api::Result<Api::SearchResult>
|
||||
YouTubeClient::search(std::string_view query,
|
||||
std::optional<std::string> pageToken)
|
||||
{
|
||||
|
||||
if (!initialized_) {
|
||||
@@ -831,7 +839,23 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
Api::Result<Api::SearchResult> YouTubeClient::searchWithYtDlp(
|
||||
std::string_view query, const std::optional<std::string>& pageToken)
|
||||
std::string_view query,
|
||||
const std::optional<std::string>& /*pageToken*/)
|
||||
{
|
||||
return searchWithYtDlpProvider(query, "ytsearch5",
|
||||
Api::TrackSource::YouTube);
|
||||
}
|
||||
|
||||
Api::Result<Api::SearchResult>
|
||||
YouTubeClient::searchSoundCloud(std::string_view query)
|
||||
{
|
||||
return searchWithYtDlpProvider(query, "scsearch5",
|
||||
Api::TrackSource::SoundCloud);
|
||||
}
|
||||
|
||||
Api::Result<Api::SearchResult> YouTubeClient::searchWithYtDlpProvider(
|
||||
std::string_view query, std::string_view searchPrefix,
|
||||
Api::TrackSource source)
|
||||
{
|
||||
|
||||
if (!ytDlpManager_->isAvailable()) {
|
||||
@@ -842,8 +866,8 @@ namespace Echoion::Core
|
||||
Void::Log::debug("YouTubeClient", "{}", "Starting yt-dlp search...");
|
||||
auto searchStart = std::chrono::steady_clock::now();
|
||||
|
||||
// yt-dlp search format: ytsearch5:query
|
||||
std::string searchQuery = std::format("ytsearch5:{}", query);
|
||||
// yt-dlp search format, e.g. ytsearch5:query or scsearch5:query
|
||||
std::string searchQuery = std::format("{}:{}", searchPrefix, query);
|
||||
|
||||
auto result = ytDlpManager_->executeAsync(
|
||||
{searchQuery, "-J", "--flat-playlist", "--no-warnings"});
|
||||
@@ -908,11 +932,16 @@ namespace Echoion::Core
|
||||
if ((entry).has("thumbnail")) {
|
||||
track.thumbnailUrl = (entry).get_str("thumbnail");
|
||||
}
|
||||
// Set originalUrl so we can extract video ID later for
|
||||
// streaming
|
||||
track.originalUrl = std::format(
|
||||
"https://www.youtube.com/watch?v={}", track.id);
|
||||
track.source = Api::TrackSource::YouTube;
|
||||
// Set originalUrl so the track can be resolved later. For
|
||||
// YouTube it's the watch URL (id is the video id); other
|
||||
// providers (SoundCloud) carry the permalink in `url`.
|
||||
if (source == Api::TrackSource::YouTube) {
|
||||
track.originalUrl = std::format(
|
||||
"https://www.youtube.com/watch?v={}", track.id);
|
||||
} else if ((entry).has("url")) {
|
||||
track.originalUrl = (entry).get_str("url");
|
||||
}
|
||||
track.source = source;
|
||||
|
||||
searchResult.tracks.push_back(track);
|
||||
}
|
||||
@@ -1019,6 +1048,101 @@ namespace Echoion::Core
|
||||
}
|
||||
}
|
||||
|
||||
Api::Result<std::vector<Api::TrackInfo>>
|
||||
YouTubeClient::randomMusicVideo(size_t limit)
|
||||
{
|
||||
if (!ytDlpManager_->isAvailable()) {
|
||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"yt-dlp not available");
|
||||
}
|
||||
|
||||
// Curated music-oriented queries so a plain ytsearch skews toward
|
||||
// actual music rather than talk/gaming clips. One is picked at random
|
||||
// per call; combined with the shuffled results this gives the radio a
|
||||
// varied backstop when no related/unplayed track is available.
|
||||
static const std::array<std::string_view, 10> kQueries = {
|
||||
"popular music", "top hits", "rock music",
|
||||
"electronic music", "hip hop music", "lofi hip hop",
|
||||
"pop music", "jazz music", "indie music",
|
||||
"dance music"};
|
||||
|
||||
static thread_local std::mt19937 rng(std::random_device{}());
|
||||
std::uniform_int_distribution<size_t> pick(0, kQueries.size() - 1);
|
||||
const std::string_view query = kQueries[pick(rng)];
|
||||
|
||||
// ytsearch<N>: returns the top N results for the query as a flat
|
||||
// playlist — cheap metadata only, same as radioMix.
|
||||
const std::string searchUrl =
|
||||
std::format("ytsearch{}:{}", limit + 10, query);
|
||||
auto future = ytDlpManager_->executeAsync(
|
||||
{searchUrl, "-J", "--flat-playlist", "--no-warnings"});
|
||||
|
||||
auto waitStatus = future.wait_for(std::chrono::seconds(30));
|
||||
if (waitStatus == std::future_status::timeout) {
|
||||
return Api::fail(Api::ErrorCode::NetworkError,
|
||||
"yt-dlp random music search timed out");
|
||||
}
|
||||
auto ytdlpResult = future.get();
|
||||
if (!ytdlpResult.success || ytdlpResult.output.empty()) {
|
||||
return Api::fail(Api::ErrorCode::NetworkError,
|
||||
"yt-dlp random music search failed");
|
||||
}
|
||||
|
||||
try {
|
||||
auto parsed = Void::Json::parse(ytdlpResult.output);
|
||||
if (!parsed) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::ParseError,
|
||||
"Failed to parse yt-dlp random music search output");
|
||||
}
|
||||
const JValue& json = *parsed;
|
||||
|
||||
std::vector<Api::TrackInfo> tracks;
|
||||
if (const JValue* entries = (json).child_array("entries")) {
|
||||
for (const JValue& entry : entries->as_array()) {
|
||||
if (!(entry).has("id")) {
|
||||
continue;
|
||||
}
|
||||
Api::TrackInfo track;
|
||||
track.id = (entry).get_str("id");
|
||||
if ((entry).has("title")) {
|
||||
track.title =
|
||||
Void::Text::html_unescape((entry).get_str("title"));
|
||||
}
|
||||
if ((entry).has("uploader")) {
|
||||
track.artist = Void::Text::html_unescape(
|
||||
(entry).get_str("uploader"));
|
||||
}
|
||||
if ((entry).has("duration")) {
|
||||
track.duration = std::chrono::seconds(
|
||||
static_cast<int64_t>((entry).get_num("duration")));
|
||||
}
|
||||
if ((entry).has("thumbnail")) {
|
||||
track.thumbnailUrl = (entry).get_str("thumbnail");
|
||||
}
|
||||
track.requestedBy = "radio\\autoplay";
|
||||
track.addedAt = std::chrono::system_clock::now();
|
||||
track.originalUrl = std::format(
|
||||
"https://www.youtube.com/watch?v={}", track.id);
|
||||
track.source = Api::TrackSource::YouTube;
|
||||
tracks.push_back(std::move(track));
|
||||
}
|
||||
}
|
||||
|
||||
// Shuffle so the caller's "take the first acceptable one" pick is
|
||||
// effectively random across the result set.
|
||||
std::shuffle(tracks.begin(), tracks.end(), rng);
|
||||
populateShortenedUrls(tracks);
|
||||
return tracks;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
return Api::fail(
|
||||
Api::ErrorCode::ParseError,
|
||||
std::format("Exception parsing yt-dlp random music search: {}",
|
||||
e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
Api::Result<Api::PlaylistInfo>
|
||||
YouTubeClient::getPlaylist(std::string_view playlistId)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user