Compare commits

..

No commits in common. "9e9f280f15482c82c2bcc1b167342afdde66d97c" and "8a5b21f4d7890c2d43ba7bf858f4b84b001f8008" have entirely different histories.

2 changed files with 35 additions and 55 deletions

View File

@ -17,18 +17,11 @@ Inspired by [DatMusic](https://github.com/alashow/datmusic-api), [MyFreeMp3](htt
*TODO*
* ~~Dowload all songs in a folder~~
* ~~Add Spotify metadatas in the m3u8 file~~
* Add Spotify metadatas in the m3u8 file
* Add proper command line options
* Add CLI progress info
* ~~Having placeholder for files that can't be downloaded, maybe as
comment in the m3u8 file~~
* Having a option to retry/resync download mp3 based on the m3u8 file
* Check the mime type of receive files because some are html files
instead of mp3 WTF !!
* Rewrite this as a plugin for Ampache
* Rewrite this as a plugin for Funkwhale
* Add MusicBrain ID3Tag on file
* Have a fallback for downloading song with youtube-dl :-P
## Goals

View File

@ -2,7 +2,6 @@
const Cheerio = require('cheerio');
const { promises: fs, createWriteStream } = require('fs');
const debug = require('debug')('vk-music-dl');
const verboseDebug = require('debug')('vk-music-dl:debug');
const vm = require('vm');
const Promise = require('bluebird');
const _ = require('lodash');
@ -30,7 +29,7 @@ async function getSpotifyPlaylist(playListUrl){
$('script#config').each(function(i, elem){
config = JSON.parse($(this).html());
});
verboseDebug('config=%O', config);
debug('config=%O', config);
const playlistSpotifyId = /\/playlist\/([^\/]+)\/?$/.exec(playListUrl)[1];
@ -41,7 +40,6 @@ async function getSpotifyPlaylist(playListUrl){
}
}).then(res => res.json());
verboseDebug('playlist=%O', playlist);
return playlist;
}
exports.getSpotifyPlaylist = getSpotifyPlaylist;
@ -92,7 +90,7 @@ async function searchOnMyFreeMp3(query){
})
.then(res => res.text())
.then(page => {
verboseDebug('page=%O', page);
debug('page=%O', page);
return page;
})
.then(jsonp => vm.runInNewContext(jsonp, { jQuery666: (payload) => payload.response }))
@ -108,61 +106,50 @@ function matchScore(spotifyMetas, vkmusicMetas){
const matchArtistScore = 1 - (leven(originalArtistNames, _.get(vkmusicMetas, 'artist', '').toLowerCase()) / Math.max(originalArtistNames.length, _.get(vkmusicMetas, 'artist', '').length));
const matchTitleScore = 1 - (leven(originalTitle, _.get(vkmusicMetas, 'title', '').toLowerCase()) / Math.max(originalTitle.length, _.get(vkmusicMetas, 'title', '').length));
const matchDurationScore = 1 - (Math.abs(originalDuration - _.get(vkmusicMetas, 'duration', 0)) / originalDuration); // TODO this can return more than 1 or less than 0
verboseDebug('matchArtistScore=%f matchTitleScore=%f matchDurationScore=%f', matchArtistScore, matchTitleScore, matchDurationScore);
debug('matchArtistScore=%f matchTitleScore=%f matchDurationScore=%f', matchArtistScore, matchTitleScore, matchDurationScore);
return matchArtistScore + matchTitleScore + matchDurationScore;
}
exports.matchScore = matchScore;
function generateM3U8Playlist(playlist){
function generateM3U8Playlist(filesWithMetas){
const m3uWriter = m3u.extendedWriter();
m3uWriter.comment(`VK-MUSIC-DL SPOTIFYURL ${playlist.external_urls.spotify}`);
playlist.tracks.items.forEach(item => {
if(item.destPath){
m3uWriter.file(item.destPath, item.vkBestMatch.duration, `${item.vkBestMatch.artist} - ${item.vkBestMatch.title}`);
} else {
const artistNames = _.map(item.track.artists, 'name').join(', ');
m3uWriter.comment(`VK-MUSIC-DL Missing song ${item.track.name} - ${artistNames}`);
}
});
filesWithMetas.forEach(fileWithMetas => m3uWriter.file(fileWithMetas.path, fileWithMetas.duration, `${fileWithMetas.artist} - ${fileWithMetas.title}`));
return m3uWriter.toString();
}
exports.generateM3U8Playlist = generateM3U8Playlist;
exports.main = async function main(playlistUrl){
const playlist = await getSpotifyPlaylist(playlistUrl);
const playlistPath = playlist.name.replace(/\/|\\/g, '');
await Promise.resolve(fs.mkdir(playlistPath)).catch({code: 'EEXIST'}, () => {});
playlist.tracks.items = await Promise.map(playlist.tracks.items, async (item) => {
const artistNames = _.map(item.track.artists, 'name').join(', ');
debug('%s - %s', item.track.name, artistNames);
const myFreeMp3Results = await searchOnMyFreeMp3(`${artistNames} ${item.track.name}`);
verboseDebug('myFreeMp3Results=%O', myFreeMp3Results);
const bestMatch = _.chain(myFreeMp3Results).map((result) => {
result.score = matchScore(item.track, result);
return result;
}).filter(result => result.score && result.url).sortBy('score').last().value();
if(bestMatch){
verboseDebug('bestMatch=%O', bestMatch);
item.vkBestMatch = bestMatch;
const destPath = `${bestMatch.artist} - ${bestMatch.title}.mp3`.replace(/\/|\\/g, '');
await Promise.resolve(fs.mkdir(playlist.name)).catch({code: 'EEXIST'}, () => {});
const vkPlaylist = await Promise.map(playlist.tracks.items, async ({track}) => {
const artistNames = _.map(track.artists, 'name').join(', ');
debug('%s - %s', track.name, artistNames);
const items = await searchOnMyFreeMp3(`${artistNames} ${track.name}`);
debug('items=%O', items);
const bestMatch = _.chain(items).map((item) => {
item.score = matchScore(track, item);
return item;
}).filter(item => item.score && item.url).sortBy('score').last().value();
if(!bestMatch){
console.log(`You are on your own for ${track.name} - ${artistNames}`);
return;
}
bestMatch.path = `${bestMatch.artist} - ${bestMatch.title}.mp3`;
debug('bestMatch=%O', bestMatch);
try {
await fs.access(path.join(playlistPath, destPath)).catch(async () => { // TODO find a proper way to not re-download, lower/uppercase problems
await fs.access(path.join(playlist.name, bestMatch.path)).catch(async () => { // TODO find a proper way to not re-download, lower/uppercase problems
await fetch(bestMatch.url).then(async res => {
// TODO check the mime type here
await promisepipe(res.body, createWriteStream(path.join(playlistPath, destPath)));
await promisepipe(res.body, createWriteStream(path.join(playlist.name, bestMatch.path)));
});
debug('Done downloading');
});
item.destPath = destPath;
} catch (err) {
console.error('Download failed for ', bestMatch);
console.error(err);
return null;
}
} else {
console.log(`You are on your own for ${item.track.name} - ${artistNames}`);
}
return item;
return bestMatch;
}, {concurrency: 1});
await fs.writeFile(path.join(playlistPath, `playlist.m3u8`), generateM3U8Playlist(playlist));
await fs.writeFile(path.join(playlist.name, `playlist.m3u8`), generateM3U8Playlist(_.compact(vkPlaylist)));
}