Initial commit
This commit is contained in:
commit
f6c5bcca39
52 files changed
+55449
No files matched your search
@@ -0,0 +1,274 @@
|
||||
#pragma once
|
||||
// Standard Library
|
||||
#include <expected>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// Third Party
|
||||
#include "cpr/cprtypes.h"
|
||||
#include "cpr/parameters.h"
|
||||
#include <curl/curl.h>
|
||||
#include <glaze/glaze.hpp> // Assumed available per your reference
|
||||
#include <quickjs.h>
|
||||
// Project (Assuming similar structure to your Spotify bot)
|
||||
#include <ts6bot/Proxy.hpp>
|
||||
|
||||
#include <PluginDefinitions.hpp>
|
||||
|
||||
namespace YouTube {
|
||||
|
||||
// ============================================================================
|
||||
// Data Types (Simplified for immediate usage)
|
||||
// ============================================================================
|
||||
|
||||
struct Thumbnail {
|
||||
std::string url;
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
struct Snippet {
|
||||
std::string publishedAt;
|
||||
std::string channelId;
|
||||
std::string title;
|
||||
std::string description;
|
||||
std::string channelTitle;
|
||||
std::map<std::string, Thumbnail> thumbnails;
|
||||
std::string liveBroadcastContent;
|
||||
std::string publishTime;
|
||||
};
|
||||
|
||||
struct ResourceId {
|
||||
std::string kind;
|
||||
std::string videoId;
|
||||
std::string channelId;
|
||||
std::string playlistId;
|
||||
};
|
||||
|
||||
// Search results use a wrapper for ID, Videos use a string ID
|
||||
struct SearchResultId {
|
||||
std::string kind;
|
||||
std::string videoId;
|
||||
std::string channelId;
|
||||
std::string playlistId;
|
||||
};
|
||||
|
||||
struct VideoContentDetails {
|
||||
std::string duration;
|
||||
std::string dimension;
|
||||
std::string definition;
|
||||
};
|
||||
|
||||
struct VideoStatistics {
|
||||
std::string viewCount;
|
||||
std::string likeCount;
|
||||
std::string commentCount;
|
||||
};
|
||||
|
||||
// Main Resource Types
|
||||
struct Video {
|
||||
std::string kind;
|
||||
std::string id;
|
||||
Snippet snippet;
|
||||
VideoContentDetails contentDetails;
|
||||
VideoStatistics statistics;
|
||||
};
|
||||
|
||||
struct SearchResult {
|
||||
std::string kind;
|
||||
std::string etag;
|
||||
SearchResultId id;
|
||||
Snippet snippet;
|
||||
};
|
||||
|
||||
// API Response Wrappers
|
||||
template <typename T> struct PageResult {
|
||||
std::string kind;
|
||||
std::string etag;
|
||||
std::string nextPageToken;
|
||||
std::string prevPageToken;
|
||||
std::string regionCode;
|
||||
struct PageInfo {
|
||||
int totalResults;
|
||||
int resultsPerPage;
|
||||
} pageInfo;
|
||||
std::vector<T> items;
|
||||
};
|
||||
// ============================================================================
|
||||
// InnerTube (Player) Data Types
|
||||
// ============================================================================
|
||||
|
||||
struct AudioFormat {
|
||||
int itag;
|
||||
std::string url; // Direct URL (if unencrypted)
|
||||
std::string signatureCipher; // Encrypted signature (if present, url is empty)
|
||||
std::string mimeType;
|
||||
int bitrate;
|
||||
std::string qualityLabel;
|
||||
};
|
||||
|
||||
struct StreamingData {
|
||||
std::string expiresInSeconds;
|
||||
std::vector<AudioFormat> formats; // Mixed video+audio
|
||||
std::vector<AudioFormat>
|
||||
adaptiveFormats; // Separate streams (we want audio from here)
|
||||
};
|
||||
|
||||
struct PlayerResponse {
|
||||
StreamingData streamingData;
|
||||
// ... other fields like playabilityStatus ignored for brevity
|
||||
};
|
||||
// ============================================================================
|
||||
// Error Handling
|
||||
// ============================================================================
|
||||
|
||||
struct ApiError {
|
||||
int status;
|
||||
std::string message;
|
||||
std::string reason;
|
||||
};
|
||||
struct GoogleError {
|
||||
struct Inner {
|
||||
int code;
|
||||
std::string message;
|
||||
} error;
|
||||
};
|
||||
template <typename T> using Result = std::expected<T, ApiError>;
|
||||
// ============================================================================
|
||||
// Request Parameters
|
||||
// ============================================================================
|
||||
|
||||
struct SearchParams {
|
||||
std::string q;
|
||||
std::vector<std::string> type = {"video"}; // video, channel, playlist
|
||||
std::string part = "snippet";
|
||||
int maxResults = 5;
|
||||
std::string order = "relevance";
|
||||
std::string pageToken = "";
|
||||
std::string videoDuration = "any"; // any, long, medium, short
|
||||
bool rawQuery = false; // Set to true to preserve -, |, etc. in query
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Main YouTube Client
|
||||
// ============================================================================
|
||||
|
||||
struct CurlDeleter {
|
||||
void operator()(CURL *ptr) const {
|
||||
if (ptr)
|
||||
curl_easy_cleanup(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
enum class HttpMethod { GET, POST, PUT, DELETE };
|
||||
|
||||
class Client {
|
||||
public:
|
||||
// YouTube typically uses API Keys for public data, OAuth for user data.
|
||||
// Pass "" as accessToken if only using API Key.
|
||||
Client(std::string apiKey, std::string accessToken = "");
|
||||
~Client();
|
||||
auto GetBestStreams(const std::string &videoId)
|
||||
-> Result<::definitions::Streams>; // Configuration
|
||||
void SetVerbose(bool verbose) { m_Verbose = verbose; }
|
||||
void SetProxy(ProxyConfig config) { m_Proxy = std::move(config); }
|
||||
void SetAccessToken(std::string token) { m_AccessToken = std::move(token); }
|
||||
void SetApiKey(std::string key) { m_ApiKey = std::move(key); }
|
||||
|
||||
// ========================================================================
|
||||
// Search API
|
||||
// ========================================================================
|
||||
|
||||
// https://developers.google.com/youtube/v3/docs/search/list
|
||||
auto Search(const SearchParams ¶ms) -> Result<PageResult<SearchResult>>;
|
||||
|
||||
// ========================================================================
|
||||
// Videos API
|
||||
// ========================================================================
|
||||
|
||||
// https://developers.google.com/youtube/v3/docs/videos/list
|
||||
auto GetVideo(const std::string &id,
|
||||
const std::string &part = "snippet,contentDetails,statistics")
|
||||
-> Result<Video>;
|
||||
|
||||
auto GetVideos(const std::vector<std::string> &ids,
|
||||
const std::string &part = "snippet,contentDetails,statistics")
|
||||
-> Result<std::vector<Video>>;
|
||||
|
||||
// ========================================================================
|
||||
// Channels API
|
||||
// ========================================================================
|
||||
|
||||
// https://developers.google.com/youtube/v3/docs/channels/list
|
||||
auto GetChannel(const std::string &id,
|
||||
const std::string &part = "snippet,statistics")
|
||||
-> Result<std::string>; // Returns raw JSON for brevity in this example
|
||||
|
||||
// ========================================================================
|
||||
// Playlists API
|
||||
// ========================================================================
|
||||
|
||||
// https://developers.google.com/youtube/v3/docs/playlistItems/list
|
||||
auto GetPlaylistItems(const std::string &playlistId,
|
||||
const std::string &pageToken = "")
|
||||
-> Result<PageResult<SearchResult>>; // Reusing SearchResult as structure
|
||||
// is similar enough for snippet
|
||||
// ========================================================================
|
||||
// Player API (The "Extraction" Logic)
|
||||
// ========================================================================
|
||||
|
||||
// Returns a raw https://googlevideo.com/... URL for the audio stream.
|
||||
// Returns error if the video is region locked or requires signature
|
||||
// deciphering.
|
||||
auto GetAudioStreamUrl(const std::string &videoId) -> Result<std::string>;
|
||||
|
||||
auto ExtractVideoId(std::string_view url) const -> std::optional<std::string>;
|
||||
|
||||
private:
|
||||
std::string m_ApiKey;
|
||||
std::string m_AccessToken;
|
||||
ProxyConfig m_Proxy;
|
||||
bool m_Verbose = false;
|
||||
const std::string m_BaseUrl = "https://www.googleapis.com/youtube/v3";
|
||||
JSRuntime *m_rt;
|
||||
JSContext *m_ctx;
|
||||
std::string m_preprocessedPlayer;
|
||||
|
||||
// Helper methods for the solver logic
|
||||
void InitSolver();
|
||||
void SetPlayerJs(const std::string &js);
|
||||
std::string SolveChallenge(const std::string &challenge,
|
||||
const std::string &type);
|
||||
auto PerformRequest(
|
||||
const std::string &purl, const std::string &endpoint,
|
||||
const cpr::Parameters ¶ms = {},
|
||||
const cpr::Header &headers =
|
||||
{{"Content-Type", "application/json"},
|
||||
{"User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}},
|
||||
HttpMethod method = HttpMethod::GET, const std::string &body = "")
|
||||
-> Result<std::string>;
|
||||
|
||||
auto PerformRequest(
|
||||
const std::string &endpoint, const cpr::Parameters ¶ms = {},
|
||||
const cpr::Header &headers =
|
||||
{{"Content-Type", "application/json"},
|
||||
{"User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}},
|
||||
HttpMethod method = HttpMethod::GET, const std::string &body = "")
|
||||
-> Result<std::string>;
|
||||
// Helper specifically for the internal YouTube API (different Base URL)
|
||||
auto PerformInternalRequest(const std::string &endpoint,
|
||||
const std::string &jsonBody)
|
||||
-> Result<std::string>;
|
||||
static auto WriteCallback(void *contents, size_t size, size_t nmemb,
|
||||
std::string *out) -> size_t;
|
||||
auto UrlEncode(std::string_view value) const -> std::string;
|
||||
auto UrlEncodePreservingOperators(std::string_view query) const
|
||||
-> std::string;
|
||||
};
|
||||
|
||||
} // namespace YouTube
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user