FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
MediaBot/src/modules/http_api.js at main · SimosNap/MediaBot · GitHub
SimosNap
MediaBot
Repository navigation
Code
Issues
9
(9)
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
MediaBot
/
src
/
modules
/
http_api.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
775 lines (613 loc) · 26.3 KB
Breadcrumbs
MediaBot
/
src
/
modules
/
http_api.js
Copy path
File metadata and controls
775 lines (613 loc) · 26.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
const
Koa
=
require
(
'koa'
)
;
const
KoaRouter
=
require
(
'koa-router'
)
;
const
koaBody
=
require
(
'koa-body'
)
;
const
fetch
=
require
(
'node-fetch'
)
;
const
utils
=
require
(
'../misc/utils.js'
)
;
const
Channel
=
require
(
'../lib/channel'
)
;
const
ircPromises
=
require
(
'../lib/ircPromises'
)
;
const
Yourls
=
require
(
'node-yourls/yourls'
)
;
require
(
'irc-colors'
)
.
global
(
)
;
module
.
exports
=
class
HttpAPI
{
shortenURL
(
url
,
title
)
{
return
new
Promise
(
(
resolve
,
reject
)
=>
{
this
.
shortener
.
shorten
(
url
,
title
,
(
error
,
result
)
=>
{
if
(
error
)
{
return
reject
(
error
)
;
}
return
resolve
(
result
)
;
}
,
)
;
}
)
;
}
constructor
(
bot
,
config
,
channels
,
dbCon
)
{
const
yourlsUrl
=
config
.
yourls_url
;
const
yourlsApi
=
config
.
yourls_api
;
this
.
shortener
=
new
Yourls
(
yourlsUrl
,
yourlsApi
)
;
this
.
bot
=
bot
;
this
.
channels
=
channels
;
this
.
dbCon
=
dbCon
;
this
.
inProgress
=
Object
.
create
(
null
)
;
this
.
koa
=
new
Koa
(
)
;
this
.
koa
.
use
(
koaBody
(
)
)
;
this
.
api
=
new
KoaRouter
(
{
prefix
:
'/api'
,
}
)
;
this
.
api
.
post
(
'/add/bot'
,
this
.
handleAddBot
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/rem/bot'
,
this
.
handleRemBot
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/radio'
,
this
.
handleSetRadio
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/radio/advertise'
,
this
.
handleSetRadioAdv
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/radio/nowplay'
,
this
.
handleSetRadioNp
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/radio/requests'
,
this
.
handleSetRadioReq
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/youtube'
,
this
.
handleSetYoutube
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/mixcloud'
,
this
.
handleSetMixcloud
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/set/rss'
,
this
.
handleSetRss
.
bind
(
this
)
)
;
this
.
api
.
post
(
'/select/feeds'
,
this
.
handleSlectFeeds
.
bind
(
this
)
)
;
this
.
koa
.
use
(
this
.
api
.
routes
(
)
)
;
this
.
koa
.
use
(
this
.
api
.
allowedMethods
(
)
)
;
this
.
koa
.
listen
(
3000
,
'127.0.0.1'
)
;
// this.koa.listen(3000);
}
async
handleAddBot
(
ctx
)
{
const
newChan
=
ctx
.
request
?.
body
?.
channel
;
if
(
this
.
inProgress
.
chanJoin
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'channel Join already in progress'
;
return
;
}
this
.
inProgress
.
chanJoin
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
chanJoin
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
newChan
)
{
return
failValidation
(
'channel required'
)
;
}
const
chan
=
this
.
channels
[
newChan
.
toLowerCase
(
)
]
;
if
(
chan
)
{
return
failValidation
(
'channel already exist'
)
;
}
try
{
const
chan
=
new
Channel
(
this
.
bot
,
newChan
)
;
this
.
channels
[
chan
.
name
.
toLowerCase
(
)
]
=
chan
;
const
addedChan
=
await
ircPromises
.
joinChan
(
this
.
bot
,
chan
,
this
.
dbCon
)
;
ctx
.
response
.
status
=
200
;
ctx
.
response
.
body
=
{
channel
:
addedChan
}
;
}
catch
(
err
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
err
;
}
delete
this
.
inProgress
.
chanJoin
;
}
async
handleRemBot
(
ctx
)
{
const
remChan
=
ctx
.
request
?.
body
?.
channel
;
if
(
this
.
inProgress
.
chanPart
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'channel remove already in progress'
;
return
;
}
this
.
inProgress
.
chanPart
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
chanPart
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
remChan
)
{
return
failValidation
(
'channel required'
)
;
}
const
chan
=
this
.
channels
[
remChan
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'channel dont exist'
)
;
}
try
{
const
removedChan
=
await
ircPromises
.
partChan
(
this
.
bot
,
chan
,
this
.
dbCon
)
;
delete
this
.
channels
[
chan
.
name
]
;
ctx
.
response
.
status
=
200
;
ctx
.
response
.
body
=
{
channel
:
removedChan
}
;
}
catch
(
err
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
err
;
}
delete
this
.
inProgress
.
chanPart
;
}
async
handleSetRadio
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
radioname
=
ctx
.
request
?.
body
?.
radioname
;
const
motd
=
ctx
.
request
?.
body
?.
motd
;
const
source
=
ctx
.
request
?.
body
?.
source
;
const
icestats
=
ctx
.
request
?.
body
?.
icestats
;
const
logo
=
ctx
.
request
?.
body
?.
logo
;
const
website
=
ctx
.
request
?.
body
?.
website
;
const
twitch
=
ctx
.
request
?.
body
?.
twitch
;
if
(
this
.
inProgress
.
setRadio
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Radio station setting already in progress'
;
return
;
}
this
.
inProgress
.
setRadio
=
true
;
let
update
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
setRadio
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
source
)
{
return
failValidation
(
'missing source'
)
;
}
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
if
(
!
utils
.
isValidSecureURL
(
source
)
)
{
return
failValidation
(
'source need https'
)
;
}
if
(
!
chan
.
source
)
{
for
(
const
channelObj
of
Object
.
values
(
this
.
channels
)
)
{
if
(
(
channelObj
.
source
)
&&
(
channelObj
.
source
.
toLowerCase
(
)
===
source
.
toLowerCase
(
)
)
&&
(
channelObj
.
name
.
toLowerCase
(
)
!==
channel
.
toLowerCase
(
)
)
)
{
return
failValidation
(
'source must be unique'
)
;
}
}
update
=
false
;
}
if
(
!
radioname
)
{
return
failValidation
(
'missing radio name'
)
;
}
if
(
!
motd
)
{
return
failValidation
(
'missing radio description'
)
;
}
if
(
(
motd
.
length
<
40
)
||
(
motd
.
length
>
120
)
)
{
return
failValidation
(
'description min 40 chars max 120 chars'
)
;
}
if
(
!
icestats
)
{
return
failValidation
(
'missing icestats url'
)
;
}
if
(
!
utils
.
isValidSecureURL
(
icestats
)
)
{
return
failValidation
(
'icestats need https'
)
;
}
if
(
!
logo
)
{
return
failValidation
(
'missing logo url'
)
;
}
if
(
!
utils
.
isValidSecureURL
(
logo
)
)
{
return
failValidation
(
'logo need https'
)
;
}
const
shortener
=
await
this
.
shortenURL
(
'https://media.simosnap.com/player/'
+
chan
.
mbID
,
'Media Player '
+
radioname
)
;
if
(
(
website
)
&&
(
!
utils
.
isValidURL
(
website
)
)
)
{
return
failValidation
(
'not valid URL'
)
;
}
if
(
(
twitch
)
&&
(
!
utils
.
isValidSecureURL
(
'https://player.twitch.tv/?channel='
+
twitch
)
)
)
{
return
failValidation
(
'invalid twitch channel URL'
)
;
}
if
(
update
===
false
)
{
this
.
dbCon
.
query
(
'INSERT INTO magirc_mediabot_radio (id, channel, name, description, source, icestats, logo, website, twitch, nowplay, announce, timer, dj, requests) values (?,?,?,?,?,?,?,?,?,0,0,30,\'\',0)'
,
[
mbID
,
channel
,
radioname
,
motd
,
source
,
icestats
,
logo
,
website
,
twitch
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
radioname
=
radioname
;
chan
.
motd
=
motd
;
chan
.
source
=
source
;
chan
.
icestats
=
icestats
;
chan
.
logo
=
logo
;
chan
.
website
=
website
;
chan
.
twitch
=
twitch
;
if
(
chan
.
announce
===
1
)
{
if
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
;
}
const
timeoutID
=
setInterval
(
(
)
=>
{
const
prefix
=
(
'Ascolta '
+
chan
.
radioname
)
.
irc
.
teal
.
bold
(
)
;
const
suffix
=
(
'[https://ilnk.stream/'
+
shortener
.
url
.
keyword
+
']'
)
.
irc
.
teal
(
)
;
const
tagData
=
[
chan
.
radioname
,
chan
.
mbID
,
]
;
this
.
bot
.
say
(
chan
.
name
,
`🎛
${
prefix
}
-
${
chan
.
motd
}
${
suffix
}
`
,
{
'+simosnap.org/radio_station'
:
tagData
.
join
(
';'
)
}
)
;
}
,
(
60000
*
chan
.
timer
)
)
;
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
=
timeoutID
;
}
else
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
;
}
}
)
;
}
else
{
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_radio SET name = ?, description = ?, source = ?, icestats = ?, logo = ?, website = ?, twitch = ? WHERE id = ?'
,
[
radioname
,
motd
,
source
,
icestats
,
logo
,
website
,
twitch
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
radioname
=
radioname
;
chan
.
motd
=
motd
;
chan
.
source
=
source
;
chan
.
icestats
=
icestats
;
chan
.
logo
=
logo
;
chan
.
website
=
website
;
chan
.
twitch
=
twitch
;
if
(
chan
.
announce
===
1
)
{
if
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
;
}
const
timeoutID
=
setInterval
(
(
)
=>
{
const
prefix
=
(
'Ascolta '
+
chan
.
radioname
)
.
irc
.
teal
.
bold
(
)
;
const
suffix
=
(
'[https://ilnk.stream/'
+
shortener
.
url
.
keyword
+
']'
)
.
irc
.
teal
(
)
;
const
tagData
=
[
chan
.
radioname
,
chan
.
mbID
,
]
;
this
.
bot
.
say
(
chan
.
name
,
`🎛
${
prefix
}
-
${
chan
.
motd
}
${
suffix
}
`
,
{
'+simosnap.org/radio_station'
:
tagData
.
join
(
';'
)
}
)
;
}
,
(
60000
*
chan
.
timer
)
)
;
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
=
timeoutID
;
}
else
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
;
}
}
)
;
}
this
.
bot
.
tagmsg
(
chan
.
name
,
{
'+simosnap.org/radio_update'
:
1
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
setRadio
;
}
async
handleSetRadioAdv
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
announce
=
ctx
.
request
?.
body
?.
announce
;
const
timer
=
ctx
.
request
?.
body
?.
timer
;
if
(
this
.
inProgress
.
setAdv
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Advertise timer setting already in progress'
;
return
;
}
this
.
inProgress
.
setAdv
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
setAdv
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
const
shortener
=
await
this
.
shortenURL
(
'https://media.simosnap.com/player/'
+
chan
.
mbID
,
'Media Player '
+
chan
.
radioname
)
;
if
(
isNaN
(
announce
)
)
{
return
failValidation
(
'not number'
)
;
}
if
(
isNaN
(
timer
)
)
{
return
failValidation
(
'not number'
)
;
}
if
(
timer
<
10
)
{
return
failValidation
(
'timer min interval 10 min'
)
;
}
if
(
timer
>
50
)
{
return
failValidation
(
'timer max interval 50 min'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_radio SET announce = ?, timer = ? WHERE id = ?'
,
[
announce
,
timer
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
announce
=
parseInt
(
announce
)
;
chan
.
timer
=
timer
;
// chan.timer = 1;
if
(
chan
.
announce
===
1
)
{
if
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
;
}
const
timeoutID
=
setInterval
(
(
)
=>
{
const
prefix
=
(
'Ascolta '
+
chan
.
radioname
)
.
irc
.
teal
.
bold
(
)
;
const
suffix
=
(
'[https://ilnk.stream/'
+
shortener
.
url
.
keyword
+
']'
)
.
irc
.
teal
(
)
;
const
tagData
=
[
chan
.
radioname
,
chan
.
mbID
,
]
;
this
.
bot
.
say
(
chan
.
name
,
`🎛
${
prefix
}
-
${
chan
.
motd
}
${
suffix
}
`
,
{
'+simosnap.org/radio_station'
:
tagData
.
join
(
';'
)
}
)
;
}
,
(
60000
*
chan
.
timer
)
)
;
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
=
timeoutID
;
}
else
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
jobs
[
chan
.
name
]
;
}
}
)
;
this
.
bot
.
tagmsg
(
chan
.
name
,
{
'+simosnap.org/radio_update'
:
1
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
setAdv
;
}
async
handleSetRadioNp
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
nowplay
=
ctx
.
request
?.
body
?.
nowplay
;
if
(
this
.
inProgress
.
nowPlay
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Nowplay setting already in progress'
;
return
;
}
this
.
inProgress
.
nowPlay
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
nowPlay
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
const
shortener
=
await
this
.
shortenURL
(
'https://media.simosnap.com/player/'
+
chan
.
mbID
,
'Media Player '
+
chan
.
radioname
)
;
if
(
isNaN
(
nowplay
)
)
{
return
failValidation
(
'not number'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_radio SET nowplay = ? WHERE id = ?'
,
[
nowplay
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
nowplay
=
parseInt
(
nowplay
)
;
if
(
chan
.
nowplay
===
1
)
{
if
(
this
.
bot
.
modules
[
'radio.js'
]
.
playjobs
[
chan
.
name
]
)
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
playjobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
playjobs
[
chan
.
name
]
;
}
const
timeoutID
=
setInterval
(
async
(
)
=>
{
const
json
=
await
fetch
(
chan
.
icestats
)
.
then
(
(
r
)
=>
r
.
json
(
)
)
;
if
(
!
json
)
{
return
;
}
const
prefix
=
(
'Adesso su '
+
chan
.
radioname
)
.
irc
.
teal
.
bold
(
)
;
const
suffix
=
(
'[https://ilnk.stream/'
+
shortener
.
url
.
keyword
+
']'
)
.
irc
.
teal
(
)
;
const
source
=
(
Array
.
isArray
(
json
.
icestats
.
source
)
)
?
json
.
icestats
.
source
[
0
]
:
json
.
icestats
.
source
;
const
artist
=
source
.
artist
;
const
song
=
source
.
artist
;
const
nowplaying
=
source
.
yp_currently_playing
;
const
bitrate
=
source
.
bitrate
;
const
colorizedBitrate
=
(
bitrate
+
' Kb/s'
)
.
irc
.
red
(
)
;
const
listeners
=
source
.
listeners
;
const
colorizedListeners
=
listeners
.
toString
(
)
.
irc
.
red
(
)
;
const
tagData
=
[
nowplaying
,
bitrate
,
chan
.
radioname
,
chan
.
mbID
,
listeners
,
]
;
this
.
bot
.
say
(
chan
.
name
,
`🎛
${
prefix
}
-
${
nowplaying
}
-
${
colorizedBitrate
}
-
${
colorizedListeners
}
ascoltatori
${
suffix
}
`
,
{
'+simosnap.org/radio_stream'
:
tagData
.
join
(
';'
)
}
)
;
// this.bot.say(chan.name, '[ Adesso su ' + chan.radioname + ' ] ' + nowplaying + ' https://media.simosnap.com/player/' + chan.mbID, { '+simosnap.org/radio_stream': tagData.join(';') });
}
,
(
60000
*
6
)
)
;
this
.
bot
.
modules
[
'radio.js'
]
.
playjobs
[
chan
.
name
]
=
timeoutID
;
}
else
{
clearInterval
(
this
.
bot
.
modules
[
'radio.js'
]
.
playjobs
[
chan
.
name
]
)
;
delete
this
.
bot
.
modules
[
'radio.js'
]
.
playjobs
[
chan
.
name
]
;
}
}
)
;
this
.
bot
.
tagmsg
(
chan
.
name
,
{
'+simosnap.org/radio_update'
:
1
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
nowPlay
;
}
async
handleSetRadioReq
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
dj
=
ctx
.
request
?.
body
?.
dj
;
const
requests
=
ctx
.
request
?.
body
?.
requests
;
if
(
this
.
inProgress
.
setReq
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Requests setting already in progress'
;
return
;
}
this
.
inProgress
.
setReq
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
setReq
;
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
if
(
requests
===
'1'
)
{
if
(
!
dj
)
{
return
failValidation
(
'dj must be set to enable requests'
)
;
}
if
(
dj
.
length
===
0
)
{
return
failValidation
(
'dj must be set to enable requests'
)
;
}
const
user
=
chan
.
getUser
(
dj
)
;
if
(
!
user
)
{
return
failValidation
(
'dj need to match a connected nickname '
)
;
}
}
if
(
isNaN
(
requests
)
)
{
return
failValidation
(
'not number'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_radio SET dj = ? , requests = ? WHERE id = ?'
,
[
dj
,
requests
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
dj
=
dj
;
chan
.
requests
=
parseInt
(
requests
)
;
}
)
;
this
.
bot
.
tagmsg
(
chan
.
name
,
{
'+simosnap.org/radio_update'
:
1
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
setReq
;
}
async
handleSetYoutube
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
enabled
=
ctx
.
request
?.
body
?.
enabled
;
if
(
this
.
inProgress
.
youtube
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Youtube setting already in progress'
;
return
;
}
this
.
inProgress
.
youtube
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
youtube
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
if
(
isNaN
(
enabled
)
)
{
return
failValidation
(
'not number'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_youtube SET enabled = ? WHERE id = ?'
,
[
enabled
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
youtube
=
parseInt
(
enabled
)
;
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
youtube
;
}
async
handleSetMixcloud
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
enabled
=
ctx
.
request
?.
body
?.
enabled
;
if
(
this
.
inProgress
.
mixcloud
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Mixcloud setting already in progress'
;
return
;
}
this
.
inProgress
.
mixcloud
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
mixcloud
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
if
(
isNaN
(
enabled
)
)
{
return
failValidation
(
'not number'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_mixcloud SET enabled = ? WHERE id = ?'
,
[
enabled
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
mixcloud
=
parseInt
(
enabled
)
;
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
mixcloud
;
}
async
handleSetRss
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
enabled
=
ctx
.
request
?.
body
?.
enabled
;
if
(
this
.
inProgress
.
rss
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'RSS setting already in progress'
;
return
;
}
this
.
inProgress
.
rss
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
rss
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
if
(
isNaN
(
enabled
)
)
{
return
failValidation
(
'not number'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_rss SET enabled = ? WHERE id = ?'
,
[
enabled
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
rss
=
parseInt
(
enabled
)
;
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
rss
;
}
async
handleSlectFeeds
(
ctx
)
{
const
mbID
=
ctx
.
request
?.
body
?.
mbID
;
const
channel
=
ctx
.
request
?.
body
?.
channel
;
const
feeds
=
ctx
.
request
?.
body
?.
feed
;
if
(
this
.
inProgress
.
feeds
)
{
ctx
.
response
.
status
=
500
;
ctx
.
response
.
body
=
'Feed setting already in progress'
;
return
;
}
this
.
inProgress
.
feed
=
true
;
const
failValidation
=
(
message
)
=>
{
delete
this
.
inProgress
.
feed
;
ctx
.
response
.
body
=
JSON
.
stringify
(
{
error
:
message
}
)
;
// ctx.throw(500, JSON.stringify({error: message}), { expose: true });
}
;
if
(
!
channel
)
{
return
failValidation
(
'missing channel'
)
;
}
if
(
!
mbID
)
{
return
failValidation
(
'missing id'
)
;
}
if
(
!
channel
.
match
(
/
^
#
(
.
+
)
$
/
)
)
{
return
failValidation
(
'invalid channel'
)
;
}
const
chan
=
this
.
channels
[
channel
.
toLowerCase
(
)
]
;
if
(
!
chan
)
{
return
failValidation
(
'not in the channel'
)
;
}
let
feedsVal
=
''
;
if
(
!
feeds
)
{
feedsVal
=
''
;
}
else
{
feedsVal
=
(
typeof
feeds
===
'string'
)
?
feeds
:
feeds
.
join
(
'|'
)
;
}
this
.
dbCon
.
query
(
'UPDATE magirc_mediabot_rss SET rss = ? WHERE id = ?'
,
[
feedsVal
,
mbID
]
,
(
error
,
results
,
fields
)
=>
{
if
(
error
)
throw
error
;
chan
.
subscriptions
=
feedsVal
;
}
)
;
ctx
.
response
.
body
=
ctx
.
request
.
body
;
delete
this
.
inProgress
.
feed
;
}
}
;
Back
|
FazBrowse Home
|
New Git URL