Migrate core infrastructure to VoidLibrary
This commit is contained in:
1 parent
53801f2b53
commit
2bdb63766d
65 files changed
+16109
-12011
No files matched your search
@@ -3,8 +3,11 @@
|
||||
#include "NetworkClient.hpp"
|
||||
#include "UrlShortener.hpp"
|
||||
#include "YtDlpManager.hpp"
|
||||
#include <NetworkTypes.hpp>
|
||||
#include <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <YouTubeTypes.hpp>
|
||||
#include <chrono>
|
||||
#include <echoion/api.hpp>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <memory>
|
||||
@@ -15,142 +18,159 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Core {
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
class YouTubeClient {
|
||||
public:
|
||||
explicit YouTubeClient(std::shared_ptr<NetworkClient> networkClient);
|
||||
~YouTubeClient();
|
||||
class YouTubeClient
|
||||
{
|
||||
public:
|
||||
|
||||
// Initialize the client - downloads yt-dlp if needed
|
||||
Api::Result<void> initialize();
|
||||
void shutdown();
|
||||
explicit YouTubeClient(
|
||||
std::shared_ptr<NetworkClient> networkClient);
|
||||
~YouTubeClient();
|
||||
|
||||
// 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();
|
||||
// Initialize the client - downloads yt-dlp if needed
|
||||
Api::Result<void> initialize();
|
||||
void shutdown();
|
||||
|
||||
// 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);
|
||||
// Check if yt-dlp is available
|
||||
[[nodiscard]] bool isAvailable() const;
|
||||
|
||||
// Get video details including stream URLs using yt-dlp
|
||||
[[nodiscard]] Api::Result<Api::TrackInfo> getVideoInfo(std::string_view videoId);
|
||||
// Get yt-dlp version
|
||||
[[nodiscard]] std::string getYtDlpVersion() const;
|
||||
|
||||
// Get video details asynchronously
|
||||
[[nodiscard]] std::future<Api::Result<Api::TrackInfo>>
|
||||
getVideoInfoAsync(std::string_view videoId);
|
||||
// Force update yt-dlp
|
||||
Api::Result<void> updateYtDlp();
|
||||
|
||||
// Get multiple video details concurrently
|
||||
[[nodiscard]] std::vector<Api::Result<Api::TrackInfo>>
|
||||
getVideoInfoBatch(const std::vector<std::string>& videoIds);
|
||||
// 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 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 video details including stream URLs using yt-dlp
|
||||
[[nodiscard]] Api::Result<Api::TrackInfo>
|
||||
getVideoInfo(std::string_view videoId);
|
||||
|
||||
// Get playlist videos asynchronously with concurrent track resolution
|
||||
[[nodiscard]] std::future<Api::Result<Api::PlaylistInfo>>
|
||||
getPlaylistAsync(std::string_view playlistId);
|
||||
// Get video details asynchronously
|
||||
[[nodiscard]] std::future<Api::Result<Api::TrackInfo>>
|
||||
getVideoInfoAsync(std::string_view videoId);
|
||||
|
||||
// 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);
|
||||
// Get multiple video details concurrently
|
||||
[[nodiscard]] std::vector<Api::Result<Api::TrackInfo>>
|
||||
getVideoInfoBatch(const std::vector<std::string>& videoIds);
|
||||
|
||||
// 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);
|
||||
// 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);
|
||||
|
||||
// Check if URL is a YouTube URL
|
||||
[[nodiscard]] static bool isYouTubeUrl(std::string_view url);
|
||||
// Get playlist videos asynchronously with concurrent track
|
||||
// resolution
|
||||
[[nodiscard]] std::future<Api::Result<Api::PlaylistInfo>>
|
||||
getPlaylistAsync(std::string_view playlistId);
|
||||
|
||||
// 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);
|
||||
// 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);
|
||||
|
||||
// Configure proxy for yt-dlp
|
||||
void setProxyConfig(const std::optional<Api::ProxyConfig>& proxy);
|
||||
[[nodiscard]] std::optional<Api::ProxyConfig> getProxyConfig() const;
|
||||
// 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);
|
||||
|
||||
// Set YouTube API key for enhanced playlist resolution (optional)
|
||||
void setApiKey(const std::string& apiKey);
|
||||
[[nodiscard]] bool hasApiKey() const;
|
||||
// Check if URL is a YouTube URL
|
||||
[[nodiscard]] static bool isYouTubeUrl(std::string_view url);
|
||||
|
||||
// Configure concurrent resolution settings
|
||||
void setMaxConcurrentResolutions(size_t max);
|
||||
[[nodiscard]] size_t getMaxConcurrentResolutions() const;
|
||||
// Get stream URL for a video (used by MusicManager)
|
||||
[[nodiscard]] Api::Result<std::string>
|
||||
getStreamUrl(std::string_view videoId);
|
||||
|
||||
// Enable/disable URL shortening for track links
|
||||
void setUrlShorteningEnabled(bool enabled);
|
||||
[[nodiscard]] bool isUrlShorteningEnabled() const;
|
||||
// Get best audio stream URL using yt-dlp
|
||||
[[nodiscard]] Api::Result<std::string>
|
||||
getAudioStreamUrl(std::string_view videoId);
|
||||
|
||||
private:
|
||||
// Configure cookie options for yt-dlp
|
||||
void setCookiesPath(const std::string& path);
|
||||
void setCookiesFromBrowser(const std::string& browser);
|
||||
|
||||
// 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);
|
||||
// Configure proxy for yt-dlp
|
||||
void setProxyConfig(const std::optional<Api::ProxyConfig>& proxy);
|
||||
[[nodiscard]] std::optional<Api::ProxyConfig>
|
||||
getProxyConfig() const;
|
||||
|
||||
// Fetch video durations for search results (batch API call)
|
||||
void fetchVideoDurations(std::vector<Api::TrackInfo>& tracks,
|
||||
const std::vector<std::string>& videoIds);
|
||||
// Set YouTube API key for enhanced playlist resolution (optional)
|
||||
void setApiKey(const std::string& apiKey);
|
||||
[[nodiscard]] bool hasApiKey() const;
|
||||
|
||||
// Parse ISO 8601 duration string
|
||||
[[nodiscard]] static std::chrono::seconds
|
||||
parseIso8601Duration(const std::string& duration);
|
||||
// Configure concurrent resolution settings
|
||||
void setMaxConcurrentResolutions(size_t max);
|
||||
[[nodiscard]] size_t getMaxConcurrentResolutions() const;
|
||||
|
||||
// URL encode/decode helpers
|
||||
[[nodiscard]] static std::string urlEncode(std::string_view value);
|
||||
[[nodiscard]] static std::string decodeHtmlEntities(std::string_view value);
|
||||
// Enable/disable URL shortening for track links
|
||||
void setUrlShorteningEnabled(bool enabled);
|
||||
[[nodiscard]] bool isUrlShorteningEnabled() const;
|
||||
|
||||
// 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);
|
||||
private:
|
||||
|
||||
// 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;
|
||||
// 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);
|
||||
|
||||
std::shared_ptr<NetworkClient> networkClient_;
|
||||
std::unique_ptr<YtDlpManager> ytDlpManager_;
|
||||
std::unique_ptr<UrlShortener> urlShortener_;
|
||||
// Fetch video durations for search results (batch API call)
|
||||
void fetchVideoDurations(std::vector<Api::TrackInfo>& tracks,
|
||||
const std::vector<std::string>& videoIds);
|
||||
|
||||
std::atomic<bool> initialized_{false};
|
||||
mutable std::mutex mutex_;
|
||||
// Parse ISO 8601 duration string
|
||||
[[nodiscard]] static std::chrono::seconds
|
||||
parseIso8601Duration(const std::string& duration);
|
||||
|
||||
// Configuration
|
||||
std::optional<std::string> apiKey_;
|
||||
mutable std::mutex configMutex_;
|
||||
size_t maxConcurrentResolutions_ = 4;
|
||||
bool urlShorteningEnabled_ = true;
|
||||
// URL encode/decode helpers
|
||||
[[nodiscard]] static std::string urlEncode(std::string_view value);
|
||||
[[nodiscard]] static std::string
|
||||
decodeHtmlEntities(std::string_view value);
|
||||
|
||||
// Populate shortened URLs for tracks
|
||||
void populateShortenedUrls(std::vector<Api::TrackInfo>& tracks);
|
||||
void populateShortenedUrl(Api::TrackInfo& track);
|
||||
};
|
||||
// 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
|
||||
Reference in new issue
Block a user