Implement reload commands; fix SOCKS5 playback; API->yt-dlp search fallback
F1 (reload commands): Core registers itself as Api::ICoreInterface in the service registry; !reloadconfig and !reload resolve it via ctx.service and call Core::reloadConfig() (which reloads config.json from disk), replacing the old placeholder replies. F2 (SOCKS5 playback): ffmpeg can't use a SOCKS5 proxy, so it connected directly to the IP-locked googlevideo URL and timed out; the fallback then buffered the whole (705 MB) body in memory and tripped Void::Net's body limit. New YouTubeClient::downloadAudioToFile() re-downloads the audio with yt-dlp (which handles SOCKS5 + writes to disk) by video id and ffmpeg opens the local file. MusicManager::downloadAudioForPlayback now uses it, and the play-open path short-circuits straight to the download when the proxy is SOCKS5 (skipping the doomed ~15 s ffmpeg attempt); HTTP/no-proxy keeps stream-first-then-download. F3 (API fallback): YouTubeClient::search() now falls back to yt-dlp when the Data API path errors (bad/expired key, quota, network), mirroring what fetchPlaylistItems already did. Build green; runtime behaviour to be verified on the live bot. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
1efdbe0771
commit
499143050b
8 files changed
+190
-81
No files matched your search
+84
-1
@@ -7,6 +7,7 @@
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <future>
|
||||
#include <iomanip>
|
||||
@@ -226,6 +227,77 @@ namespace Echoion::Core
|
||||
Api::ErrorCode::NotFound, "Failed to get audio URL from yt-dlp");
|
||||
}
|
||||
|
||||
Api::Result<std::string>
|
||||
YouTubeClient::downloadAudioToFile(std::string_view videoIdOrUrl,
|
||||
const std::string& outDir)
|
||||
{
|
||||
if (!initialized_ || !ytDlpManager_->isAvailable()) {
|
||||
return Api::fail(Api::ErrorCode::ServiceUnavailable,
|
||||
"yt-dlp not available");
|
||||
}
|
||||
if (videoIdOrUrl.empty()) {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"Video ID/URL is empty");
|
||||
}
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(outDir, ec);
|
||||
|
||||
const std::string id(videoIdOrUrl);
|
||||
const std::string outTemplate = outDir + "/%(id)s.%(ext)s";
|
||||
|
||||
auto startTime = std::chrono::steady_clock::now();
|
||||
Void::Log::info(
|
||||
"YouTubeClient", "{}",
|
||||
std::format("Downloading audio via yt-dlp: {}", id));
|
||||
|
||||
// yt-dlp downloads through the configured proxy + cookies and writes to
|
||||
// disk; --print after_move:filepath emits the final path on stdout.
|
||||
auto future = ytDlpManager_->executeAsync(
|
||||
{id, "-f", "bestaudio/251/140", "-o", outTemplate, "--no-playlist",
|
||||
"--no-warnings", "--print", "after_move:filepath"});
|
||||
|
||||
auto waitStatus = future.wait_for(std::chrono::seconds(180));
|
||||
if (waitStatus == std::future_status::timeout) {
|
||||
return Api::fail(Api::ErrorCode::NetworkError,
|
||||
"yt-dlp audio download timed out");
|
||||
}
|
||||
|
||||
auto ytdlpResult = future.get();
|
||||
if (!ytdlpResult.success) {
|
||||
return Api::fail(Api::ErrorCode::NetworkError,
|
||||
"yt-dlp audio download failed");
|
||||
}
|
||||
|
||||
// The emitted path is the last non-empty stdout line.
|
||||
std::string path;
|
||||
{
|
||||
std::istringstream stream(ytdlpResult.output);
|
||||
for (std::string line; std::getline(stream, line);) {
|
||||
while (!line.empty() &&
|
||||
(line.back() == '\r' || line.back() == '\n')) {
|
||||
line.pop_back();
|
||||
}
|
||||
if (!line.empty()) {
|
||||
path = line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (path.empty() || !std::filesystem::exists(path)) {
|
||||
return Api::fail(Api::ErrorCode::NotFound,
|
||||
"yt-dlp reported no downloaded file");
|
||||
}
|
||||
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - startTime)
|
||||
.count();
|
||||
Void::Log::info("YouTubeClient", "{}",
|
||||
std::format("Downloaded audio to {} in {} ms", path,
|
||||
elapsed));
|
||||
return path;
|
||||
}
|
||||
|
||||
Api::Result<std::string>
|
||||
YouTubeClient::getStreamUrl(std::string_view videoId)
|
||||
{
|
||||
@@ -365,9 +437,20 @@ namespace Echoion::Core
|
||||
|
||||
// Use YouTube Data API v3 if key is available
|
||||
if (hasApiKey()) {
|
||||
Void::Log::info("YouTubeClient", "{}",
|
||||
Void::Log::info("YouTubeClient", "{}",
|
||||
std::format("Searching YouTube via API: '{}'", query));
|
||||
result = searchWithApi(query, pageToken);
|
||||
|
||||
// Fall back to yt-dlp if the API path failed (bad/expired key,
|
||||
// quota exceeded, network error, etc.).
|
||||
if (!result) {
|
||||
Void::Log::warn(
|
||||
"YouTubeClient", "{}",
|
||||
std::format("YouTube API search failed ({}), falling back "
|
||||
"to yt-dlp",
|
||||
result.error().message));
|
||||
result = searchWithYtDlp(query, pageToken);
|
||||
}
|
||||
} else {
|
||||
// Fallback to yt-dlp if no API key
|
||||
Void::Log::warn("YouTubeClient", "{}", "No YouTube API key configured, falling back "
|
||||
|
||||
Reference in new issue
Block a user