summaryrefslogtreecommitdiff
path: root/include/emotespp/seventv.hpp
blob: 650d8649974e30a7efb1c8e78563b6cc76a9bda3 (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
#pragma once

#include <nlohmann/json.hpp>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>

#include "cpr/cpr.h"
#include "emotespp/emotes.hpp"
#include "ixwebsocket/IXWebSocket.h"

namespace emotespp {
  class SevenTVWebsocketClient : public RetrieveEmoteWebsocket<Emote> {
    public:
      SevenTVWebsocketClient();

      void subscribe_emote_set(const std::string &emote_set_id);
      void unsubscribe_emote_set(const std::string &emote_set_id);

      void start();

      const std::vector<std::string> &get_ids() const;

    private:
      Emote create_emote(const nlohmann::json &data);

      std::vector<std::string> ids;
      ix::WebSocket websocket;

      bool is_connected = false;
  };

  struct User {
      std::string alias_id, id, username, emote_set_id;
  };

  struct EmoteSet {
      std::string id, name;
      User owner;
      std::vector<Emote> emotes;
  };

  class SevenTVAPIClient : public RetrieveEmoteAPI<Emote> {
    public:
      SevenTVAPIClient() = default;
      ~SevenTVAPIClient() = default;

      std::vector<Emote> get_channel_emotes(
          std::string &channel_id) const override {
        cpr::Response r =
            cpr::Get(cpr::Url{base_url + "/users/twitch/" + channel_id});

        if (r.status_code != 200) {
          throw std::runtime_error(
              "Failed to get channel emotes. Status code: " +
              std::to_string(r.status_code));
        }

        nlohmann::json j = nlohmann::json::parse(r.text);
        nlohmann::json set = j["emote_set"];

        return this->parse_emoteset(set);
      }

      std::vector<Emote> get_global_emotes() const override {
        cpr::Response r = cpr::Get(cpr::Url{base_url + "/emote-sets/global"});

        if (r.status_code != 200) {
          throw std::runtime_error(
              "Failed to get global emotes. Status code: " +
              std::to_string(r.status_code));
        }

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

        return this->parse_emoteset(j);
      }

      std::optional<User> get_user_by_twitch_id(
          const unsigned int &twitch_id) const;
      std::optional<User> get_user(const std::string &id) const;
      std::optional<EmoteSet> get_emote_set(
          const std::string &emote_set_id) const;

    private:
      std::vector<Emote> parse_emoteset(const nlohmann::json &value) const;
      std::optional<User> parse_user(const nlohmann::json &value) const;

      const std::string base_url = "https://7tv.io/v3";
  };
}