Added more functionality

This commit is contained in:
corner 2019-11-19 16:46:16 +01:00
parent e2e79af01e
commit 8c991fe062
5 changed files with 148 additions and 34 deletions

BIN
.DS_Store vendored

Binary file not shown.

38
.gitignore vendored
View File

@ -1,3 +1,39 @@
node_modules/
# Created by https://www.gitignore.io/api/angular
# Edit at https://www.gitignore.io/?templates=angular
### Angular ###
## Angular ##
# compiled output
dist/
tmp/
app/**/*.js
app/**/*.js.map
# dependencies
node_modules/
bower_components/
# IDEs and editors
.idea/
# misc
.sass-cache/
connect.lock/
coverage/
libpeerconnection.log/
npm-debug.log
testem.log
typings/
# e2e
e2e/*.js
e2e/*.map
#System Files
.DS_Store
#Output
music/
# End of https://www.gitignore.io/api/angular

97
main.js
View File

@ -3,31 +3,54 @@ const ytdl = require("ytdl-core");
const ffmpeg = require("fluent-ffmpeg");
const id3 = require('node-id3');
const axios = require('axios');
var request = require("request").defaults({ encoding: null });
const request = require("request").defaults({ encoding: null });
const chalk = require('chalk');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const terminalLink = require('terminal-link');
/**
* log function for logging in one shell line.
* @param something the thing that should be logged.
* @param {any} something the thing that should be logged.
*/
const log = (something) => {
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write(something);
}
const convertTime = (timeString /* '00:00:46.27' */) => {
/**
* convertTime converts a string to a number of seconds.
* @param {string} timeString like '00:00:46.27'
*/
const convertTime = (timeString) => {
const timeParts = timeString.split(':');
let seconds = Number(timeParts[2]);
seconds = seconds + (Number(timeParts[1]) * 60) + (Number(timeParts[0]) * 60 * 60);
return seconds;
};
if (process.argv.length !== 5) {
console.log(chalk.red('Not getting metadata for this song!'));
if (process.argv.find(v => v === "help" || v === "-h" || v === "--help")) {
console.log(chalk.bold("ytdownloader by Jobbel.nl\n"));
console.log(chalk.underline("Usage:"));
console.log(`node main.js ${chalk.italic("<YouTube video url> <?Artist name> <?Song name>")}\n`);
console.log("The arguments starting with <? are optional. If not specified the song won't have id3 tags written to it.");
process.exit();
}
ffmpeg.setFfmpegPath(__dirname + '/ffmpeg');
if (process.argv.length !== 5) {
console.log(chalk.red("⚠ Not getting metadata for this song; expected arguments are not given!"));
}
const ffmpegPath = path.join(__dirname + "/ffmpeg");
if (fs.existsSync(ffmpegPath)) {
// On linux, you require the command-line version of ffmpeg.
if (process.platform !== "linux") ffmpeg.setFfmpegPath(ffmpegPath);
} else {
console.error(chalk.red("❌ The FFmpeg executable wasn't found. See " + terminalLink("ffmpeg's website", "https://www.ffmpeg.org") + " for downloads."))
process.exit(1);
}
log(chalk.yellow(" Getting information from YouTube..."));
@ -50,13 +73,13 @@ ytdl.getInfo(process.argv[2], (err, info) => {
chalk.blue(
`⬇️ ${Math.floor(
(progress / Number(info.length_seconds)) * 100
)}% downloaded.`
)}% downloaded. (${p.targetSize}kB)`
)
);
})
.on('error', err => {
log(chalk.red('Error: ' + err.message))
process.exit();
console.error(chalk.red('❌ Found an error: ' + err.message));
process.exit(1);
})
.on("end", () => {
if (process.argv.length === 5) {
@ -67,34 +90,42 @@ ytdl.getInfo(process.argv[2], (err, info) => {
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, function(err, _res, body) {
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 => {
log(chalk.green("✅ Retreived information!"));
log(chalk.green("✅ Retreived information!"));
var tags = {
title: song,
artist: res.data.results[0].artistName,
album: res.data.results[0].collectionName,
image: {
mime: "jpeg",
type: {
id: 3,
name: "front cover"
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
},
imageBuffer: body
},
date: res.data.results[0].releaseDate,
fileType: "mp3",
genre: res.data.results[0].primaryGenreName
};
fileType: "mp3",
genre: res.data.results[0].primaryGenreName
};
log(chalk.yellow("✍🏻 Writing tags to mp3 file..."));
log(chalk.yellow("✍🏻 Writing tags to mp3 file..."));
id3.update(tags, path);
id3.update(tags, path, () => {
if (fs.existsSync(path) && process.platform === "darwin") {
log(chalk.yellow("🎵 Adding song to Apple Music library..."));
cp.execSync(`cp "${path}" "/Users/job/Music/Music/Media/Automatically\ Add\ to\ Music.localized"`);
}
log(chalk.green(` Successfully downloaded ${info.title}.mp3!\n`));
log(chalk.green(` Successfully downloaded ${info.title}.mp3!\n`));
process.exit();
process.exit();
});
});
});
});
} else {

46
package-lock.json generated
View File

@ -15,6 +15,14 @@
"uri-js": "^4.2.2"
}
},
"ansi-escapes": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.0.tgz",
"integrity": "sha512-EiYhwo0v255HUL6eDyuLrXEkTi7WwVCLAw+SeOQ7M7qdun1z1pum4DEm/nuqIVbPvi9RPPc9k9LbyBv6H0DwVg==",
"requires": {
"type-fest": "^0.8.1"
}
},
"ansi-styles": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
@ -433,6 +441,39 @@
"has-flag": "^3.0.0"
}
},
"supports-hyperlinks": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/supports-hyperlinks/-/supports-hyperlinks-2.0.0.tgz",
"integrity": "sha512-bFhn0MQ8qefLyJ3K7PpHiPUTuTVPWw6RXfaMeV6xgJLXtBbszyboz1bvGTVv4R0YpQm2DqlXXn0fFHhxUHVE5w==",
"requires": {
"has-flag": "^4.0.0",
"supports-color": "^7.0.0"
},
"dependencies": {
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="
},
"supports-color": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz",
"integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==",
"requires": {
"has-flag": "^4.0.0"
}
}
}
},
"terminal-link": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/terminal-link/-/terminal-link-2.0.0.tgz",
"integrity": "sha512-rdBAY35jUvVapqCuhehjenLbYY73cVgRQ6podD6u9EDBomBBHjCOtmq2InPgPpTysOIOsQ5PdBzwSC/sKjv6ew==",
"requires": {
"ansi-escapes": "^4.2.1",
"supports-hyperlinks": "^2.0.0"
}
},
"tough-cookie": {
"version": "2.4.3",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz",
@ -462,6 +503,11 @@
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
"integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q="
},
"type-fest": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
"integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA=="
},
"uri-js": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz",

View File

@ -14,6 +14,7 @@
"fluent-ffmpeg": "^2.1.2",
"node-id3": "^0.1.11",
"request": "^2.88.0",
"terminal-link": "^2.0.0",
"ytdl-core": "^0.29.7"
}
}