diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/seventv.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/seventv.cpp b/src/seventv.cpp index 51c306c..62ea550 100644 --- a/src/seventv.cpp +++ b/src/seventv.cpp @@ -3,6 +3,7 @@ #include <algorithm> #include <nlohmann/json.hpp> #include <optional> +#include <string> namespace emotespp { SevenTVWebsocketClient::SevenTVWebsocketClient() { @@ -162,4 +163,96 @@ namespace emotespp { return {id, code, original_code}; } + + std::vector<Emote> SevenTVAPIClient::parse_emoteset( + const nlohmann::json &value) const { + std::vector<Emote> emotes; + + nlohmann::json e = value["emotes"]; + + for (const nlohmann::json v : e) { + std::string id = v["id"]; + std::string code = v["name"]; + std::optional<std::string> original_code = v["data"]["name"]; + + if (code == original_code) { + original_code = std::nullopt; + } + + emotes.push_back({id, code, original_code}); + } + + return emotes; + } + + std::optional<User> SevenTVAPIClient::get_user_by_twitch_id( + const unsigned int &twitch_id) const { + cpr::Response r = cpr::Get(cpr::Url{this->base_url + "/users/twitch/" + + std::to_string(twitch_id)}); + + if (r.status_code != 200) { + return std::nullopt; + } + + nlohmann::json j = nlohmann::json::parse(r.text); + + std::string alias_id = j["id"]; + std::string username = j["username"]; + std::string emote_set_id = j["emote_set_id"]; + std::string id = j["user"]["id"]; + + return (User){alias_id, id, username, emote_set_id}; + } + + std::optional<User> SevenTVAPIClient::get_user(const std::string &id) const { + cpr::Response r = cpr::Get(cpr::Url{this->base_url + "/users/" + id}); + + if (r.status_code != 200) { + return std::nullopt; + } + + nlohmann::json j = nlohmann::json::parse(r.text); + + return this->parse_user(j); + } + + std::optional<EmoteSet> SevenTVAPIClient::get_emote_set( + const std::string &emote_set_id) const { + cpr::Response r = + cpr::Get(cpr::Url{this->base_url + "/emote-sets/" + emote_set_id}); + + if (r.status_code != 200) { + return std::nullopt; + } + + nlohmann::json j = nlohmann::json::parse(r.text); + + std::string id = j["id"]; + std::string name = j["name"]; + std::optional<User> owner = this->parse_user(j["owner"]); + + if (!owner.has_value()) { + return std::nullopt; + } + + return (EmoteSet){id, name, owner.value(), this->parse_emoteset(j)}; + } + + std::optional<User> SevenTVAPIClient::parse_user( + const nlohmann::json &value) const { + std::string id = value["id"]; + + nlohmann::json conns = value["connections"]; + auto twitchconn = std::find_if( + conns.begin(), conns.end(), + [](const nlohmann::json &x) { return x["platform"] == "TWITCH"; }); + + if (twitchconn == conns.end()) { + return std::nullopt; + } + + nlohmann::json c = *twitchconn; + + return (User){c["id"], id, c["username"], c["emote_set_id"]}; + } }
\ No newline at end of file |
