diff options
| author | moderndevslulw <moderndevslulw@alright.party> | 2025-04-04 14:30:36 +0500 |
|---|---|---|
| committer | moderndevslulw <moderndevslulw@alright.party> | 2025-04-04 14:30:36 +0500 |
| commit | a44fc682a5f2eb54dfac6705ff21d0778639af6a (patch) | |
| tree | a7b3d624bf89ed5b05a6890a00c40e22d9445b45 /include/emotespp | |
| parent | b19cee38da6bcdbaa8cd3a4115c0f24e7dadc9cd (diff) | |
feat: 7TV API client
Diffstat (limited to 'include/emotespp')
| -rw-r--r-- | include/emotespp/emotes.hpp | 9 | ||||
| -rw-r--r-- | include/emotespp/seventv.hpp | 62 |
2 files changed, 71 insertions, 0 deletions
diff --git a/include/emotespp/emotes.hpp b/include/emotespp/emotes.hpp index 5a5e172..4f08f39 100644 --- a/include/emotespp/emotes.hpp +++ b/include/emotespp/emotes.hpp @@ -3,6 +3,7 @@ #include <functional> #include <optional> #include <string> +#include <vector> namespace emotespp { struct Emote { @@ -45,4 +46,12 @@ namespace emotespp { std::function<void(std::string, std::optional<std::string>, T)>> emote_update; }; + + template <typename T> + class RetrieveEmoteAPI { + public: + virtual std::vector<T> get_channel_emotes( + std::string &channel_id) const = 0; + virtual std::vector<T> get_global_emotes() const = 0; + }; }
\ No newline at end of file diff --git a/include/emotespp/seventv.hpp b/include/emotespp/seventv.hpp index cce8ae7..77db912 100644 --- a/include/emotespp/seventv.hpp +++ b/include/emotespp/seventv.hpp @@ -1,9 +1,12 @@ #pragma once #include <nlohmann/json.hpp> +#include <optional> +#include <stdexcept> #include <string> #include <vector> +#include "cpr/cpr.h" #include "emotespp/emotes.hpp" #include "ixwebsocket/IXWebSocket.h" @@ -25,4 +28,63 @@ namespace emotespp { bool is_connected = false; }; + + struct User { + std::string alias_id, id, username, emote_set_id; + }; + + struct EmoteSet { + std::string id, name; + User owner; + std::vector<Emote> emotes; + }; + + class SevenTVAPIClient : public RetrieveEmoteAPI<Emote> { + public: + SevenTVAPIClient() = default; + ~SevenTVAPIClient() = default; + + std::vector<Emote> get_channel_emotes( + std::string &channel_id) const override { + cpr::Response r = + cpr::Get(cpr::Url{base_url + "/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); + nlohmann::json set = j["emote_set"]; + + return this->parse_emoteset(set); + } + + 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); + + return this->parse_emoteset(j); + } + + std::optional<User> get_user_by_twitch_id( + const unsigned int &twitch_id) const; + std::optional<User> get_user(const std::string &id) const; + std::optional<EmoteSet> get_emote_set( + const std::string &emote_set_id) const; + + private: + std::vector<Emote> parse_emoteset(const nlohmann::json &value) const; + std::optional<User> parse_user(const nlohmann::json &value) const; + + const std::string base_url = "https://7tv.io/v3"; + }; }
\ No newline at end of file |
