From d1793df1eda463b10107d41785ad1d7f055ed476 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 18 May 2024 14:48:12 +0500 Subject: upd: moved the bot part to a relative subfolder --- bot/src/api/twitch/helix_client.cpp | 144 ++++++++++++++++++++++++++++++++++ bot/src/api/twitch/helix_client.hpp | 31 ++++++++ bot/src/api/twitch/schemas/stream.hpp | 35 +++++++++ bot/src/api/twitch/schemas/user.hpp | 10 +++ 4 files changed, 220 insertions(+) create mode 100644 bot/src/api/twitch/helix_client.cpp create mode 100644 bot/src/api/twitch/helix_client.hpp create mode 100644 bot/src/api/twitch/schemas/stream.hpp create mode 100644 bot/src/api/twitch/schemas/user.hpp (limited to 'bot/src/api') 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 +#include +#include + +#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 HelixClient::get_users( + const std::vector &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 HelixClient::get_users( + const std::vector &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 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 users; + + nlohmann::json j = nlohmann::json::parse(response.text); + + for (const auto &d : j["data"]) { + schemas::User u{std::stoi(d["id"].get()), d["login"]}; + + users.push_back(u); + } + + return users; + } + + std::vector 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 users; + + nlohmann::json j = nlohmann::json::parse(response.text); + + for (const auto &d : j["data"]) { + schemas::User u{std::stoi(d["user_id"].get()), + d["user_login"]}; + + users.push_back(u); + } + + return users; + } + + std::vector HelixClient::get_streams( + const std::vector &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 streams; + + nlohmann::json j = nlohmann::json::parse(response.text); + + for (const auto &d : j["data"]) { + schemas::Stream u{std::stoi(d["user_id"].get()), + d["user_login"], d["game_name"], d["title"], + d["started_at"]}; + + streams.push_back(u); + } + + return streams; + } +} diff --git a/bot/src/api/twitch/helix_client.hpp b/bot/src/api/twitch/helix_client.hpp new file mode 100644 index 0000000..27a9fa3 --- /dev/null +++ b/bot/src/api/twitch/helix_client.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include "schemas/stream.hpp" +#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 get_users( + const std::vector &logins) const; + std::vector get_users(const std::vector &ids) const; + + std::vector get_chatters(const int &broadcaster_id, + const int &moderator_id) const; + + std::vector get_streams( + const std::vector &ids) const; + + private: + std::vector 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/bot/src/api/twitch/schemas/stream.hpp b/bot/src/api/twitch/schemas/stream.hpp new file mode 100644 index 0000000..e3d485e --- /dev/null +++ b/bot/src/api/twitch/schemas/stream.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include "../../../utils/chrono.hpp" + +namespace bot::api::twitch::schemas { + class Stream { + public: + Stream(int user_id, std::string user_login, std::string game_name, + std::string title, std::string started_at) + : user_id(user_id), + user_login(user_login), + game_name(game_name), + title(title), + started_at(utils::chrono::string_to_time_point( + started_at, "%Y-%m-%dT%H:%M:%SZ")) {} + + Stream(int user_id) : user_id(user_id) {} + + const int &get_user_id() const { return this->user_id; } + const std::string &get_user_login() const { return this->user_login; } + const std::string &get_game_name() const { return this->game_name; } + const std::string &get_title() const { return this->title; } + const std::chrono::system_clock::time_point &get_started_at() const { + return this->started_at; + } + + private: + int user_id; + std::string user_login, game_name, title; + std::chrono::system_clock::time_point started_at; + }; +} diff --git a/bot/src/api/twitch/schemas/user.hpp b/bot/src/api/twitch/schemas/user.hpp new file mode 100644 index 0000000..288ec72 --- /dev/null +++ b/bot/src/api/twitch/schemas/user.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace bot::api::twitch::schemas { + struct User { + int id; + std::string login; + }; +} -- cgit v1.2.3