-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (75 loc) · 2.73 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
const axios = require('axios');
const readline = require('readline');
const fs = require('fs');
const path = require('path');
// Create readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function sanitizeFilename(name) {
return name.replace(/[/\\?%*:|"<>]/g, '');
}
function askForAlbum() {
rl.question('Which album cover do you want to download? : ', (albumName) => {
// Send request to iTunes API
const url = `https://itunes.apple.com/search?term=${encodeURIComponent(albumName)}&country=us&entity=album&limit=1`;
axios.get(url)
.then(response => {
const albums = response.data.results;
if (albums.length > 0) {
const album = albums[0];
let artworkUrl = album.artworkUrl100;
// Modify the URL
artworkUrl = artworkUrl.replace('https://is1-ssl.mzstatic.com/image/thumb/', 'https://a5.mzstatic.com/us/r1000/0/');
artworkUrl = artworkUrl.replace('/100x100bb.jpg', '');
// Sanitize the album and artist names
const sanitizedCollectionName = sanitizeFilename(album.collectionName);
const sanitizedArtistName = sanitizeFilename(album.artistName);
// Create file name
const fileName = `${sanitizedCollectionName} - ${sanitizedArtistName}.jpg`;
const filePath = path.join(__dirname, 'covers', fileName);
// Create covers directory if it doesn't exist
if (!fs.existsSync(path.join(__dirname, 'covers'))) {
fs.mkdirSync(path.join(__dirname, 'covers'));
}
// Download and save the image
axios({
url: artworkUrl,
responseType: 'stream'
}).then(response => {
response.data.pipe(fs.createWriteStream(filePath))
.on('finish', () => {
console.log(`Album cover saved: ${fileName}`);
askToContinue();
})
.on('error', (error) => {
console.error('An error occurred while saving the file:', error);
askToContinue();
});
}).catch(error => {
console.error('An error occurred while downloading the image:', error);
askToContinue();
});
} else {
console.log('Album not found.');
askToContinue();
}
})
.catch(error => {
console.error('An error occurred:', error);
askToContinue();
});
});
}
function askToContinue() {
rl.question('Do you want to download another album cover? (Y/N): ', (answer) => {
if (answer.toLowerCase() === 'y') {
askForAlbum();
} else {
rl.close();
}
});
}
// Start by asking for the first album
askForAlbum();