feat: add-test and some fixes #1

Merged
hugopoi merged 4 commits from feat/add-test into master 2020-09-27 11:41:19 +02:00
Showing only changes of commit 4f7d770ed3 - Show all commits

View File

@ -10,13 +10,15 @@ const { URL, URLSearchParams } = require('url');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const m3u = require('m3u'); const m3u = require('m3u');
const promisepipe = require('promisepipe'); const promisepipe = require('promisepipe');
const path = require('path');
require('dotenv').config(); require('dotenv').config();
const userAgentString = 'Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0';
async function getSpotifyPlaylist(playListUrl){ async function getSpotifyPlaylist(playListUrl){
const spotifyPlaylistPageContent = await fetch(playListUrl, { const spotifyPlaylistPageContent = await fetch(playListUrl, {
headers: { 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()) .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`, { const playlist = await fetch(`https://api.spotify.com/v1/playlists/${playlistSpotifyId}?type=track%2Cepisode`, {
headers: { 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}` Authorization: `Bearer ${config.accessToken}`
} }
}).then(res => res.json()); }).then(res => res.json());
@ -72,6 +74,15 @@ async function searchOnMyFreeMp3(query){
return await fetch(url, { return await fetch(url, {
method: 'POST', 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({ body: new URLSearchParams({
q: query, q: query,
page: 0, page: 0,
@ -109,6 +120,7 @@ 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);
await Promise.resolve(fs.mkdir(playlist.name)).catch({code: 'EEXIST'}, () => {});
const vkPlaylist = await Promise.map(playlist.tracks.items, async ({track}) => { const vkPlaylist = await Promise.map(playlist.tracks.items, async ({track}) => {
const artistNames = _.map(track.artists, 'name').join(', '); const artistNames = _.map(track.artists, 'name').join(', ');
debug('%s - %s', track.name, artistNames); debug('%s - %s', track.name, artistNames);
@ -125,9 +137,9 @@ exports.main = async function main(playlistUrl){
bestMatch.path = `${bestMatch.artist} - ${bestMatch.title}.mp3`; bestMatch.path = `${bestMatch.artist} - ${bestMatch.title}.mp3`;
debug('bestMatch=%O', bestMatch); debug('bestMatch=%O', bestMatch);
try { 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 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'); debug('Done downloading');
}); });
@ -139,5 +151,5 @@ exports.main = async function main(playlistUrl){
return bestMatch; return bestMatch;
}, {concurrency: 1}); }, {concurrency: 1});
await fs.writeFile(`playlist.m3u8`, generateM3U8Playlist(_.compact(vkPlaylist))); await fs.writeFile(path.join(playlist.name, `playlist.m3u8`), generateM3U8Playlist(_.compact(vkPlaylist)));
} }