Added more functionality
This commit is contained in:
parent
e2e79af01e
commit
8c991fe062
38
.gitignore
vendored
38
.gitignore
vendored
@ -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/
|
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/
|
music/
|
||||||
|
|
||||||
|
# End of https://www.gitignore.io/api/angular
|
||||||
|
97
main.js
97
main.js
@ -3,31 +3,54 @@ const ytdl = require("ytdl-core");
|
|||||||
const ffmpeg = require("fluent-ffmpeg");
|
const ffmpeg = require("fluent-ffmpeg");
|
||||||
const id3 = require('node-id3');
|
const id3 = require('node-id3');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
var request = require("request").defaults({ encoding: null });
|
const request = require("request").defaults({ encoding: null });
|
||||||
const chalk = require('chalk');
|
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.
|
* 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) => {
|
const log = (something) => {
|
||||||
readline.clearLine(process.stdout, 0);
|
readline.clearLine(process.stdout, 0);
|
||||||
readline.cursorTo(process.stdout, 0);
|
readline.cursorTo(process.stdout, 0);
|
||||||
process.stdout.write(something);
|
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(':');
|
const timeParts = timeString.split(':');
|
||||||
let seconds = Number(timeParts[2]);
|
let seconds = Number(timeParts[2]);
|
||||||
seconds = seconds + (Number(timeParts[1]) * 60) + (Number(timeParts[0]) * 60 * 60);
|
seconds = seconds + (Number(timeParts[1]) * 60) + (Number(timeParts[0]) * 60 * 60);
|
||||||
return seconds;
|
return seconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (process.argv.length !== 5) {
|
if (process.argv.find(v => v === "help" || v === "-h" || v === "--help")) {
|
||||||
console.log(chalk.red('Not getting metadata for this song!'));
|
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..."));
|
log(chalk.yellow("ℹ️ Getting information from YouTube..."));
|
||||||
|
|
||||||
@ -50,13 +73,13 @@ ytdl.getInfo(process.argv[2], (err, info) => {
|
|||||||
chalk.blue(
|
chalk.blue(
|
||||||
`⬇️ ${Math.floor(
|
`⬇️ ${Math.floor(
|
||||||
(progress / Number(info.length_seconds)) * 100
|
(progress / Number(info.length_seconds)) * 100
|
||||||
)}% downloaded.`
|
)}% downloaded. (${p.targetSize}kB)`
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.on('error', err => {
|
.on('error', err => {
|
||||||
log(chalk.red('Error: ' + err.message))
|
console.error(chalk.red('❌ Found an error: ' + err.message));
|
||||||
process.exit();
|
process.exit(1);
|
||||||
})
|
})
|
||||||
.on("end", () => {
|
.on("end", () => {
|
||||||
if (process.argv.length === 5) {
|
if (process.argv.length === 5) {
|
||||||
@ -67,34 +90,42 @@ ytdl.getInfo(process.argv[2], (err, info) => {
|
|||||||
log(chalk.yellow("🎵 Calling iTunes api..."));
|
log(chalk.yellow("🎵 Calling iTunes api..."));
|
||||||
|
|
||||||
axios.get(`https://itunes.apple.com/search?term=${artist} ${song}&entity=song`).then(res => {
|
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: res.data.results[0].trackName,
|
||||||
var tags = {
|
artist: res.data.results[0].artistName,
|
||||||
title: song,
|
performerInfo: album.data.results[0].artistName,
|
||||||
artist: res.data.results[0].artistName,
|
album: res.data.results[0].collectionName,
|
||||||
album: res.data.results[0].collectionName,
|
copyright: album.data.results[0].copyright,
|
||||||
image: {
|
year: new Date(res.data.results[0].releaseDate).getFullYear(),
|
||||||
mime: "jpeg",
|
image: {
|
||||||
type: {
|
mime: "jpeg",
|
||||||
id: 3,
|
type: {
|
||||||
name: "front cover"
|
id: 3,
|
||||||
|
name: "front cover"
|
||||||
|
},
|
||||||
|
imageBuffer: body
|
||||||
},
|
},
|
||||||
imageBuffer: body
|
fileType: "mp3",
|
||||||
},
|
genre: res.data.results[0].primaryGenreName
|
||||||
date: res.data.results[0].releaseDate,
|
};
|
||||||
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 {
|
} else {
|
||||||
|
46
package-lock.json
generated
46
package-lock.json
generated
@ -15,6 +15,14 @@
|
|||||||
"uri-js": "^4.2.2"
|
"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": {
|
"ansi-styles": {
|
||||||
"version": "3.2.1",
|
"version": "3.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
|
||||||
@ -433,6 +441,39 @@
|
|||||||
"has-flag": "^3.0.0"
|
"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": {
|
"tough-cookie": {
|
||||||
"version": "2.4.3",
|
"version": "2.4.3",
|
||||||
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz",
|
"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",
|
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.14.5.tgz",
|
||||||
"integrity": "sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q="
|
"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": {
|
"uri-js": {
|
||||||
"version": "4.2.2",
|
"version": "4.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz",
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
"fluent-ffmpeg": "^2.1.2",
|
"fluent-ffmpeg": "^2.1.2",
|
||||||
"node-id3": "^0.1.11",
|
"node-id3": "^0.1.11",
|
||||||
"request": "^2.88.0",
|
"request": "^2.88.0",
|
||||||
|
"terminal-link": "^2.0.0",
|
||||||
"ytdl-core": "^0.29.7"
|
"ytdl-core": "^0.29.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user