Files
EchoionCore/include/echoion/core/YouTubeClient.hpp
T
2026-02-03 05:07:15 +03:00

157 lines
5.4 KiB
C++

#pragma once
#include "NetworkClient.hpp"
#include "UrlShortener.hpp"
#include "YtDlpManager.hpp"
#include <chrono>
#include <echoion/api.hpp>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <optional>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>
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 YouTube Data API v3 (or yt-dlp as fallback)
[[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 video details asynchronously
[[nodiscard]] std::future<Api::Result<Api::TrackInfo>>
getVideoInfoAsync(std::string_view videoId);
// Get multiple video details concurrently
[[nodiscard]] std::vector<Api::Result<Api::TrackInfo>>
getVideoInfoBatch(const std::vector<std::string>& 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<Api::PlaylistInfo>
getPlaylist(std::string_view playlistId);
// Get playlist videos asynchronously with concurrent track resolution
[[nodiscard]] std::future<Api::Result<Api::PlaylistInfo>>
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<void(Api::TrackInfo&&)> trackCallback,
std::function<void(size_t totalTracks)> onPlaylistInfo = nullptr,
std::function<void()> onComplete = nullptr);
// 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);
// Configure proxy for yt-dlp
void setProxyConfig(const std::optional<Api::ProxyConfig>& proxy);
[[nodiscard]] std::optional<Api::ProxyConfig> 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<Api::SearchResult>
searchWithApi(std::string_view query,
const std::optional<std::string>& pageToken);
[[nodiscard]] Api::Result<Api::SearchResult>
searchWithYtDlp(std::string_view query,
const std::optional<std::string>& pageToken);
// Fetch video durations for search results (batch API call)
void fetchVideoDurations(std::vector<Api::TrackInfo>& tracks,
const std::vector<std::string>& videoIds);
// Parse ISO 8601 duration string
[[nodiscard]] static std::chrono::seconds
parseIso8601Duration(const std::string& duration);
// URL encode/decode helpers
[[nodiscard]] static std::string urlEncode(std::string_view value);
[[nodiscard]] static std::string decodeHtmlEntities(std::string_view value);
// Internal methods for playlist resolution
[[nodiscard]] Api::Result<std::vector<Api::YouTubePlaylistItem>>
fetchPlaylistItems(std::string_view playlistId);
[[nodiscard]] Api::Result<Api::TrackInfo>
resolveTrackWithYtDlp(const Api::YouTubePlaylistItem& item);
// Build YouTube API v3 URL for playlist items
[[nodiscard]] std::string buildPlaylistApiUrl(
std::string_view playlistId,
const std::optional<std::string>& pageToken = std::nullopt) const;
std::shared_ptr<NetworkClient> networkClient_;
std::unique_ptr<YtDlpManager> ytDlpManager_;
std::unique_ptr<UrlShortener> urlShortener_;
std::atomic<bool> initialized_{false};
mutable std::mutex mutex_;
// Configuration
std::optional<std::string> apiKey_;
mutable std::mutex configMutex_;
size_t maxConcurrentResolutions_ = 4;
bool urlShorteningEnabled_ = true;
// Populate shortened URLs for tracks
void populateShortenedUrls(std::vector<Api::TrackInfo>& tracks);
void populateShortenedUrl(Api::TrackInfo& track);
};
} // namespace Echoion::Core