ytdownloader/tags.js

55 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const id3 = require('node-id3');
const path = require('path');
const chalk = require('chalk');
const axios = require('axios');
const request = require("request").defaults({ encoding: null });
const fs = require('fs');
const hasflag = require('has-flag');
const cp = require('child_process');
const filePath = path.join(__dirname, '/music/', process.argv[2]);
const artist = process.argv[3];
const song = process.argv[4];
console.log(chalk.yellow("🎵 Calling iTunes api..."));
axios.get(`https://itunes.apple.com/search?term=${artist} ${song}&entity=song`).then(res => {
request.get(res.data.results[0].artworkUrl100.replace('100x100', '3000x3000'), function(err, _res, body) {
axios.get(`https://itunes.apple.com/search?term=${artist} ${res.data.results[0].collectionName}&entity=album`).then(album => {
console.log(chalk.green("✅ Retreived information!"));
var tags = {
title: res.data.results[0].trackName,
artist: res.data.results[0].artistName,
performerInfo: album.data.results[0].artistName,
album: res.data.results[0].collectionName,
copyright: album.data.results[0].copyright,
year: new Date(res.data.results[0].releaseDate).getFullYear(),
image: {
mime: "jpeg",
type: {
id: 3,
name: "front cover"
},
imageBuffer: body
},
fileType: "mp3",
genre: res.data.results[0].primaryGenreName
};
console.log(chalk.yellow("✍🏻 Writing tags to mp3 file..."));
id3.update(tags, filePath, () => {
if (fs.existsSync(filePath) && process.platform === "darwin" && !hasflag("no-itunes")) {
console.log(chalk.yellow("🎵 Adding song to Apple Music library..."));
cp.execSync(`cp "${filePath}" "/Users/job/Music/Music/Media/Automatically\ Add\ to\ Music.localized"`);
}
console.log(chalk.green(` Successfully wrote ${process.argv[2]}.mp3!\n`));
process.exit();
})
})
})
})