-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (129 loc) · 4.13 KB
/
index.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
let log = require('npmlog'),
chalk = require('chalk');
let RA = require('./src/ra');
let Beatport = require('./src/beatport');
let Discogs = require('./src/discogs');
const LOG_PREFIX = 'club-me';
const FULL = false;
let Filter;
// let Filter = 'kaos';
function getInfos(party) {
let artists = party.artists;
let p = artists.map(a => {
return Beatport.getInfo(a)
.then(info => {
a.beatport = info;
return a;
})
.then(a => Discogs.getInfo(a))
.then(info => {
a.discogs = info;
return a;
});
});
return Promise
.all(p)
.then(infos => {
party.artists = infos;
return party;
});
}
function printParty(party) {
let search = [
party.name, party.club.name
].join(' ');
if (Filter) {
let regex = new RegExp(Filter, 'gi');
if (!regex.test(search)) return false;
}
process.stdout.write(`> ${chalk.bold(party.name)} @ ${party.club.name}\n`);
process.stdout.write(` ${chalk.green(party.price)} ${party.priceTag}\n`);
let genres = getGenres(party);
process.stdout.write(` ${genres.join(', ')}\n`);
if (!FULL) return process.stdout.write('\n');
party.artists.forEach(artist => {
process.stdout.write(` * ${chalk.bold(artist.name)}\n`);
if (artist.beatport) {
process.stdout.write(` Beatport: `);
process.stdout.write(artist.beatport.genres.join(', '));
process.stdout.write('\n');
}
if (artist.discogs) {
process.stdout.write(` Discogs: `);
process.stdout.write(artist.discogs.profile);
process.stdout.write('\n');
artist.discogs.albums.forEach(album => {
process.stdout.write(` - `);
process.stdout.write(`${album.title}`);
if (album.genres) {
process.stdout.write(` (${album.genres.join(', ')})`);
}
process.stdout.write(`\n`);
});
}
process.stdout.write('\n');
});
process.stdout.write('\n');
}
function getGenres(party) {
let genres = [];
party.artists.forEach(artist => {
if (artist.beatport) {
genres = [].concat(genres, artist.beatport.genres);
}
if (artist.discogs) {
artist.discogs.albums.forEach(a => {
genres = [].concat(genres, a.genres);
});
}
});
let genresObj = genres
.reduce((cur, g) => {
if (!cur[g]) cur[g] = 0;
cur[g]++;
return cur;
}, {});
let sortedGenres = Object.keys(genresObj)
.sort((a, b) => genresObj[b] - genresObj[a]);
let sortedGenresFmt = [].concat(sortedGenres);
if (genresObj[sortedGenres[0]] > genresObj[sortedGenres[1]]) {
sortedGenresFmt[0] = chalk.bold(sortedGenres[0]);
}
for (let i = 0; i < sortedGenres.length - 1; i++) {
if (genresObj[sortedGenres[i]] >= genresObj[sortedGenres[i+1]]*2) {
return sortedGenresFmt.slice(0, i+1);
}
}
return sortedGenres;
}
RA
.getParties()
.then(parties => {
let p = Promise.resolve([]);
parties
.slice(0, 20)
.forEach(party => {
p = p
.then(data => {
log.info(LOG_PREFIX, 'getting infos for', party.name);
return RA
.getParty(party)
.then(getInfos)
.then(d => {
printParty(d);
data.push(d);
return data;
});
});
});
return p;
})
.catch(e => setTimeout(() => {
throw e;
}));
process.on('unhandledRejection', (reason, p) => {
log.error(LOG_PREFIX, p, reason);
});
process.on('uncaughtException', err => {
log.error(LOG_PREFIX, err);
});