diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-03 23:26:52 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-03 23:26:52 +0500 |
| commit | 1924b6751a29f8b528d09412bd1e1e74f4d30c27 (patch) | |
| tree | ceaeb5a4806ba217e09fde395589065fd48aa2b4 /src | |
| parent | d5a08922b8f22e4494fca0e28252f4b769649d64 (diff) | |
feat: stream listener client
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 4 | ||||
| -rw-r--r-- | src/stream.cpp | 61 | ||||
| -rw-r--r-- | src/stream.hpp | 30 |
3 files changed, 95 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp index 2eb0a96..20823ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include "irc/client.hpp" #include "irc/message.hpp" #include "localization/localization.hpp" +#include "stream.hpp" int main(int argc, char *argv[]) { std::cout << "hi world\n"; @@ -61,6 +62,8 @@ int main(int argc, char *argv[]) { bot::api::twitch::HelixClient helix_client(cfg.bot_password, cfg.bot_client_id); + bot::stream::StreamListenerClient stream_listener_client(helix_client); + client.on<bot::irc::MessageType::Privmsg>( [&client, &command_loader, &localization, &cfg, &helix_client]( const bot::irc::Message<bot::irc::MessageType::Privmsg> &message) { @@ -75,6 +78,7 @@ int main(int argc, char *argv[]) { }); client.run(); + stream_listener_client.run_thread(); return 0; } diff --git a/src/stream.cpp b/src/stream.cpp new file mode 100644 index 0000000..a42a050 --- /dev/null +++ b/src/stream.cpp @@ -0,0 +1,61 @@ +#include "stream.hpp" + +#include <algorithm> +#include <chrono> +#include <thread> + +namespace bot::stream { + void StreamListenerClient::listen_channel(const int &id) { + this->ids.push_back(id); + } + void StreamListenerClient::unlisten_channel(const int &id) { + auto x = std::find_if(this->ids.begin(), this->ids.end(), + [&](const auto &x) { return x == id; }); + + if (x != this->ids.end()) { + this->ids.erase(x); + } + + auto y = std::find_if(this->online_ids.begin(), this->online_ids.end(), + [&](const auto &x) { return x == id; }); + + if (y != this->online_ids.end()) { + this->online_ids.erase(y); + } + } + void StreamListenerClient::run_thread() { + std::thread t(&bot::stream::StreamListenerClient::run, this); + t.join(); + } + void StreamListenerClient::run() { + while (true) { + this->check(); + std::this_thread::sleep_for(std::chrono::seconds(5)); + } + } + void StreamListenerClient::check() { + auto streams = this->helix_client.get_streams(this->ids); + + // adding new ids + for (const auto &stream : streams) { + bool is_already_live = + std::any_of(this->online_ids.begin(), this->online_ids.end(), + [&](const auto &x) { return x == stream.user_id; }); + + if (!is_already_live) { + this->online_ids.insert(stream.user_id); + } + } + + // removing old ids + for (auto i = this->online_ids.begin(); i != this->online_ids.end(); i++) { + bool is_live = + std::any_of(streams.begin(), streams.end(), + [&](const auto &x) { return x.user_id == *i; }); + + if (!is_live) { + this->online_ids.erase(i); + } + } + } +} diff --git a/src/stream.hpp b/src/stream.hpp new file mode 100644 index 0000000..ba71a6a --- /dev/null +++ b/src/stream.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include <set> +#include <vector> + +#include "api/twitch/helix_client.hpp" + +namespace bot::stream { + class StreamListenerClient { + public: + StreamListenerClient(const api::twitch::HelixClient &helix_client) + : helix_client(helix_client){}; + ~StreamListenerClient() = default; + + void run_thread(); + + void listen_channel(const int &id); + void unlisten_channel(const int &id); + + private: + void run(); + void check(); + + const api::twitch::HelixClient &helix_client; + + std::vector<int> ids; + + std::set<int> online_ids; + }; +} |
