-
Notifications
You must be signed in to change notification settings - Fork 0
/
pullmeta.js
58 lines (48 loc) · 1.84 KB
/
pullmeta.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
const axios = require("axios").default;
const download = require('download');
const fs = require('fs');
const { SPOTIFY_KEY } = require('./config.json');
module.exports = (trackName, trackAlbum) => {
var options = {
method: 'GET',
url: 'https://unsa-unofficial-spotify-api.p.rapidapi.com/search',
params: { query: `${trackName}, ${trackAlbum}`, count: '20', type: 'tracks' },
headers: {
'x-rapidapi-host': 'unsa-unofficial-spotify-api.p.rapidapi.com',
'x-rapidapi-key': SPOTIFY_KEY
}
};
axios.request(options).then(function (response) {
var name = response.data.Results[0].name;
var album = response.data.Results[0].album.name;
var artist = response.data.Results[0].artists[0].name;
var url = response.data.Results[0].external_urls.spotify;
var imageURL = response.data.Results[0].album.images[0].url;
var imageName = album.replace(/[^a-zA-Z0-9]/g, "").toLowerCase().substring(0,32);
if (imageName.length > 32) {
console.log("NIGHTMARE");
}
console.log(`Track found:\nTrack: ${name}\nAlbum: ${album}`);
try {
if (!fs.existsSync(`${__dirname}/covers/${imageName}.jpg`)) {
dlAlbumCover(imageName, imageURL);
};
} catch(err) {
console.error(err);
};
const spawn = require("child_process").spawn;
spawn('python', [__dirname + "./presence.py", name, album, artist, url, imageName]);
}).catch(function (error) {
console.error(error);
});
};
async function dlAlbumCover(name, imageURL) {
const filePath = `${__dirname}/covers`;
options = {
filename: `${name}.jpg`
};
download(imageURL, filePath, options)
.then(() => {
console.log('Download Completed');
});
};