-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
137 lines (122 loc) · 3.15 KB
/
main.js
File metadata and controls
137 lines (122 loc) · 3.15 KB
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
import NodeID3 from 'node-id3-promise'
import axios from 'axios'
import { promises as fs, createWriteStream } from 'fs'
import path from 'path'
import urlencode from 'urlencode'
const baseUrl = "https://api.deezer.com"
main()
async function main() {
const file = process.argv[2]
try {
const stat = await fs.stat(file)
if (stat.isDirectory())
await processDir(file)
else if (stat.isFile())
await processFile(file)
else
console.error('The file must be a MP3 file or a directory!')
} catch (err) {
console.error(err)
}
}
async function processDir(dirName) {
console.log(dirName)
try {
const files = await fs.readdir(dirName)
for (const file of files) {
try {
console.log(dirName + '\\' + file)
await processFile(dirName + '\\' + file)
} catch(err) {
// ignored because not a MP3 file
}
}
} catch (err) {
console.error(err)
}
}
async function processFile(file = '') {
if (typeof file !== 'string' || file.substring(file.indexOf('.') + 1) !== 'mp3')
throw new Error('The file must be a MP3 file or a directory!')
try {
// Get filename
const fileName = path.basename(file, '.mp3')
console.log(`Processing: ${fileName}`)
// Search for music
const resp = await search(fileName)
const music = toMusic(resp.data.data[0])
// Request image
const imgFile = fileName + '.jpg'
const img = await getImage(music.album.cover_medium)
img.data.pipe(createWriteStream(imgFile))
// Request album
const { data: album } = await getAlbum(music.album.id)
const trackNumber = album.tracks.data.findIndex(track => track.title === music.title) + 1
const genre = album.genres.data[0].name
const year = new Date(album.release_date).getFullYear()
const tags = {
title: music.title,
artist: music.artist.name,
album: music.album.title,
genre,
year,
trackNumber,
APIC: imgFile
}
await NodeID3.write(tags, file)
// Delete image file
await fs.unlink(imgFile)
} catch (err) {
console.error(err)
}
}
function search(str) {
const req = {
method: 'get',
url: baseUrl + '/search',
params: { q: urlencode(str) }
}
return axios(req)
}
function getImage(url) {
const req = {
method: 'get',
url,
responseType: 'stream'
}
return axios(req)
}
function getAlbum(id) {
const req = {
method: 'get',
url: baseUrl + '/album/' + id
}
return axios(req)
}
function toMusic({
id,
title: musicTitle,
artist: {
id: artistId,
name
},
album: {
id: albumId,
title,
cover_medium
}
}) {
return {
id,
title: musicTitle,
artist: {
id: artistId,
name
},
album: {
id: albumId,
title,
cover_medium
}
}
}