diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-02 00:05:53 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-02 00:05:53 +0500 |
| commit | 64ee8f8e7639c1e84a7e68d45e2aceec5299a4d6 (patch) | |
| tree | cbed19fbf96eceab891d8435eb80f69d51e7ed0f /src | |
| parent | de0439408529a257f975e613a9ac6b31926844f6 (diff) | |
feat: client for twitch api
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/twitch/helix_client.cpp | 78 | ||||
| -rw-r--r-- | src/api/twitch/helix_client.hpp | 24 | ||||
| -rw-r--r-- | src/api/twitch/schemas/user.hpp | 10 | ||||
| -rw-r--r-- | src/main.cpp | 4 |
4 files changed, 116 insertions, 0 deletions
diff --git a/src/api/twitch/helix_client.cpp b/src/api/twitch/helix_client.cpp new file mode 100644 index 0000000..c5031f0 --- /dev/null +++ b/src/api/twitch/helix_client.cpp @@ -0,0 +1,78 @@ +#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/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; + } +} diff --git a/src/api/twitch/helix_client.hpp b/src/api/twitch/helix_client.hpp new file mode 100644 index 0000000..fedec7f --- /dev/null +++ b/src/api/twitch/helix_client.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include <string> +#include <vector> + +#include "schemas/user.hpp" + +namespace bot::api::twitch { + class HelixClient { + public: + HelixClient(const std::string &token, const std::string &client_id); + ~HelixClient() = default; + + std::vector<schemas::User> get_users( + const std::vector<std::string> &logins) const; + std::vector<schemas::User> get_users(const std::vector<int> &ids) const; + + private: + std::vector<schemas::User> get_users_by_query( + const std::string &query) const; + std::string token, client_id; + const std::string base_url = "https://api.twitch.tv/helix"; + }; +} diff --git a/src/api/twitch/schemas/user.hpp b/src/api/twitch/schemas/user.hpp new file mode 100644 index 0000000..288ec72 --- /dev/null +++ b/src/api/twitch/schemas/user.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include <string> + +namespace bot::api::twitch::schemas { + struct User { + int id; + std::string login; + }; +} diff --git a/src/main.cpp b/src/main.cpp index d617769..842e093 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include <pqxx/pqxx> #include <string> +#include "api/twitch/helix_client.hpp" #include "bundle.hpp" #include "commands/command.hpp" #include "config.hpp" @@ -57,6 +58,9 @@ int main(int argc, char *argv[]) { work.commit(); conn.close(); + bot::api::twitch::HelixClient helix_client(cfg.bot_password, + cfg.bot_client_id); + client.on<bot::irc::MessageType::Privmsg>( [&client, &command_loader, &localization, &cfg](const bot::irc::Message<bot::irc::MessageType::Privmsg> &message) { |
