From e5fcabdd37f01c3f506dff76cdf132ec3bca13b9 Mon Sep 17 00:00:00 2001 From: Job Vonk Date: Tue, 26 Nov 2019 07:30:26 +0100 Subject: [PATCH] More work done --- main.js | 10 +++++----- tags.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 10 ---------- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 tags.js delete mode 100755 test.js diff --git a/main.js b/main.js index e0fb738..ae36a83 100755 --- a/main.js +++ b/main.js @@ -63,13 +63,13 @@ ytdl.getInfo(process.argv[2], (err, info) => { //filter: 'audioonly', }); - const path = `${__dirname}/music/${info.title}.mp3`; + const filePath = path.join(__dirname, `/music/${info.title}.mp3`); log(chalk.yellow("🏁 Starting download...")); ffmpeg(stream) .audioBitrate(128) - .save(path) + .save(filePath) .on("progress", p => { const progress = convertTime(p.timemark); log( @@ -118,10 +118,10 @@ ytdl.getInfo(process.argv[2], (err, info) => { log(chalk.yellow("✍🏻 Writing tags to mp3 file...")); - id3.update(tags, path, () => { - if (fs.existsSync(path) && process.platform === "darwin" && !hasflag("no-itunes")) { + id3.update(tags, filePath, () => { + if (fs.existsSync(filePath) && process.platform === "darwin" && !hasflag("no-itunes")) { log(chalk.yellow("🎵 Adding song to Apple Music library...")); - cp.execSync(`cp "${path}" "/Users/job/Music/Music/Media/Automatically\ Add\ to\ Music.localized"`); + cp.execSync(`cp "${filePath}" "/Users/job/Music/Music/Media/Automatically\ Add\ to\ Music.localized"`); } log(chalk.green(`✅ Successfully downloaded ${info.title}.mp3!\n`)); diff --git a/tags.js b/tags.js new file mode 100644 index 0000000..290a547 --- /dev/null +++ b/tags.js @@ -0,0 +1,55 @@ +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(); + }) + }) + }) +}) \ No newline at end of file diff --git a/test.js b/test.js deleted file mode 100755 index 2aa269f..0000000 --- a/test.js +++ /dev/null @@ -1,10 +0,0 @@ -const ytdl = require("ytdl-core"); - -ytdl.getInfo(process.argv[2], (err, info) => { - //let stream = ytdl(process.argv[2], { - // quality: "highestaudio" - //filter: 'audioonly', - //}); - - console.log(info); -}); \ No newline at end of file