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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
#include "stream.hpp"
#include <algorithm>
#include <chrono>
#include <pqxx/pqxx>
#include <string>
#include <thread>
#include <vector>
#include "api/kick.hpp"
#include "api/twitch/schemas/stream.hpp"
#include "config.hpp"
#include "logger.hpp"
#include "nlohmann/json.hpp"
#include "schemas/stream.hpp"
#include "utils/string.hpp"
namespace bot::stream {
void StreamListenerClient::listen_channel(const int &id,
const StreamerType &type) {
this->streamers.push_back({id, type, false, "", ""});
log::info("TwitchStreamListener",
"Listening stream events for ID " + std::to_string(id));
}
void StreamListenerClient::unlisten_channel(const int &id,
const StreamerType &type) {
auto x = std::find_if(this->streamers.begin(), this->streamers.end(),
[&id, &type](const StreamerData &x) {
return x.id == id && x.type == type;
});
if (x != this->streamers.end()) {
this->streamers.erase(x);
}
}
void StreamListenerClient::run() {
while (true) {
this->update_channel_ids();
this->check();
std::this_thread::sleep_for(std::chrono::seconds(5));
}
}
void StreamListenerClient::check() {
std::vector<int> twitch_ids, kick_ids;
std::for_each(this->streamers.begin(), this->streamers.end(),
[&twitch_ids, &kick_ids](const StreamerData &data) {
if (data.type == TWITCH) {
twitch_ids.push_back(data.id);
} else if (data.type == KICK) {
kick_ids.push_back(data.id);
}
});
auto kick_streams = this->kick_api_client.get_channels(kick_ids);
auto twitch_streams = this->helix_client.get_streams(twitch_ids);
auto now = std::chrono::system_clock::now();
auto now_time_it = std::chrono::system_clock::to_time_t(now);
auto now_tm = std::gmtime(&now_time_it);
now = std::chrono::system_clock::from_time_t(std::mktime(now_tm));
// notifying about new livestreams
for (const auto &stream : twitch_streams) {
auto data = std::find_if(this->streamers.begin(), this->streamers.end(),
[&stream](const StreamerData &data) {
return data.type == TWITCH &&
data.id == stream.get_user_id();
});
if (data == this->streamers.end()) {
continue;
}
if (!data->is_live) {
data->is_live = true;
auto difference = now - stream.get_started_at();
auto difference_min =
std::chrono::duration_cast<std::chrono::minutes>(difference);
if (difference_min.count() < 1) {
this->handler(schemas::EventType::LIVE, stream, *data);
}
}
}
for (const api::KickChannel &channel : kick_streams) {
auto data = std::find_if(this->streamers.begin(), this->streamers.end(),
[&channel](const StreamerData &data) {
return data.type == KICK &&
data.id == channel.broadcaster_user_id;
});
if (data == this->streamers.end()) {
continue;
}
if (!data->is_live && channel.is_live) {
data->is_live = true;
auto difference = now - channel.start_time;
auto difference_min =
std::chrono::duration_cast<std::chrono::minutes>(difference);
if (difference_min.count() < 1) {
this->handler(schemas::EventType::KICK_LIVE,
{channel.broadcaster_user_id, channel.slug,
channel.stream_game_name, channel.stream_title},
*data);
}
}
}
// removing ended livestreams
for (StreamerData &data : this->streamers) {
bool in_twitch_streams = std::any_of(
twitch_streams.begin(), twitch_streams.end(), [&data](const auto &s) {
return s.get_user_id() == data.id && data.type == TWITCH;
});
bool in_kick_streams =
std::any_of(kick_streams.begin(), kick_streams.end(),
[&data](const api::KickChannel &s) {
return s.broadcaster_user_id == data.id &&
data.type == KICK && s.is_live;
});
if (data.type == TWITCH && data.is_live && !in_twitch_streams) {
data.is_live = false;
this->handler(schemas::EventType::OFFLINE,
api::twitch::schemas::Stream{data.id}, data);
} else if (data.type == KICK && data.is_live && !in_kick_streams) {
data.is_live = false;
this->handler(schemas::EventType::KICK_OFFLINE,
api::twitch::schemas::Stream{data.id}, data);
}
}
// notifying about stream info
auto twitch_stream_info =
this->helix_client.get_channel_information(twitch_ids);
for (const auto &stream : twitch_stream_info) {
auto data = std::find_if(this->streamers.begin(), this->streamers.end(),
[&stream](const StreamerData &data) {
return data.type == TWITCH &&
data.id == stream.get_user_id();
});
if (data == this->streamers.end()) {
continue;
}
if (stream.get_title() != data->title) {
if (!data->title.empty()) {
this->handler(schemas::EventType::TITLE, stream, *data);
}
data->title = stream.get_title();
}
if (stream.get_game_name() != data->game) {
if (!data->game.empty()) {
this->handler(schemas::EventType::GAME, stream, *data);
}
data->game = stream.get_game_name();
}
}
for (const api::KickChannel &channel : kick_streams) {
auto data = std::find_if(this->streamers.begin(), this->streamers.end(),
[&channel](const StreamerData &data) {
return data.type == KICK &&
data.id == channel.broadcaster_user_id;
});
if (data == this->streamers.end()) {
continue;
}
api::twitch::schemas::Stream stream{
channel.broadcaster_user_id, channel.slug, channel.stream_game_name,
channel.stream_title};
if (channel.stream_title != data->title &&
!channel.stream_title.empty()) {
if (!data->title.empty()) {
this->handler(schemas::EventType::KICK_TITLE, stream, *data);
}
data->title = channel.stream_title;
}
if (channel.stream_game_name != data->game &&
!channel.stream_game_name.empty()) {
if (!data->game.empty()) {
this->handler(schemas::EventType::KICK_GAME, stream, *data);
}
data->game = channel.stream_game_name;
}
}
}
void StreamListenerClient::handler(const schemas::EventType &type,
const api::twitch::schemas::Stream &stream,
const StreamerData &data) {
pqxx::connection conn(GET_DATABASE_CONNECTION_URL(this->configuration));
pqxx::work work(conn);
pqxx::result events = work.exec_params(
"SELECT e.id, e.message, array_to_json(e.flags) AS "
"flags, c.alias_name AS channel_aname, c.alias_id AS channel_aid FROM "
"events e "
"INNER JOIN channels c ON c.id = e.channel_id "
"WHERE e.event_type = $1 AND e.name = $2",
pqxx::params{static_cast<int>(type), stream.get_user_id()});
for (const auto &event : events) {
std::vector<std::string> names;
bool massping_enabled = false;
if (!event[2].is_null()) {
nlohmann::json j = nlohmann::json::parse(event[2].as<std::string>());
massping_enabled = std::any_of(j.begin(), j.end(), [](const auto &x) {
return static_cast<int>(x) == static_cast<int>(schemas::MASSPING);
});
}
if (massping_enabled) {
auto chatters = this->helix_client.get_chatters(
event[4].as<int>(), this->irc_client.get_bot_id());
std::for_each(chatters.begin(), chatters.end(),
[&names](const auto &x) { names.push_back(x.login); });
} else {
pqxx::result subs = work.exec_params(
"SELECT u.alias_name FROM users u "
"INNER JOIN events e ON e.id = $1 "
"INNER JOIN event_subscriptions es ON es.event_id = e.id "
"WHERE u.id = es.user_id",
pqxx::params{event[0].as<int>()});
std::for_each(subs.begin(), subs.end(), [&names](const pqxx::row &x) {
names.push_back(x[0].as<std::string>());
});
}
std::string base = "⚡ " + event[1].as<std::string>();
if (!names.empty()) {
base.append(" · ");
}
int pos = base.find("{old}");
if (pos != std::string::npos) {
if (type == schemas::EventType::TITLE ||
type == schemas::EventType::KICK_TITLE)
base.replace(pos, 5, data.title);
else if (type == schemas::EventType::GAME ||
type == schemas::EventType::KICK_GAME)
base.replace(pos, 5, data.game);
}
pos = base.find("{new}");
if (pos != std::string::npos) {
if (type == schemas::EventType::TITLE ||
type == schemas::EventType::KICK_TITLE)
base.replace(pos, 5, stream.get_title());
else if (type == schemas::EventType::GAME ||
type == schemas::EventType::KICK_GAME)
base.replace(pos, 5, stream.get_game_name());
}
std::vector<std::string> msgs =
utils::string::separate_by_length(base, names, "@", " ", 500);
for (const auto &msg : msgs) {
this->irc_client.say(event[3].as<std::string>(), base + msg);
}
}
work.commit();
conn.close();
}
void StreamListenerClient::update_channel_ids() {
pqxx::connection conn(GET_DATABASE_CONNECTION_URL(this->configuration));
pqxx::work work(conn);
pqxx::result ids =
work.exec("SELECT name, event_type FROM events WHERE event_type < 10");
for (const auto &row : ids) {
int id = row[0].as<int>();
int event_type = row[1].as<int>();
StreamerType type = (event_type >= schemas::EventType::KICK_LIVE &&
event_type <= schemas::EventType::KICK_GAME)
? StreamerType::KICK
: StreamerType::TWITCH;
if (std::any_of(this->streamers.begin(), this->streamers.end(),
[&id, &type](const StreamerData &x) {
return x.type == type && x.id == id;
})) {
continue;
}
listen_channel(id, type);
}
work.commit();
conn.close();
}
}
|