#ifdef BUILD_BETTERTTV #pragma once #include #include #include #include #include #include #include "cpr/cpr.h" #include "emotespp/emotes.hpp" #include "ixwebsocket/IXWebSocket.h" namespace emotespp { class BetterTTVWebsocketClient : public RetrieveEmoteWebsocket { public: BetterTTVWebsocketClient(); void subscribe_emote_set(const std::string &emote_set_id); void unsubscribe_emote_set(const std::string &emote_set_id); void start(); const std::map> &get_ids() const; private: std::map> ids; ix::WebSocket websocket; bool is_connected = false; }; class BetterTTVAPIClient : public RetrieveEmoteAPI { public: BetterTTVAPIClient() = default; ~BetterTTVAPIClient() = default; std::vector get_channel_emotes( std::string &channel_id) const override { cpr::Response r = cpr::Get(cpr::Url{base_url + "/cached/users/twitch/" + channel_id}); if (r.status_code != 200) { throw std::runtime_error( "Failed to get channel emotes. Status code: " + std::to_string(r.status_code)); } nlohmann::json j = nlohmann::json::parse(r.text); std::vector emotes; nlohmann::json channel_emotes = j["channelEmotes"]; std::for_each(channel_emotes.begin(), channel_emotes.end(), [this, &emotes](const nlohmann::json &v) { emotes.push_back(this->parse_emote(v)); }); nlohmann::json shared_emotes = j["sharedEmotes"]; std::for_each(shared_emotes.begin(), shared_emotes.end(), [this, &emotes](const nlohmann::json &v) { emotes.push_back(this->parse_emote(v)); }); return emotes; } std::vector get_global_emotes() const override { cpr::Response r = cpr::Get(cpr::Url{base_url + "/emote-sets/global"}); if (r.status_code != 200) { throw std::runtime_error( "Failed to get global emotes. Status code: " + std::to_string(r.status_code)); } nlohmann::json j = nlohmann::json::parse(r.text); std::vector emotes; std::for_each(j.begin(), j.end(), [this, &emotes](const nlohmann::json &v) { emotes.push_back(this->parse_emote(v)); }); return emotes; } private: Emote parse_emote(const nlohmann::json &j) const; const std::string base_url = "https://api.betterttv.net/3"; }; } #endif