#pragma once #include "NetworkClient.hpp" #include "UrlShortener.hpp" #include "YtDlpManager.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Echoion::Core { class YouTubeClient { public: explicit YouTubeClient( std::shared_ptr networkClient); ~YouTubeClient(); // Initialize the client - downloads yt-dlp if needed Api::Result 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 updateYtDlp(); // Search for videos using YouTube Data API v3 (or yt-dlp as // fallback) [[nodiscard]] Api::Result search(std::string_view query, std::optional pageToken = std::nullopt); // Get video details including stream URLs using yt-dlp [[nodiscard]] Api::Result getVideoInfo(std::string_view videoId); // Get video details asynchronously [[nodiscard]] std::future> getVideoInfoAsync(std::string_view videoId); // Get multiple video details concurrently [[nodiscard]] std::vector> getVideoInfoBatch(const std::vector& videoIds); // Get playlist videos - uses YouTube API v2 if key available, // otherwise yt-dlp Then resolves each track individually via yt-dlp // for full metadata [[nodiscard]] Api::Result getPlaylist(std::string_view playlistId); // Get playlist videos asynchronously with concurrent track // resolution [[nodiscard]] std::future> getPlaylistAsync(std::string_view playlistId); // Stream playlist tracks with callback - calls trackCallback for // each track as it is resolved, enabling immediate playback of // first track void getPlaylistStreaming( std::string_view playlistId, std::function trackCallback, std::function onPlaylistInfo = nullptr, std::function onComplete = nullptr); // Extract video ID from various YouTube URL formats (handles // redirects for short URLs) [[nodiscard]] std::optional extractVideoId(std::string_view url); [[nodiscard]] static std::optional 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 getStreamUrl(std::string_view videoId); // Get best audio stream URL using yt-dlp [[nodiscard]] Api::Result getAudioStreamUrl(std::string_view videoId); // Configure cookie options for yt-dlp void setCookiesPath(const std::string& path); void setCookiesFromBrowser(const std::string& browser); // Configure proxy for yt-dlp void setProxyConfig(const std::optional& proxy); [[nodiscard]] std::optional getProxyConfig() const; // Set YouTube API key for enhanced playlist resolution (optional) void setApiKey(const std::string& apiKey); [[nodiscard]] bool hasApiKey() const; // Configure concurrent resolution settings void setMaxConcurrentResolutions(size_t max); [[nodiscard]] size_t getMaxConcurrentResolutions() const; // Enable/disable URL shortening for track links void setUrlShorteningEnabled(bool enabled); [[nodiscard]] bool isUrlShorteningEnabled() const; private: // Internal search implementations [[nodiscard]] Api::Result searchWithApi(std::string_view query, const std::optional& pageToken); [[nodiscard]] Api::Result searchWithYtDlp(std::string_view query, const std::optional& pageToken); // Fetch video durations for search results (batch API call) void fetchVideoDurations(std::vector& tracks, const std::vector& videoIds); // Internal methods for playlist resolution [[nodiscard]] Api::Result> fetchPlaylistItems(std::string_view playlistId); [[nodiscard]] Api::Result resolveTrackWithYtDlp(const Api::YouTubePlaylistItem& item); // Build YouTube API v3 URL for playlist items [[nodiscard]] std::string buildPlaylistApiUrl( std::string_view playlistId, const std::optional& pageToken = std::nullopt) const; std::shared_ptr networkClient_; std::unique_ptr ytDlpManager_; std::unique_ptr urlShortener_; // Worker pool backing the parallel API/resolution fan-outs. Bounds // thread reuse instead of spawning an OS thread per std::async call. Void::Concurrency::ThreadPool pool_; std::atomic initialized_{false}; mutable std::mutex mutex_; // Configuration std::optional apiKey_; mutable std::mutex configMutex_; size_t maxConcurrentResolutions_ = 4; bool urlShorteningEnabled_ = true; // Populate shortened URLs for tracks void populateShortenedUrls(std::vector& tracks); void populateShortenedUrl(Api::TrackInfo& track); }; } // namespace Echoion::Core