summaryrefslogtreecommitdiff
path: root/src/stream.cpp
blob: a42a0504b436bf7d55f41b9e68237d26a329ea0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
      }
    }
  }
}