This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
make-xml.js
185 lines (158 loc) · 4.75 KB
/
make-xml.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
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
var fs = require('fs');
var async = require('async');
var builder = require('xmlbuilder');
var mp3Duration = require('mp3-duration');
var md5File = require('md5-file')
var workList = [];
var missBGList = [];
var missBKList = [];
var missLyricList = [];
var missMp3HQList = [];
var missMp3LQList = [];
var nullImage = md5File('./data/config/null.jpg');
var buildXML = function(){
var xml = builder.create('list',
{version: '1.0', encoding: 'UTF-8', standalone: true},
{pubID: null, sysID: null},
{allowSurrogateChars: false, skipNullAttributes: false,
headless: true, ignoreDecorators: false, stringify: {}})
for(var i=workList.length-1;i>=0;i--){
if(typeof(workList[i])=="undefined"){
console.log('Error: id '+i+' is not found')
continue;
}
var haveBg = true;
//test item
if(!fs.existsSync('./data/bg/'+workList[i].music_id+'.jpg')){
missBGList.push(workList[i]);
haveBg = false;
}else{
if(md5File('./data/bg/'+workList[i].music_id+'.jpg')==nullImage){
missBGList.push(workList[i]);
haveBg = false;
}
}
if(haveBg){
var o = {
'type':workList[i].type,
'music_id':workList[i].music_id,
'duration':workList[i].duration,
'label':workList[i].label,
'lrc':'lrc/'+workList[i].music_id+'.txt',
'bg_video':'bk91/'+workList[i].music_id+'.jpg',
'bg':'bg.php?id='+workList[i].music_id+'',
'dl_link':workList[i].dl_link
};
xml.ele('m',o);
}else{
var o = {
'type':workList[i].type,
'music_id':workList[i].music_id,
'duration':workList[i].duration,
'label':workList[i].label,
'lrc':'lrc/'+workList[i].music_id+'.txt',
'bg_video':'bk91/'+workList[i].music_id+'.jpg',
'bg':'bg.php?id=0',
'dl_link':workList[i].dl_link
};
xml.ele('m',o);
}
if(!fs.existsSync('./data/bk/'+workList[i].music_id+'.jpg')){
missBKList.push(workList[i]);
}
if(!fs.existsSync('./data/lyric/'+workList[i].music_id+'.txt')){
missLyricList.push(workList[i]);
}
if(!fs.existsSync('./data/mp3/'+workList[i].music_id+'.mp3')){
missMp3HQList.push(workList[i]);
}
if(!fs.existsSync('./data/mp3-lq/'+workList[i].music_id+'.mp3')){
missMp3LQList.push(workList[i]);
}
}
var xmlString = xml.end({ pretty: true, indent: ' ', newline: '\r\n' });
//console.log(xmlString);
fs.writeFile('./gen/list.xml',xmlString,function(err){
if(err){
console.log('error in writing list.xml');
}else{
console.log('xml created succeed!')
}
});
fs.writeFile('./gen/miss-bg.json',JSON.stringify(missBGList, null, 4),function(err){
if(err){
console.log('error in writing miss-bg.json');
}else{
console.log('miss-bg.json created succeed!')
}
});
fs.writeFile('./gen/miss-bk.json',JSON.stringify(missBKList, null, 4),function(err){
if(err){
console.log('error in writing miss-bk.json');
}else{
console.log('miss-bk.json created succeed!')
}
});
fs.writeFile('./gen/miss-lyric.json',JSON.stringify(missLyricList, null, 4),function(err){
if(err){
console.log('error in writing miss-lyric.json');
}else{
console.log('miss-lyric.json created succeed!')
}
});
fs.writeFile('./gen/miss-mp3-hq.json',JSON.stringify(missMp3HQList, null, 4),function(err){
if(err){
console.log('error in writing miss-mp3-hq.json');
}else{
console.log('miss-mp3-hq.json created succeed!')
}
});
fs.writeFile('./gen/miss-mp3-lq.json',JSON.stringify(missMp3LQList, null, 4),function(err){
if(err){
console.log('error in writing miss-mp3-lq.json');
}else{
console.log('miss-mp3-lq.json created succeed!')
}
});
}
var scanJSON = function(){
var path = './data/json';
var files = fs.readdirSync(path);
for(var i=0;i<files.length;i++) {
var item = files[i];
var tmpPath = path + '/' + item,
stats = fs.statSync(tmpPath);
if (!stats.isDirectory()) {
var name = item.split('.',2);
if(name[1]=='json'){
workList[Number(name[0])-1] = JSON.parse(fs.readFileSync(tmpPath, 'utf8'));
workList[Number(name[0])-1].music_id = Number(name[0]);
}
}
};
// scan and update duration
async.mapLimit(workList,20,function(obj,callback){
var id = obj.music_id;
if (typeof(obj.duration) == "undefined"){
mp3Duration('./data/mp3-lq/'+id+'.mp3', function (err, duration) {
if (err){
workList[Number(id)-1].duration = 0;
console.log(err.message);
return callback();
}
console.log(id+'.mp3 duration:' + duration);
workList[Number(id)-1].duration = Math.floor(Number(duration));
//cache the duration
var o = JSON.parse(fs.readFileSync('./data/json/'+id+'.json', 'utf8'));
o.duration = Math.floor(Number(duration));
fs.writeFileSync('./data/json/'+id+'.json',JSON.stringify(o));
return callback();
});
}else{
return callback();
}
},function(){
buildXML();
});
}
scanJSON();