feat: add-test and some fixes #1
22
lib/index.js
22
lib/index.js
|
@ -10,13 +10,15 @@ const { URL, URLSearchParams } = require('url');
|
|||
const fetch = require('node-fetch');
|
||||
const m3u = require('m3u');
|
||||
const promisepipe = require('promisepipe');
|
||||
const path = require('path');
|
||||
require('dotenv').config();
|
||||
|
||||
const userAgentString = 'Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0';
|
||||
|
||||
async function getSpotifyPlaylist(playListUrl){
|
||||
const spotifyPlaylistPageContent = await fetch(playListUrl, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0',
|
||||
'User-Agent': userAgentString,
|
||||
}
|
||||
})
|
||||
.then(res => res.text())
|
||||
|
@ -33,7 +35,7 @@ async function getSpotifyPlaylist(playListUrl){
|
|||
|
||||
const playlist = await fetch(`https://api.spotify.com/v1/playlists/${playlistSpotifyId}?type=track%2Cepisode`, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0',
|
||||
'User-Agent': userAgentString,
|
||||
Authorization: `Bearer ${config.accessToken}`
|
||||
}
|
||||
}).then(res => res.json());
|
||||
|
@ -72,6 +74,15 @@ async function searchOnMyFreeMp3(query){
|
|||
|
||||
return await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'User-Agent': userAgentString,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
Accept: 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
|
||||
'Accept-Language': 'fr-FR,fr;q=0.5',
|
||||
Referer: 'https://myfreemp3.vip/',
|
||||
Origin: 'https://myfreemp3.vip',
|
||||
//Cookie: '__cfduid=d1821f08faee82ed9d4bf93b1b5fb9d3e1601196911; musicLang=en'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
q: query,
|
||||
page: 0,
|
||||
|
@ -109,6 +120,7 @@ exports.generateM3U8Playlist = generateM3U8Playlist;
|
|||
|
||||
exports.main = async function main(playlistUrl){
|
||||
const playlist = await getSpotifyPlaylist(playlistUrl);
|
||||
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);
|
||||
|
@ -125,9 +137,9 @@ exports.main = async function main(playlistUrl){
|
|||
bestMatch.path = `${bestMatch.artist} - ${bestMatch.title}.mp3`;
|
||||
debug('bestMatch=%O', bestMatch);
|
||||
try {
|
||||
await fs.access(bestMatch.path).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 promisepipe(res.body, createWriteStream(bestMatch.path));
|
||||
await promisepipe(res.body, createWriteStream(path.join(playlist.name, bestMatch.path)));
|
||||
});
|
||||
debug('Done downloading');
|
||||
});
|
||||
|
@ -139,5 +151,5 @@ exports.main = async function main(playlistUrl){
|
|||
return bestMatch;
|
||||
}, {concurrency: 1});
|
||||
|
||||
await fs.writeFile(`playlist.m3u8`, generateM3U8Playlist(_.compact(vkPlaylist)));
|
||||
await fs.writeFile(path.join(playlist.name, `playlist.m3u8`), generateM3U8Playlist(_.compact(vkPlaylist)));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user