diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-03 22:53:55 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-03 22:53:55 +0500 |
| commit | d5a08922b8f22e4494fca0e28252f4b769649d64 (patch) | |
| tree | 71fef5066c436d7ba18735b227ca4600b226d10f /src | |
| parent | 9bb97ad75e32d50a682571fa36bafa79f1e2d014 (diff) | |
feat: get_streams method
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/twitch/helix_client.cpp | 39 | ||||
| -rw-r--r-- | src/api/twitch/helix_client.hpp | 4 | ||||
| -rw-r--r-- | src/api/twitch/schemas/stream.hpp | 9 |
3 files changed, 52 insertions, 0 deletions
diff --git a/src/api/twitch/helix_client.cpp b/src/api/twitch/helix_client.cpp index ceac720..04d630b 100644 --- a/src/api/twitch/helix_client.cpp +++ b/src/api/twitch/helix_client.cpp @@ -8,6 +8,7 @@ #include "cpr/bearer.h" #include "cpr/cprtypes.h" #include "cpr/response.h" +#include "schemas/stream.hpp" #include "schemas/user.hpp" namespace bot::api::twitch { @@ -102,4 +103,42 @@ namespace bot::api::twitch { 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; + } } diff --git a/src/api/twitch/helix_client.hpp b/src/api/twitch/helix_client.hpp index 2b5f4ea..27a9fa3 100644 --- a/src/api/twitch/helix_client.hpp +++ b/src/api/twitch/helix_client.hpp @@ -3,6 +3,7 @@ #include <string> #include <vector> +#include "schemas/stream.hpp" #include "schemas/user.hpp" namespace bot::api::twitch { @@ -18,6 +19,9 @@ namespace bot::api::twitch { std::vector<schemas::User> get_chatters(const int &broadcaster_id, const int &moderator_id) const; + std::vector<schemas::Stream> get_streams( + const std::vector<int> &ids) const; + private: std::vector<schemas::User> get_users_by_query( const std::string &query) const; diff --git a/src/api/twitch/schemas/stream.hpp b/src/api/twitch/schemas/stream.hpp new file mode 100644 index 0000000..ea380db --- /dev/null +++ b/src/api/twitch/schemas/stream.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include <string> +namespace bot::api::twitch::schemas { + struct Stream { + int user_id; + std::string user_login, game_name, title, started_at; + }; +} |
