-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (65 loc) · 2.15 KB
/
app.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
var echojs = require('echojs');
var express = require('express');
var app = express();
var music = require('musicmatch')({usertoken:'014c6eff84790b6561117bb2cba33918'});
var lyricBank = require('./lyricBank');
var bodyParser = require('body-parser');
var fs=require("fs");
var router=require('./routes');
app.use('/', express.static(__dirname));
// app.use('/', makesRouter);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.listen(process.env.PORT || 3000);
var artistArr =["kendrick lamar","kanye west", "snoop dogg", "eminem", "nas"];
var countArr=[];
//for (var j=0;j<artistArr.length;j++) {
var j=0;
return music.trackSearch({q: artistArr[j], f_has_lyrics: true, page_size: 100 })
.then(function(data) {
return data.message.body.track_list.map(function(index) {
return index.track.track_id;
});
})
.then(function(array){
return Promise.all(array.map(function(element) {
return music.trackLyrics({ track_id: element });
}));
})
.then(function(lyrics){
return lyrics.map(function(element) {
return element.message.body.lyrics.lyrics_body;
});
})
.then(function(result) {
lyricBank.data.push({artist: artistArr[j], lyrics: result});
return lyricBank.data;
})
.then(function(artist){
var songStrLen="";
for (var prop in artist[j].lyrics)
songStrLen += artist[j].lyrics[prop];
songStrLen=countWords(songStrLen);
countArr.push({artist: artist[j].artist, wordLength: songStrLen});
return countArr;
})
.then(function(final){
//console.log(final)
return final;
})
.catch(function(err) {
console.error(err);
});
//}
function countWords(s){
s = s.replace(/(^\s*)|(\s*$)/gi,"");//exclude start and end white-space
s = s.replace(/[ ]{2,}/gi," ");//2 or more space to 1
s = s.replace(/\n /,"\n"); // exclude newline with a start spacing
var wordArray = s.split(' ');
var wordAvg = 0;
for (var i = 0; i < wordArray.length; i++){
wordAvg += wordArray[i].length;
}
var avgLen = wordAvg / wordArray.length;
return avgLen;
}