Files
EchoionCore/include/echoion/core/YouTubeClient.hpp
T
2026-02-01 23:04:49 +03:00

68 lines
2.1 KiB
C++

#pragma once
#include <echoion/api.hpp>
#include "NetworkClient.hpp"
#include "YtDlpManager.hpp"
#include <regex>
#include <mutex>
#include <optional>
#include <string>
#include <memory>
namespace Echoion::Core {
class YouTubeClient {
public:
explicit YouTubeClient(std::shared_ptr<NetworkClient> networkClient);
~YouTubeClient();
// Initialize the client - downloads yt-dlp if needed
Api::Result<void> initialize();
void shutdown();
// Check if yt-dlp is available
[[nodiscard]] bool isAvailable() const;
// Get yt-dlp version
[[nodiscard]] std::string getYtDlpVersion() const;
// Force update yt-dlp
Api::Result<void> updateYtDlp();
// Search for videos using yt-dlp
[[nodiscard]] Api::Result<Api::SearchResult> search(std::string_view query,
std::optional<std::string> pageToken = std::nullopt);
// Get video details including stream URLs using yt-dlp
[[nodiscard]] Api::Result<Api::TrackInfo> getVideoInfo(std::string_view videoId);
// Get playlist videos using yt-dlp
[[nodiscard]] Api::Result<Api::PlaylistInfo> getPlaylist(std::string_view playlistId);
// Extract video ID from various YouTube URL formats (handles redirects for short URLs)
[[nodiscard]] std::optional<std::string> extractVideoId(std::string_view url);
[[nodiscard]] static std::optional<std::string> extractPlaylistId(std::string_view url);
// Check if URL is a YouTube URL
[[nodiscard]] static bool isYouTubeUrl(std::string_view url);
// Get stream URL for a video (used by MusicManager)
[[nodiscard]] Api::Result<std::string> getStreamUrl(std::string_view videoId);
// Get best audio stream URL using yt-dlp
[[nodiscard]] Api::Result<std::string> getAudioStreamUrl(std::string_view videoId);
// Configure cookie options for yt-dlp
void setCookiesPath(const std::string& path);
void setCookiesFromBrowser(const std::string& browser);
private:
std::shared_ptr<NetworkClient> networkClient_;
std::unique_ptr<YtDlpManager> ytDlpManager_;
std::atomic<bool> initialized_{false};
mutable std::mutex mutex_;
};
} // namespace Echoion::Core