-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3666khz.js
124 lines (113 loc) · 4.87 KB
/
3666khz.js
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
registerPlugin({
name: 'Radio 3666khz Songtext-Anzeige',
version: '1.0.1',
description: 'Zeigt in der Kanalbeschreibung den aktuell gespielten Titel an',
author: 'Michael H. <[email protected]>',
requiredModules: ['http'],
vars: {
id1: {
title: 'Select Channel',
type: 'channel',
},
interval: {
title: 'Intervall in Sekunden',
type: 'number',
placeholder: '60'
},
description: {
title: 'Original Channel Description',
type: 'multiline'
}
}
}, (_, config, meta) => {
// import modules
const engine = require('engine');
const http = require('http');
const backend = require('backend');
const media = require('media');
var description = '';
var length = 60;
// Channel Description static
/* var description = "\
[center][img]https://gateway-deutschland.de/media/images/route666_small.jpg[/img]\n\n\
[size=13][b][i]Rock, Hamspirit und so...3666kHz.[/i][/b][/size]\n\n\
Hier gibt es das aussergewöhnliche Radioerlebnis der alten Schule. Sender der Kurzwellen Runde auf 3666khz. [URL]www.3666khz.de[/URL] Viel Spaß beim lauschen";
*/
waitForBackend(10, 3)
.then(() => {
engine.log(meta.name + ' V' + meta.version + ' > The script has loaded successfully!');
if (!config || !config.id1 || !config.interval) {
engine.log("Nicht Konfiguriert! Script beendet...");
return;
}
if (config.description) {
description = config.description;
}
// send request
setInterval(() => {
http.simpleRequest({
'method': 'GET',
'url': 'https://api.laut.fm/station/heiderocker/current_song',
'timeout': 6000,
}, function (error, response) {
if (error) {
engine.log("Error: " + error);
return;
}
if (response.statusCode != 200) {
engine.log("HTTP Error: " + response.status);
return;
}
// success!
const json = response.data.toString();
const obj = JSON.parse(json);
var channel_id1 = backend.getChannelByID(config.id1);
var track = media.getCurrentTrack();
/*try {
length = (obj.length + 2);
desc = description + ('\n\n[size=10][u]Sie hören:[/u] \n[i][b][color=red]' + obj.artist.name + '[color=black] - [color=blue]' + obj.title + '[/color][/b][/i]');
} catch {
length = config.interval;
desc = description;
};*/
desc = description + ('\n\n[size=10][u]Sie hören:[/u] \n[i][b][color=red]' + track.tempArtist() + '[color=black] - [color=blue]' + track.tempTitle() + '[/color][/b][/i]');
channel_id1.setDescription(desc);
if (track.duration() > 1) {
length = track.duration();
} else {
length = (config.interval * 1000);
}
});
}, length * 1000);
})
.catch(error => {
if (error === 'backend') {
engine.log(
meta.name + ' V' + meta.version + ' > The bot was not able to connect to the backend in time! To use this script, the bot needs to be connected to your TeamSpeak server. Make sure it can connect. Deactivating script...'
);
} else {
engine.log(meta.name + ' V' + meta.version + ' > Unknown error occured! Please report this to the script author.');
console.log(error);
}
});
function waitForBackend(attempts, wait) {
return new Promise((success, fail) => {
let attempt = 1;
const timer = setInterval(() => {
setTimeout(() => { backend.connect(); }, 3500);
if (backend.isConnected()) {
clearInterval(timer);
if (config.dev) engine.log('waitForBackend() took ' + attempt + ' attempts with a timer of ' + wait + ' seconds to resolve');
success();
return;
} else if (attempt > attempts) {
clearInterval(timer);
if (config.dev) engine.log('waitForBackend() failed at ' + attempt + '. attempt with a timer of ' + wait + ' seconds');
fail('backend');
return;
}
attempt++;
}, wait * 1000);
});
}
});