-
Notifications
You must be signed in to change notification settings - Fork 52
/
util.js
155 lines (128 loc) · 5.68 KB
/
util.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
var async = require('async');
var rest = require('restler');
var config = require('./config');
var DEBUG = false;
var TRACK_SEPARATOR = ' - ';
function parseTitleString(string, partsCallback) {
var artist, title, credits = [];
var string = string || '';
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
// TODO: load from datafile
var baddies = ['[dubstep]', '[electro]', '[edm]', '[house music]',
'[glitch hop]', '[video]', '[official video]', '(official video)',
'(official music video)', '(lyrics)',
'[ official video ]', '[official music video]', '[free download]',
'[official]', '[electronica]', '[electronica | plasmapool]',
'[free dl]', '( 1080p )', '(with lyrics)', '(high res / official video)',
'(music video)', '[music video]', '[hd]', '(hd)', '[hq]', '(hq)',
'(original mix)', '[original mix]', '[lyrics]', '[free]', '[trap]',
'[monstercat release]', '[monstercat freebie]', '[monstercat]',
'[edm.com premeire]', '[edm.com exclusive]', '[enm release]',
'[free download!]', '[monstercat free release]', '[official lyric video]'];
baddies.forEach(function(token) {
string = string.replace(token + ' - ', '').trim();
string = string.replace(token.toUpperCase() + ' - ', '').trim();
string = string.replace(token.toLowerCase() + ' - ', '').trim();
string = string.replace(token.capitalize() + ' - ', '').trim();
string = string.replace(token, '').trim();
string = string.replace(token.toUpperCase(), '').trim();
string = string.replace(token.toLowerCase(), '').trim();
string = string.replace(token.capitalize(), '').trim();
});
if (DEBUG) console.log('next string: ' +string );
var parts = string.split( ' - ' );
if (DEBUG) console.log(parts);
for (var i = 0; i < parts.length; i++) {
if ( baddies.indexOf( parts[i].toLowerCase() ) >= 0 ) {
parts.splice( i , 1 );
}
}
if (DEBUG) console.log(parts);
if (parts.length == 2) {
artist = parts[0];
title = parts[1];
} else if (parts.length > 2) {
// uh...
artist = parts[0];
title = parts[1];
} else {
artist = parts[0];
title = parts[0];
}
// one last pass
baddies.forEach(function(baddy) {
title = title.replace( new RegExp( escapeRegExp(baddy) , 'i') , '').trim();
artist = artist.replace( new RegExp( escapeRegExp(baddy) , 'i') , '').trim();
});
// look for certain patterns in the string
credits.push( title.replace(/(.*)\((.*) remix\)/i, '$2').trim() );
credits.push( title.replace(/(.*) ft\.? (.*)/i, '$1').trim() );
credits.push( title.replace(/(.*) ft\.? (.*)/i, '$2').trim() );
credits.push( title.replace(/(.*) feat\.? (.*)/i, '$1').trim() );
credits.push( title.replace(/(.*) feat\.? (.*)/i, '$2').trim() );
credits.push( title.replace(/(.*) featuring (.*)/i, '$2').trim() );
credits.push( title.replace(/(.*) \(ft (.*)\)/i, '$1').trim() );
credits.push( title.replace(/(.*) \(ft (.*)\)/i, '$2').trim() );
credits.push( title.replace(/(.*) \(feat\.? (.*)\)/i, '$2').trim() );
credits.push( title.replace(/(.*) \(featuring (.*)\)/i, '$2').trim() );
credits.push( title.replace(/(.*) \(produced by (.*)\)/i,'$2').trim() );
credits.push( artist.replace(/(.*) ft\.? (.*)/i, '$1').trim() );
credits.push( artist.replace(/(.*) ft\.? (.*)/i, '$2').trim() );
credits.push( artist.replace(/(.*) feat\.? (.*)/i, '$1').trim() );
credits.push( artist.replace(/(.*) feat\.? (.*)/i, '$2').trim() );
credits.push( artist.replace(/(.*) featuring (.*)/i, '$2').trim() );
credits.push( artist.replace(/(.*) \(ft (.*)\)/i, '$1').trim() );
credits.push( artist.replace(/(.*) \(ft (.*)\)/i, '$2').trim() );
credits.push( artist.replace(/(.*) \(feat\.? (.*)\)/i, '$1').trim() );
credits.push( artist.replace(/(.*) \(featuring (.*)\)/i, '$2').trim() );
credits.push( artist.replace(/(.*) & (.*)/ig, '$1').trim() );
credits.push( artist.replace(/(.*) & (.*)/ig, '$2').trim() );
credits.push( artist.replace(/(.*) vs\.? (.*)/i, '$1').trim() );
credits.push( artist.replace(/(.*) vs\.? (.*)/i, '$2').trim() );
credits.push( artist.replace(/(.*) x (.*)/i, '$1').trim() );
credits.push( artist.replace(/(.*) x (.*)/i, '$2').trim() );
var creditMap = {};
credits.forEach(function(credit) {
if (credit !== title) { // temp., until we find out why title is in credits
creditMap[ credit ] = credit;
}
});
var output = {
artist: artist
, title: title
, credits: Object.keys(creditMap)
};
if (DEBUG) console.log('output parts: ' + JSON.stringify(output) );
/* console.log('artist: ' + artist);
console.log('title: ' + title);
console.log('credits: ' + credits);*/
partsCallback(output);
}
String.prototype.capitalize = function(){
return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};
module.exports = {
timeSeries: function(field, interval, skip, limit) {
var queries = [];
if (!interval) { var interval = 24; }
if (!skip) { var skip = 1000 * 60 * 60 * 24; }
if (!limit) { var limit = 24; }
var halfTime = interval / 2; // moving window
for (var i = 0; i <= limit; i++) {
var start = new Date();
var end = new Date( start.getTime() );
start = new Date( start - ((i+1) * skip) );
end = new Date( start.getTime() + interval );
var query = {};
query[ field ] = {
$gte: start
, $lt: end
};
queries.push( query );
}
return queries;
},
parseTitleString: parseTitleString
}