diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
| commit | d1793df1eda463b10107d41785ad1d7f055ed476 (patch) | |
| tree | fd3e41c3b4a05924748ae4b762e1ae55a0bc815c /bot/src/api/twitch/helix_client.cpp | |
| parent | d7a2de17e9b7931f68b5b4079b1c36866a19d343 (diff) | |
upd: moved the bot part to a relative subfolder
Diffstat (limited to 'bot/src/api/twitch/helix_client.cpp')
| -rw-r--r-- | bot/src/api/twitch/helix_client.cpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/bot/src/api/twitch/helix_client.cpp b/bot/src/api/twitch/helix_client.cpp new file mode 100644 index 0000000..04d630b --- /dev/null +++ b/bot/src/api/twitch/helix_client.cpp @@ -0,0 +1,144 @@ +#include "helix_client.hpp" + +#include <nlohmann/json.hpp> +#include <string> +#include <vector> + +#include "cpr/api.h" +#include "cpr/bearer.h" +#include "cpr/cprtypes.h" +#include "cpr/response.h" +#include "schemas/stream.hpp" +#include "schemas/user.hpp" + +namespace bot::api::twitch { + HelixClient::HelixClient(const std::string &token, + const std::string &client_id) { + this->token = token; + this->client_id = client_id; + } + + std::vector<schemas::User> HelixClient::get_users( + const std::vector<std::string> &logins) const { + std::string s; + + for (auto i = logins.begin(); i != logins.end(); i++) { + std::string start; + if (i == logins.begin()) { + start = "?"; + } else { + start = "&"; + } + + s += start + "login=" + *i; + } + + return this->get_users_by_query(s); + } + + std::vector<schemas::User> HelixClient::get_users( + const std::vector<int> &ids) const { + std::string s; + + for (auto i = ids.begin(); i != ids.end(); i++) { + std::string start; + if (i == ids.begin()) { + start = "?"; + } else { + start = "&"; + } + + s += start + "id=" + std::to_string(*i); + } + + return this->get_users_by_query(s); + } + + std::vector<schemas::User> HelixClient::get_users_by_query( + const std::string &query) const { + cpr::Response response = cpr::Get( + cpr::Url{this->base_url + "/users" + query}, cpr::Bearer{this->token}, + cpr::Header{{"Client-Id", this->client_id.c_str()}}); + + if (response.status_code != 200) { + return {}; + } + + std::vector<schemas::User> users; + + nlohmann::json j = nlohmann::json::parse(response.text); + + for (const auto &d : j["data"]) { + schemas::User u{std::stoi(d["id"].get<std::string>()), d["login"]}; + + users.push_back(u); + } + + return users; + } + + std::vector<schemas::User> HelixClient::get_chatters( + const int &broadcaster_id, const int &moderator_id) const { + cpr::Response response = + cpr::Get(cpr::Url{this->base_url + "/chat/chatters?broadcaster_id=" + + std::to_string(broadcaster_id) + + "&moderator_id=" + std::to_string(moderator_id)}, + cpr::Bearer{this->token}, + cpr::Header{{"Client-Id", this->client_id.c_str()}}); + + if (response.status_code != 200) { + return {}; + } + + std::vector<schemas::User> users; + + nlohmann::json j = nlohmann::json::parse(response.text); + + for (const auto &d : j["data"]) { + schemas::User u{std::stoi(d["user_id"].get<std::string>()), + d["user_login"]}; + + users.push_back(u); + } + + return users; + } + + std::vector<schemas::Stream> HelixClient::get_streams( + const std::vector<int> &ids) const { + std::string s; + + for (auto i = ids.begin(); i != ids.end(); i++) { + std::string start; + if (i == ids.begin()) { + start = "?"; + } else { + start = "&"; + } + + s += start + "user_id=" + std::to_string(*i); + } + + cpr::Response response = cpr::Get( + cpr::Url{this->base_url + "/streams" + s}, cpr::Bearer{this->token}, + cpr::Header{{"Client-Id", this->client_id.c_str()}}); + + if (response.status_code != 200) { + return {}; + } + + std::vector<schemas::Stream> streams; + + nlohmann::json j = nlohmann::json::parse(response.text); + + for (const auto &d : j["data"]) { + schemas::Stream u{std::stoi(d["user_id"].get<std::string>()), + d["user_login"], d["game_name"], d["title"], + d["started_at"]}; + + streams.push_back(u); + } + + return streams; + } +} |
