-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (48 loc) · 1.91 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
var SpotifyWebHelper = require('spotify-web-helper');
const request = require('request-promise');
const http = require('http');
const api = require('genius-api');
const secure = require('./secure');
const genius = new api(secure.key);
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
let helper = SpotifyWebHelper({port: 4380});
let website;
let exclusionArray = require('./exclusions.json');
helper.player.on('ready', () => {
helper.player.on('track-will-change', track => {
return track.artist_resource ? getFromGenius(`${track.artist_resource.name} ${track.track_resource.name}`) : console.log("I couldn't find the song.");
});
});
function isInArray(string, array) {
for(i = 0;i<array.length;i++) {
if(string.toLowerCase().includes(array[i].toLowerCase())) return true;
}
return false;
}
function getFromGenius(query) {
process.stdout.write('\n'.repeat(100));
genius.search(query).then((response) => {
let index = 0;
if(!response.hits[index]) {
website = null;
}
while(response.hits[index] && isInArray(response.hits[index].result.full_title, exclusionArray)) {
index += 1
}
songObject = response.hits[index] ? response.hits[index].result : null;
website = songObject ? `https://genius.com${songObject.path}` : null;
}).then(() => makeRequest(website)).catch(reason => console.log(reason));
}
function makeRequest(website) {
if(website == null) return console.log("I couldn't find the song.");
request({uri: website}, (e,r,b) => {
doc = new JSDOM(b).window.document
console.log(`${doc.querySelector("h1").textContent} by ${doc.querySelector("h2").textContent.trim()}`);
console.log("---");
return console.log(doc.querySelector("p").textContent);
});
}
helper.player.on('error', err => {
if(err.message.match(/No user logged in/)) return process.exit(0);
});