summaryrefslogtreecommitdiff
path: root/bot/src/api/kick.cpp
blob: 9b64f48c52f1bcd5dd982c98f93d88170e288821 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "api/kick.hpp"

#include <chrono>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>

#include "cpr/api.h"
#include "cpr/cprtypes.h"
#include "cpr/response.h"
#include "logger.hpp"
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#include "utils/chrono.hpp"
#include "utils/string.hpp"

namespace bot::api {
  std::vector<KickChannel> parse_json_channels(const nlohmann::json &value) {
    if (!value.contains("data")) {
      log::warn("api/kick", "No data object in Kick API get_channels response");
      return {};
    }

    std::vector<KickChannel> channels;
    nlohmann::json d = value["data"];

    for (const nlohmann::json &c : d) {
      channels.push_back(
          {c["broadcaster_user_id"], c["slug"], c["stream_title"],
           c["category"]["name"], c["stream"]["is_live"],
           utils::chrono::string_to_time_point(c["stream"]["start_time"],
                                               "%Y-%m-%dT%H:%M:%SZ")});
    }

    return channels;
  }

  std::vector<KickChannel> KickAPIClient::get_channels(
      const std::vector<int> &ids) const {
    if (this->authorization_key.empty()) {
      log::error("api/kick", "You must be authorized before using Kick API");
      return {};
    }

    if (ids.empty()) {
      return {};
    }

    cpr::Response r = cpr::Get(
        cpr::Url{this->base_url + "/public/v1/channels?broadcaster_user_id=" +
                 utils::string::str(ids.begin(), ids.end(), ',')},
        cpr::Header{{"Authorization", "Bearer " + this->authorization_key}});

    if (r.status_code != 200) {
      log::error("api/kick", "Failed to get Kick channels. Status code: " +
                                 std::to_string(r.status_code));
      return {};
    }

    nlohmann::json j = nlohmann::json::parse(r.text);
    return parse_json_channels(j);
  }

  std::vector<KickChannel> KickAPIClient::get_channels(
      const std::string &slug) const {
    if (this->authorization_key.empty()) {
      log::error("api/kick", "You must be authorized before using Kick API");
      return {};
    }

    if (slug.empty()) {
      return {};
    }

    cpr::Response r = cpr::Get(
        cpr::Url{this->base_url + "/public/v1/channels?slug=" + slug},
        cpr::Header{{"Authorization", "Bearer " + this->authorization_key}});

    if (r.status_code != 200) {
      log::error("api/kick", "Failed to get Kick channels. Status code: " +
                                 std::to_string(r.status_code));
      return {};
    }

    nlohmann::json j = nlohmann::json::parse(r.text);
    return parse_json_channels(j);
  }

  void KickAPIClient::authorize() {
    cpr::Response r = cpr::Post(
        cpr::Url{"https://id.kick.com/oauth/"
                 "token?grant_type=client_credentials&client_id=" +
                 this->client_id + "&client_secret=" + this->client_secret});

    if (r.status_code != 200) {
      throw std::runtime_error(
          "Failed to authorize in Kick API. Status code: " +
          std::to_string(r.status_code));
    }

    nlohmann::json j = nlohmann::json::parse(r.text);

    this->authorization_key = j["access_token"];
    this->expires_in = j["expires_in"];
    this->token_acquired_timestamp = std::time(nullptr);

    log::info("api/kick", "Successfully authorized in Kick API!");
  }

  void KickAPIClient::refresh_token_thread() {
    while (true) {
      std::this_thread::sleep_for(std::chrono::seconds(60));
      if (this->client_id.empty() || this->client_secret.empty()) {
        break;
      } else if (std::time(nullptr) - this->token_acquired_timestamp <
                 this->expires_in - 300) {
        continue;
      }

      log::info("api/kick", "Kick token is going to expire. Refreshing...");
      this->authorize();
    }
  }
}