diff options
Diffstat (limited to 'include/emotespp')
| -rw-r--r-- | include/emotespp/betterttv.hpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/include/emotespp/betterttv.hpp b/include/emotespp/betterttv.hpp new file mode 100644 index 0000000..abf7e7f --- /dev/null +++ b/include/emotespp/betterttv.hpp @@ -0,0 +1,96 @@ +#ifdef BUILD_BETTERTTV +#pragma once + +#include <algorithm> +#include <map> +#include <nlohmann/json.hpp> +#include <stdexcept> +#include <string> +#include <vector> + +#include "cpr/cpr.h" +#include "emotespp/emotes.hpp" +#include "ixwebsocket/IXWebSocket.h" + +namespace emotespp { + class BetterTTVWebsocketClient : public RetrieveEmoteWebsocket<Emote> { + 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<std::string, std::vector<Emote>> &get_ids() const; + + private: + std::map<std::string, std::vector<Emote>> ids; + ix::WebSocket websocket; + + bool is_connected = false; + }; + + class BetterTTVAPIClient : public RetrieveEmoteAPI<Emote> { + public: + BetterTTVAPIClient() = default; + ~BetterTTVAPIClient() = default; + + std::vector<Emote> 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<Emote> 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<Emote> 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<Emote> 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
\ No newline at end of file |
