-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweetwitch.pl
419 lines (393 loc) · 15.4 KB
/
weetwitch.pl
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use strict;
use warnings;
use utf8;
use JSON;
use Try::Tiny;
use Date::Parse;
use Date::Format;
use Date::Language;
my $sc_name = "WeeTwitch";
my $version = "0.11.0";
my $lang = Date::Language->new('French');
my ($token, $clientid, $channel, $server, $json, $decode, $fdecode, $user_id, $player, $couleur);
my ($game, $user, $mature, $follow, $buffer, $partner, $cb_str, $w_str, $incr, $reason, $stream_arg, $gpchat, $time);
my @liste;
my (%tags, %badge);
weechat::register($sc_name, "BOUTARD Florent <bandit.kroot\@gmail.com", $version, "GPL3", "Lance les streams Twitch.tv", "unload", "");
weechat::hook_command("whostream", "Juste taper /whostream.", "", "", "", "who_stream", "");
weechat::hook_command("whotwitch", "Taper /whotwitch et le nom d\'un utilisateur.", "", "", "", "whotwitch", "");
weechat::hook_command("stream", "Juste taper /stream dans le channel désiré.", "", "", "", "stream", "");
weechat::hook_command("subcheck", "Juste taper /subcheck dans le channel désiré.", "", "", "", "subcheck", "");
weechat::hook_command("viewers", "Juste taper /viewers.", "", "", "", "viewer", "");
weechat::hook_modifier("irc_in_WHISPER", "whisper_cb", "");
weechat::hook_modifier("irc_out_PRIVMSG", "privmsg_out_cb", "");
weechat::hook_modifier("irc_in2_PRIVMSG", "privmsg_in_cb", "");
weechat::hook_modifier("irc_in2_NOTICE", "notice_in_cb", "");
weechat::hook_modifier("irc_in_USERSTATE", "ignore_cb", "");
weechat::hook_modifier("irc_in_ROOMSTATE", "roomstate_cb", "");
weechat::hook_modifier("irc_in_HOSTTARGET", "ignore_cb", "");
weechat::hook_modifier("irc_in_USERNOTICE", "usernotice_cb", "");
weechat::hook_modifier("irc_in_CLEARCHAT", "clearchat_cb", "");
weechat::hook_modifier("irc_in_CLEARMSG", "ignore_cb", "");
my $file = weechat::info_get('weechat_dir', '') . "/weetwitch.json";
try {
open(FICHIER, "<", $file) or die weechat::print(weechat::current_buffer(), "*\tImpossible d'ouvrir le fichier de configuration.");
@liste = <FICHIER>;
close(FICHIER);
$json = join("", @liste);
$fdecode = decode_json($json);
$token = $fdecode->{'token'};
$clientid = $fdecode->{'clientid'};
$player = $fdecode->{'player'};
}
catch {
weechat::print(weechat::current_buffer(), "*\tPas de token et client id Twitch trouvé.");
};
userid(weechat::config_string(weechat::config_get("irc.server.twitch.nicks")));
my $twitch_un = $user_id;
#Commande /whostream
sub who_stream {
buffer();
try {
$json = `curl -s -X GET 'https://api.twitch.tv/helix/streams/followed?user_id=$twitch_un' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
weechat::print(weechat::current_buffer(), "curl -s -X GET 'https://api.twitch.tv/helix/streams/followed?user_id=$twitch_un' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'");
$decode = decode_json($json);
@liste = undef;
$incr = 1;
weechat::print($buffer, "---\t" . weechat::color("red") . weechat::color("bold") . "Stream en cours :");
foreach my $displayname (@{$decode->{'data'}}) {
if ($displayname->{'game_name'}) {
$game = "du " . weechat::color("bold") . $displayname->{'game_name'} . weechat::color("-bold");
}
else {
$game = "aucun jeu";
}
if ($displayname->{'type'} eq "live"){
$couleur = "default";
}
else {
$couleur = "blue";
}
$incr++;
push @liste, lc($displayname->{'user_name'});
weechat::print($buffer, "\t" . weechat::color($couleur) . weechat::color("bold") . "$displayname->{'user_name'}" . weechat::color("-bold") . " stream $game avec $displayname->{'viewer_count'} spectateurs.");
}
}
catch {
weechat::print($buffer, "---\t" . weechat::color("red") . weechat::color("bold") . "Impossible de vérifier les streams en cours...");
};
return weechat::WEECHAT_RC_OK;
}
#whotwitch pour remplacer le whois
sub whotwitch {
$user = lc($_[2]);
if ($user) {
buffer();
$json = `curl -s -X GET 'https://api.twitch.tv/helix/users?login=$user' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
$decode = decode_json($json);
foreach my $displayuser (@{$decode->{'data'}}) {
$user_id = $displayuser->{'id'};
weechat::print("$buffer","Utilisateur\t". weechat::color("bold") . $displayuser->{'display_name'} . weechat::color("-bold"));
if ($displayuser->{'type'}) { weechat::print("$buffer","Type\t$displayuser->{'type'}"); }
if ($displayuser->{'description'}) { weechat::print("$buffer","Bio\t$displayuser->{'description'}"); }
timeparse($displayuser->{'created_at'});
weechat::print("$buffer","Créé le\t$time");
$json = `curl -s -X GET 'https://api.twitch.tv/helix/users/follows?from_id=$user_id' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
$decode = decode_json($json);
if ($decode->{'total'} eq "1") {
foreach my $displayfollow (@{$decode->{'data'}}) {
$follow = " : " . $displayfollow->{'to_name'};
}
}
else {
$follow = "s.";
}
weechat::print("$buffer","Il follow\t" . $decode->{'total'} . " personne" . $follow);
weechat::print("$buffer","URL\thttp://twitch.tv/$user/profile");
}
}
return weechat::WEECHAT_RC_OK;
}
#Vérifie si l'utilisateur est sub à la chaine
sub subcheck {
if (server()) {
try {
$json = `curl -s -X GET 'https://api.twitch.tv/helix/subscriptions/user?broadcaster_id=$user_id&user_id=$twitch_un' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
$decode = decode_json($json);
if ($decode->{'error'}) {
weechat::print(weechat::current_buffer(), "*\t" . weechat::color("magenta") . "Vous n'êtes pas abonné.");
}
else {
foreach my $displaytier (@{$decode->{'data'}}) {
weechat::print(weechat::current_buffer(), "*\t" . weechat::color("magenta") . "Vous êtes abonné tier " . $displaytier->{'tier'} / 1000 . ".");
}
}
}
catch {
weechat::print(weechat::current_buffer(), "*\tImpossible de vérifier l\'abonnement.");
};
}
return weechat::WEECHAT_RC_OK;
}
#Gère la commande /stream
sub stream {
$stream_arg = lc($_[2]);
if ($stream_arg) {
$channel = $stream_arg;
undef $stream_arg;
weechat::command("", "/quote -server twitch JOIN #" . $channel);
stream_launch();
return weechat::WEECHAT_RC_OK;
}
elsif (server() and not $stream_arg) {
stream_launch();
}
return weechat::WEECHAT_RC_OK;
}
#Lancement d'un scream
sub stream_launch {
buffer();
weechat::print($buffer, "---\tLancement du stream twitch.tv/$channel...");
weechat::hook_process("$player twitch.tv/$channel", 0, "stream_end", "");
}
#Fin de stream
sub stream_end {
buffer();
weechat::print($buffer, "Fin du stream.");
return weechat::WEECHAT_RC_OK;
}
#Récupère les info sur les chaines
sub channel_info {
userid($channel);
buffer();
weechat::print($buffer, "Chaîne\t" . weechat::color("bold") . "$channel");
try {
$json = `curl -s -X GET 'https://api.twitch.tv/helix/streams?user_id=$user_id' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
$decode = decode_json($json);
if (@{$decode->{'data'}}) {
foreach my $decodechan (@{$decode->{'data'}}) {
weechat::buffer_set(weechat::buffer_search("irc", "twitch.#" . $channel), "title", $decodechan->{'title'});
weechat::print($buffer, "Titre\t$decodechan->{'title'}");
weechat::print($buffer, "Jeu en cours\t$decodechan->{'game_name'}");
weechat::print($buffer, "Spectateurs\t$decodechan->{'viewer_count'}");
weechat::print($buffer, "Type\t$decodechan->{'type'}");
timeparse($decodechan->{'started_at'});
weechat::print($buffer, "Commencé\tle $time");
weechat::print($buffer, "Langage\t$decodechan->{'language'}");
if ($decodechan->{'is_mature'}) { weechat::print($buffer, "*\tStream mature"); }
}
}
else {
weechat::print($buffer, "*\t" . weechat::color("bold") . "Pas de stream en cours...");
}
}
catch {
weechat::print($buffer, "Impossible de récupérer le topic.");
};
}
#Affiche les viewers
sub viewer {
if (server()) {
try {
$json = `curl -s -X GET 'https://api.twitch.tv/helix/streams?user_id=$user_id' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
$decode = decode_json($json);
if (@{$decode->{'data'}}) {
foreach my $decodeview (@{$decode->{'data'}}) {
weechat::print(weechat::current_buffer(), "*\t" . weechat::color("magenta") . "Actuellement $decodeview->{'viewer_count'} spectateurs.");
}
}
else {
weechat::print(weechat::current_buffer(), "*\tPas de live en cours...");
}
}
catch {
weechat::print(weechat::current_buffer(), "*\tImpossible de récupérer le topic.");
};
}
return weechat::WEECHAT_RC_OK;
}
#Pour afficher le nouveau buffer
sub buffer {
$buffer = weechat::buffer_search("perl", $sc_name);
if ($buffer eq "") {
$buffer = weechat::buffer_new($sc_name, "buffer_input", "", "", "");
weechat::buffer_set($buffer, "title", "$sc_name $version [q] pour quitter.");
}
else {
weechat::print($buffer, weechat::color("yellow") . "*\t". weechat::color("magenta") . "-" x 10);
}
}
#Pour joindre un cannal après un whostream
sub buffer_input {
my ($data, $buff, $string) = ($_[0], $_[1], $_[2]);
if ($string eq "q") {
weechat::buffer_close($buff);
}
elsif ($string =~ m/^\d+$/) {
if ($string <= $#liste) {
stream(0,0,$liste[$string]); #Placer en argument 2 à cause de /stream username
}
}
return weechat::WEECHAT_RC_OK;
}
#Vérification server et channel correct
sub server {
if (weechat::buffer_get_string(weechat::current_buffer(), "localvar_type") eq "channel") {
$server = weechat::buffer_get_string(weechat::current_buffer(), "localvar_server");
$channel = substr(weechat::buffer_get_string(weechat::current_buffer(), "localvar_channel"), 1);
}
if ($server eq "twitch") {
userid($channel);
return 1;
}
else {
weechat::print(weechat::current_buffer(), "*\tServeur et/ou channel non valide.");
return 0;
}
}
#Récupération du userid
sub userid{
$user = $_[0];
foreach my $displayid (@{$fdecode->{'id'}}) {
if ($user eq $displayid->{'name'}) {
$user_id = $displayid->{'userid'};
return;
}
}
$json = `curl -s -X GET 'https://api.twitch.tv/helix/users?login=$user' -H 'Authorization: Bearer $token' -H 'Client-Id: $clientid'`;
$decode = decode_json($json);
foreach my $displayuser (@{$decode->{'data'}}) {
$user_id = $displayuser->{'id'};
}
$fdecode->{'id'}[scalar(@{$fdecode->{'id'}})] = {"name", $user, "userid", $user_id};
open(FICHADD, ">:encoding(UTF-8)", $file);
print FICHADD JSON->new->indent->space_after->encode($fdecode);
close(FICHADD);
buffer();
weechat::print($buffer, "---\t" . weechat::color("blue") ."ID utilisateur ajouté au fichier $file");
}
#Affiche les messages d'expulsion de twitch
sub clearchat_cb {
(undef, undef, undef, $cb_str) = @_;
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
%tags = split(/[;=]/, $cb_str->{'tags'});
if (exists($tags{'ban-duration'})) {
return ":" . $cb_str->{'text'} . " NOTICE " . $cb_str->{'channel'} . " :a été expulsé du salon pour " . $tags{'ban-duration'} . "s.";
}
else {
return ":" . $cb_str->{'text'} . " NOTICE " . $cb_str->{'channel'} . " :a été banni du salon.";
}
}
#Affiche les whispers de twitch
sub whisper_cb {
(undef, undef, undef, $cb_str) = @_;
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
return ":" . $cb_str->{'host'} . " PRIVMSG " . $cb_str->{'arguments'};
}
#Pour envoyer les message privé
sub privmsg_out_cb {
(undef, undef, $server, $cb_str) = @_;
if ($server ne "twitch") { return $cb_str; }
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
if ($cb_str->{'channel'} =~ "#") {
return "PRIVMSG " . $cb_str->{'channel'} . " :" . $cb_str->{'text'};
}
else {
return "PRIVMSG jtv :.w " . $cb_str->{'nick'} . " " . $cb_str->{'text'};
}
}
#Gestion des badges
sub privmsg_in_cb {
(undef, undef, $server, $cb_str) = @_;
no utf8;
if ($server ne "twitch") { return $cb_str; }
$w_str = $cb_str;
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
if (substr($cb_str->{'channel'}, 0, 1) ne "#") { return $w_str; }
if ($cb_str->{'nick'} eq weechat::config_string(weechat::config_get("irc.server.twitch.nicks"))) { return ""; } #utilisé pour les salles privées, twitch renvoi les message de l'utilisateur
$reason = $cb_str->{'nick'};
my @taglist = split(/[;]/, $cb_str->{'tags'});
if (substr($taglist[1], -1) ne "=") {
%tags = split(/[=]/, $taglist[1]);
%badge = split(/[\/,]/, $tags{"badges"});
if (exists($badge{'subscriber'})) { $reason = weechat::color("bold") . $reason; }
if (exists($badge{'turbo'})) { $reason = weechat::color("125") . "+" . $reason; }
if (exists($badge{'premium'})) { $reason = weechat::color("37") . "+" . $reason; }
if (exists($badge{'partner'})) { $reason = weechat::color("136") . "✓" . $reason; }
if (exists($badge{'moderator'})) { $reason = weechat::color("166") . "@" . $reason; }
if (exists($badge{'global_mod'})) { $reason = weechat::color("gray,red") . "@" . $reason; }
if (exists($badge{'admin'})) { $reason = weechat::color("white,red") . "%" . $reason; }
if (exists($badge{'staff'})) { $reason = weechat::color("white,magenta") . "&" . $reason; }
}
$reason = weechat::color("245") . $reason;
return ":$reason PRIVMSG " . $cb_str->{'arguments'};
}
#Modification des notices
sub notice_in_cb {
(undef, undef, $server, $cb_str) = @_;
no utf8;
if ($server ne "twitch") { return $cb_str; }
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
if ($cb_str->{'nick'} eq "sub" or $cb_str->{'nick'} eq "resub" or $cb_str->{'nick'} eq "subgift") {
$couleur = "green";
}
elsif ($cb_str->{'nick'} eq "raid") {
$couleur = "red";
}
elsif ($cb_str->{'nick'} eq "ritual") {
$couleur = "blue";
}
else {
$couleur = "default";
}
return ":" . weechat::color($couleur) . $cb_str->{'nick'} . " NOTICE " . $cb_str->{'channel'} . " :" . weechat::color($couleur) . $cb_str->{'text'};
}
#Subscription raid ritual
sub usernotice_cb {
(undef, undef, undef, $cb_str) = @_;
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
if (substr($cb_str->{'tags'}, -1) eq "=") {
%tags = split(/[;=]/, $cb_str->{'tags'} . " "); #ajouter de " " car user-type n'est pas systématiquement envoyé
}
else {
%tags = split(/[;=]/, $cb_str->{'tags'});
}
$reason = join(" ", split(/[\\]s/, $tags{'system-msg'}));
if ($cb_str->{'text'}) { $reason = $reason . " Message : " . $cb_str->{'text'}; }
return ":" . $tags{'msg-id'} . " NOTICE " . $cb_str->{'channel'} . " :$reason";
}
#Mode des salons
sub roomstate_cb {
(undef, undef, $server, $cb_str) = @_;
if ($server ne "twitch") { return $cb_str; }
$cb_str = weechat::info_get_hashtable("irc_message_parse", {"message" => $cb_str});
$channel = substr($cb_str->{'channel'},1);
channel_info();
if (substr($cb_str->{'tags'}, -1) ne "=") {
%tags = split(/[;=]/, $cb_str->{'tags'});
if (exists($tags{'broadcaster-lang'})) {
$reason = "";
if ($tags{'emote-only'}) { $reason = "emote-only "; }
if ($tags{'followers-only'}) { $reason = $reason . "followers-only "; }
if ($tags{'r9k'}) { $reason = $reason . "r9k "; }
if ($tags{'slow'}) { $reason = $reason . "slow " . $tags{'slow'} . "s "; }
if ($tags{'subs-only'}) { $reason = $reason . "subs-only "; }
if ($tags{'rituals'}) { $reason = $reason . "rituals"; }
if ($reason) { return ":#" . $channel . " NOTICE #" . $channel . " :Mode : $reason"; }
}
}
return "";
}
#formatage des dates
sub timeparse {
$time = str2time(@_);
$time = $lang->time2str("%A %e %B %Y à %X", $time);
}
#Ignore les commandes CLEARMSG, USERSTATE et HOSTARGET envoyé par twitch
sub ignore_cb {
return "";
}
#Déchargement de script
sub unload {
return weechat::WEECHAT_RC_OK;
}