-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseRecord.js
158 lines (155 loc) · 4.53 KB
/
parseRecord.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
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
function padLeft(str, numChars = 4, char = ' ') {
return (Array.from({ length: numChars }).fill(char).join('') + str).slice(-1 * numChars)
}
function padRight(str, numChars = 4, char = ' ') {
return (str + Array.from({ length: numChars }).fill(char).join('')).slice(0, numChars)
}
let md = `| Station Name | Strem link |
| ------------------- | ------------------- |`;
const fs = require('fs'),
path = require('path'),
GETURLTOFILE = function(url, output) {
return new Promise(function(resolve, reject){
const options = new URL(url);
const file = fs.createWriteStream(output);
const https = require('node:https');
https.get(options, function(response){
if (response.statusCode !== 200) {
console.error(`Произошла ошибка: сервер отдал статус ${response.statusCode}`);
fs.unlinkSync(output);
reject();
return;
}
response.pipe(file);
}).on('error', function(e){
fs.unlinkSync(output);
console.error(e);
reject();
});
file.on('finish', () => {
file.close(() => {
resolve();
});
});
file.on('error', (err) => {
fs.unlinkSync(output);
console.error(err.message);
reject();
});
});
},
MAGICK = function(input, sufix, arg) {
return new Promise(function(resolve, reject){
const {exec} = require('child_process');
let app = 'magick',
id = input.split('.')[0],
name = path.normalize(path.join(__dirname, input)),
out = path.normalize(path.join(__dirname, `${id}_${sufix}.png`)),
ls,
a = [
`${app}`,
`"${name}"`,
'-fill',
'"#FF6000"',
'-colorize',
'100',
'-channel',
'RGBA',
'-fuzz',
'5%',
'-fill',
'white',
'-opaque',
'None',
'-alpha',
'off',
'-resize',
'180x180'
];
if(arg){
a.push('-gravity');
a.push('center');
a.push('-extent');
a.push('360x180');
}
a.push(`"${out}"`);
ls = exec(a.join(' '), (error, stdout, stderr) => {
if (error) {
reject(error);
} else if (stderr) {
reject(stderr);
} else {
resolve();
}
});
});
},
FAVICON = function(id){
return new Promise(function(resolve, reject){
const {exec} = require('child_process');
let app = 'magick',
name = path.normalize(path.join(__dirname, `${id}_icon.png`)),
out = path.normalize(path.join(__dirname, `${id}_favicon.png`)),
args = `${app} "${name}" -alpha on ( +clone -threshold -1 -negate -fill white -draw "circle 90,90 90,0" ) -compose copy_opacity -composite "${out}"`,
ls = exec(args, (error, stdout, stderr) => {
if (error) {
reject(error);
} else if (stderr) {
reject(stderr);
} else {
resolve();
}
});
});
};
GETURLTOFILE('https://www.radiorecord.ru/api/stations/', 'record.json').then(async function(res){
const s = fs.readFileSync ('record.json', {encoding: 'utf8'});
const result = JSON.parse(s);
fs.unlinkSync('record.json');
const stations = result.result.stations;
var obj = JSON.parse(fs.readFileSync('src/sources/data.json', 'utf8'));
const playlist = obj.stations || {};
for(let i = 0; i < stations.length - 1; ++i){
const station = stations[i];
const [dateValues, timeValues] = station.updated.split(' ');
const [day, month, year] = dateValues.split('.');
const [hours, minutes, seconds] = timeValues.split(':');
const icon = station.icon_fill_white;
const stream = station.stream_320;
const name = `Radio Record «${station.title}»`;
const ms = (new Date()).getMilliseconds();
const date = new Date(
+year,
+month - 1,
+day,
+hours,
+minutes,
+seconds
);
const id = date.getTime();
await GETURLTOFILE(icon, `${id}.png`);
await MAGICK(`${id}.png`, 'big', true);
let bigicon = fs.readFileSync(`${id}_big.png`, {encoding: 'base64'});
await MAGICK(`${id}.png`, 'icon', false);
await FAVICON(id);
let favicon = fs.readFileSync(`${id}_favicon.png`, {encoding: 'base64'});
fs.unlinkSync(`${id}.png`);
fs.unlinkSync(`${id}_big.png`);
fs.unlinkSync(`${id}_icon.png`);
fs.unlinkSync(`${id}_favicon.png`);
md += `\n| ${name} | ${stream} |`;
playlist[id] = {
"name": name,
"stream": stream,
"id": id,
"favicon": `data:image/png;base64,${favicon}`,
"image": `data:image/png;base64,${bigicon}`
};
console.log(name, "\n", date, id, stream, "\n");
}
obj.stations = playlist;
fs.writeFileSync('application/radio/data.json', JSON.stringify(obj, null, "\t"), {encoding: 'utf8'});
fs.writeFileSync('radiorecord.md', md, {encoding: 'utf8'});
}).catch(function(error){
console.log(error);
});