Compare commits
No commits in common. "9e9f280f15482c82c2bcc1b167342afdde66d97c" and "8a5b21f4d7890c2d43ba7bf858f4b84b001f8008" have entirely different histories.
9e9f280f15
...
8a5b21f4d7
|
@ -17,18 +17,11 @@ Inspired by [DatMusic](https://github.com/alashow/datmusic-api), [MyFreeMp3](htt
|
||||||
*TODO*
|
*TODO*
|
||||||
|
|
||||||
* ~~Dowload all songs in a folder~~
|
* ~~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 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 Ampache
|
||||||
* Rewrite this as a plugin for Funkwhale
|
* Rewrite this as a plugin for Funkwhale
|
||||||
* Add MusicBrain ID3Tag on file
|
* Add MusicBrain ID3Tag on file
|
||||||
* Have a fallback for downloading song with youtube-dl :-P
|
|
||||||
|
|
||||||
## Goals
|
## Goals
|
||||||
|
|
||||||
|
|
65
lib/index.js
65
lib/index.js
|
@ -2,7 +2,6 @@
|
||||||
const Cheerio = require('cheerio');
|
const Cheerio = require('cheerio');
|
||||||
const { promises: fs, createWriteStream } = require('fs');
|
const { promises: fs, createWriteStream } = require('fs');
|
||||||
const debug = require('debug')('vk-music-dl');
|
const debug = require('debug')('vk-music-dl');
|
||||||
const verboseDebug = require('debug')('vk-music-dl:debug');
|
|
||||||
const vm = require('vm');
|
const vm = require('vm');
|
||||||
const Promise = require('bluebird');
|
const Promise = require('bluebird');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
@ -30,7 +29,7 @@ async function getSpotifyPlaylist(playListUrl){
|
||||||
$('script#config').each(function(i, elem){
|
$('script#config').each(function(i, elem){
|
||||||
config = JSON.parse($(this).html());
|
config = JSON.parse($(this).html());
|
||||||
});
|
});
|
||||||
verboseDebug('config=%O', config);
|
debug('config=%O', config);
|
||||||
|
|
||||||
const playlistSpotifyId = /\/playlist\/([^\/]+)\/?$/.exec(playListUrl)[1];
|
const playlistSpotifyId = /\/playlist\/([^\/]+)\/?$/.exec(playListUrl)[1];
|
||||||
|
|
||||||
|
@ -41,7 +40,6 @@ async function getSpotifyPlaylist(playListUrl){
|
||||||
}
|
}
|
||||||
}).then(res => res.json());
|
}).then(res => res.json());
|
||||||
|
|
||||||
verboseDebug('playlist=%O', playlist);
|
|
||||||
return playlist;
|
return playlist;
|
||||||
}
|
}
|
||||||
exports.getSpotifyPlaylist = getSpotifyPlaylist;
|
exports.getSpotifyPlaylist = getSpotifyPlaylist;
|
||||||
|
@ -92,7 +90,7 @@ async function searchOnMyFreeMp3(query){
|
||||||
})
|
})
|
||||||
.then(res => res.text())
|
.then(res => res.text())
|
||||||
.then(page => {
|
.then(page => {
|
||||||
verboseDebug('page=%O', page);
|
debug('page=%O', page);
|
||||||
return page;
|
return page;
|
||||||
})
|
})
|
||||||
.then(jsonp => vm.runInNewContext(jsonp, { jQuery666: (payload) => payload.response }))
|
.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 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 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
|
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;
|
return matchArtistScore + matchTitleScore + matchDurationScore;
|
||||||
}
|
}
|
||||||
exports.matchScore = matchScore;
|
exports.matchScore = matchScore;
|
||||||
|
|
||||||
function generateM3U8Playlist(playlist){
|
function generateM3U8Playlist(filesWithMetas){
|
||||||
const m3uWriter = m3u.extendedWriter();
|
const m3uWriter = m3u.extendedWriter();
|
||||||
m3uWriter.comment(`VK-MUSIC-DL SPOTIFYURL ${playlist.external_urls.spotify}`);
|
filesWithMetas.forEach(fileWithMetas => m3uWriter.file(fileWithMetas.path, fileWithMetas.duration, `${fileWithMetas.artist} - ${fileWithMetas.title}`));
|
||||||
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}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return m3uWriter.toString();
|
return m3uWriter.toString();
|
||||||
}
|
}
|
||||||
exports.generateM3U8Playlist = generateM3U8Playlist;
|
exports.generateM3U8Playlist = generateM3U8Playlist;
|
||||||
|
|
||||||
exports.main = async function main(playlistUrl){
|
exports.main = async function main(playlistUrl){
|
||||||
const playlist = await getSpotifyPlaylist(playlistUrl);
|
const playlist = await getSpotifyPlaylist(playlistUrl);
|
||||||
const playlistPath = playlist.name.replace(/\/|\\/g, '');
|
await Promise.resolve(fs.mkdir(playlist.name)).catch({code: 'EEXIST'}, () => {});
|
||||||
await Promise.resolve(fs.mkdir(playlistPath)).catch({code: 'EEXIST'}, () => {});
|
const vkPlaylist = await Promise.map(playlist.tracks.items, async ({track}) => {
|
||||||
playlist.tracks.items = await Promise.map(playlist.tracks.items, async (item) => {
|
const artistNames = _.map(track.artists, 'name').join(', ');
|
||||||
const artistNames = _.map(item.track.artists, 'name').join(', ');
|
debug('%s - %s', track.name, artistNames);
|
||||||
debug('%s - %s', item.track.name, artistNames);
|
const items = await searchOnMyFreeMp3(`${artistNames} ${track.name}`);
|
||||||
const myFreeMp3Results = await searchOnMyFreeMp3(`${artistNames} ${item.track.name}`);
|
debug('items=%O', items);
|
||||||
verboseDebug('myFreeMp3Results=%O', myFreeMp3Results);
|
const bestMatch = _.chain(items).map((item) => {
|
||||||
const bestMatch = _.chain(myFreeMp3Results).map((result) => {
|
item.score = matchScore(track, item);
|
||||||
result.score = matchScore(item.track, result);
|
return item;
|
||||||
return result;
|
}).filter(item => item.score && item.url).sortBy('score').last().value();
|
||||||
}).filter(result => result.score && result.url).sortBy('score').last().value();
|
if(!bestMatch){
|
||||||
if(bestMatch){
|
console.log(`You are on your own for ${track.name} - ${artistNames}`);
|
||||||
verboseDebug('bestMatch=%O', bestMatch);
|
return;
|
||||||
item.vkBestMatch = bestMatch;
|
}
|
||||||
const destPath = `${bestMatch.artist} - ${bestMatch.title}.mp3`.replace(/\/|\\/g, '');
|
bestMatch.path = `${bestMatch.artist} - ${bestMatch.title}.mp3`;
|
||||||
|
debug('bestMatch=%O', bestMatch);
|
||||||
try {
|
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 => {
|
await fetch(bestMatch.url).then(async res => {
|
||||||
// TODO check the mime type here
|
await promisepipe(res.body, createWriteStream(path.join(playlist.name, bestMatch.path)));
|
||||||
await promisepipe(res.body, createWriteStream(path.join(playlistPath, destPath)));
|
|
||||||
});
|
});
|
||||||
debug('Done downloading');
|
debug('Done downloading');
|
||||||
});
|
});
|
||||||
item.destPath = destPath;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Download failed for ', bestMatch);
|
console.error('Download failed for ', bestMatch);
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
return bestMatch;
|
||||||
console.log(`You are on your own for ${item.track.name} - ${artistNames}`);
|
|
||||||
}
|
|
||||||
return item;
|
|
||||||
}, {concurrency: 1});
|
}, {concurrency: 1});
|
||||||
|
|
||||||
await fs.writeFile(path.join(playlistPath, `playlist.m3u8`), generateM3U8Playlist(playlist));
|
await fs.writeFile(path.join(playlist.name, `playlist.m3u8`), generateM3U8Playlist(_.compact(vkPlaylist)));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user