blob: abf7e7fb7c79362281b29f524c87bb9e518b7df4 (
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
|
#ifdef BUILD_BETTERTTV
#pragma once
#include <algorithm>
#include <map>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
#include <vector>
#include "cpr/cpr.h"
#include "emotespp/emotes.hpp"
#include "ixwebsocket/IXWebSocket.h"
namespace emotespp {
class BetterTTVWebsocketClient : public RetrieveEmoteWebsocket<Emote> {
public:
BetterTTVWebsocketClient();
void subscribe_emote_set(const std::string &emote_set_id);
void unsubscribe_emote_set(const std::string &emote_set_id);
void start();
const std::map<std::string, std::vector<Emote>> &get_ids() const;
private:
std::map<std::string, std::vector<Emote>> ids;
ix::WebSocket websocket;
bool is_connected = false;
};
class BetterTTVAPIClient : public RetrieveEmoteAPI<Emote> {
public:
BetterTTVAPIClient() = default;
~BetterTTVAPIClient() = default;
std::vector<Emote> get_channel_emotes(
std::string &channel_id) const override {
cpr::Response r =
cpr::Get(cpr::Url{base_url + "/cached/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);
std::vector<Emote> emotes;
nlohmann::json channel_emotes = j["channelEmotes"];
std::for_each(channel_emotes.begin(), channel_emotes.end(),
[this, &emotes](const nlohmann::json &v) {
emotes.push_back(this->parse_emote(v));
});
nlohmann::json shared_emotes = j["sharedEmotes"];
std::for_each(shared_emotes.begin(), shared_emotes.end(),
[this, &emotes](const nlohmann::json &v) {
emotes.push_back(this->parse_emote(v));
});
return emotes;
}
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);
std::vector<Emote> emotes;
std::for_each(j.begin(), j.end(),
[this, &emotes](const nlohmann::json &v) {
emotes.push_back(this->parse_emote(v));
});
return emotes;
}
private:
Emote parse_emote(const nlohmann::json &j) const;
const std::string base_url = "https://api.betterttv.net/3";
};
}
#endif
|