Skip to content

Commit

Permalink
Fix media download by updating dependencies (#32)
Browse files Browse the repository at this point in the history
highland: 2.11.1 => 2.13.0
left-pad: 1.1.3 => 1.3.0
spotify-web-api: 2.3.5 => 5.0.2
youtube-api: 2.0.2 => 3.0.1
ytdl-core: 3.1.2 => 4.11.0
  • Loading branch information
danguilherme authored Mar 12, 2022
1 parent 993bceb commit 3dc7747
Show file tree
Hide file tree
Showing 8 changed files with 445 additions and 1,070 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Node.js Package

on:
release:
types: [created]
types: [published]

jobs:
build:
Expand All @@ -12,7 +12,7 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: yarn install
# - run: npm test

publish-npm:
Expand All @@ -24,7 +24,7 @@ jobs:
with:
node-version: 12
registry-url: 'https://registry.npmjs.org/'
- run: npm ci
- run: npm publish
- run: yarn install
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
media/
config.json
config.json
.DS_Store
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function init() {
logger
});
} else {
cmdPromise = cmd_playlist(args.username, args.playlists, {
cmdPromise = cmd_playlist(args.playlists, {
spotifyClientId: config.spotifyClientId,
spotifyClientSecret: config.spotifyClientSecret,
youtubeKey: config.youtubeKey,
Expand Down Expand Up @@ -156,7 +156,7 @@ function cmd_user(username,
});
}

function cmd_playlist(username, playlists,
function cmd_playlist(playlists,
{ spotifyClientId, spotifyClientSecret, youtubeKey,
format, quality, output, flat, logger } = {}) {

Expand All @@ -166,7 +166,7 @@ function cmd_playlist(username, playlists,
.then(() => youtube.login(youtubeKey))
.then(() => {
highland(playlists)
.flatMap(playlist => highland(spotify.getPlaylist(username, playlist)))
.flatMap(playlist => highland(spotify.getPlaylist(playlist, { logger })))
.flatMap(playlist => downloadPlaylist(playlist, { format, quality, path: output, createSubFolder: !flat, logger }))
.errors(err => handleDownloadError(err, logger))
.done(resolve);
Expand All @@ -183,7 +183,7 @@ function cmd_track(tracks,
.then(() => youtube.login(youtubeKey))
.then(() => {
highland(tracks)
.flatMap(track => highland(spotify.getTrack(track)))
.flatMap(track => highland(spotify.getTrack(track, { logger })))
.flatMap(track => highland(downloadTrack(track, { format, quality, path: output, logger })))
.errors(err => handleDownloadError(err, logger))
.done(resolve);
Expand Down Expand Up @@ -236,7 +236,7 @@ function downloadPlaylist(playlist,
let metadata = getMetadata(metadataPath);

return spotify
.getAllPlaylistTracks(playlist.owner.id, playlist.id, { logger })
.getAllPlaylistTracks(playlist.id, { logger })
.sequence()
.map(playlistTrack => playlistTrack.track)
.filter(track => !isTrackDownloaded(track.id, metadata))
Expand Down
7 changes: 6 additions & 1 deletion log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { INFO_COLUMN_WIDTH } = require('./constants');

const debugPrimaryColor = chalk.reset.cyan;
const debugSecondaryColor = chalk.reset.cyan.bold;
const errorPrimaryColor = chalk.reset.red;
const warnPrimaryColor = chalk.reset.yellow;
const promptPrimaryColor = chalk.reset.magenta;
const promptSecondaryColor = chalk.reset.magenta.bold;
Expand All @@ -30,6 +31,10 @@ function warn(logger, ...args) {
log(logger, 'warn', warnPrimaryColor(leftPad('[WARN]', INFO_COLUMN_WIDTH)), ...args);
}

function error(logger, ...args) {
log(logger, 'error', errorPrimaryColor(leftPad('[ERROR]', INFO_COLUMN_WIDTH)), ...args);
}

function prompt(text) {
const inputInterface = readline.createInterface({
input: process.stdin,
Expand All @@ -45,4 +50,4 @@ function prompt(text) {
});
}

module.exports = { info, debug, warn, prompt };
module.exports = { info, debug, error, warn, prompt };
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"scripts": {
"prestart": "npm install",
"run-bin": "node index.js",
"start": "node index.js init",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -23,15 +24,15 @@
"dependencies": {
"caporal": "^0.7.0",
"chalk": "^1.1.3",
"highland": "^2.11.1",
"highland": "^2.13.0",
"jsonfile": "^2.4.0",
"left-pad": "^1.1.3",
"left-pad": "^1.3.0",
"mkdirp": "^0.5.1",
"spotify-web-api-node": "^2.3.5",
"youtube-api": "^2.0.2",
"ytdl-core": "3.1.2"
"spotify-web-api-node": "5.0.2",
"youtube-api": "3.0.1",
"ytdl-core": "4.11.0"
},
"devDependencies": {
"eslint": "^4.7.2"
"eslint": "^4.19.1"
}
}
27 changes: 20 additions & 7 deletions spotify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const SpotifyWebApi = require('spotify-web-api-node');
const highland = require('highland');

const { debug } = require('./log');
const { debug, error } = require('./log');

let api = null;

Expand All @@ -16,26 +16,39 @@ function login(clientId, clientSecret, { logger } = {}) {

api = new SpotifyWebApi({ clientId, clientSecret });

debug(logger, 'Granting credentials...');
// Retrieve an access token.
api.clientCredentialsGrant()
.then(function (data) {
// Save the access token so that it's used in future calls
api.setAccessToken(data.body['access_token']);

debug(logger, 'Spotify login successful');
resolve(api);
}, function (err) {
console.error('Something went wrong when retrieving an access token', err);
error(logger, `Something went wrong when retrieving an access token: ${err.message}`);
debug(logger, `Stack trace:\n${err.stack}`);
reject(err);
});
})
}

function getTrack(trackId, { logger } = {}) {
return api.getTrack(trackId).then(r => r.body);
debug(logger, `Fetch track: ${trackId}`);
return api.getTrack(trackId).then(trackResponse => {
const track = trackResponse.body;
debug(logger, `Success: ${track.name}`);
return track;
});
}

function getPlaylist(username, playlistId, { logger } = {}) {
return api.getPlaylist(username, playlistId).then(r => r.body);
function getPlaylist(playlistId, { logger } = {}) {
debug(logger, `Fetch playlist: ${playlistId}`);
return api.getPlaylist(playlistId).then(playlistResponse => {
const playlist = playlistResponse.body;
debug(logger, `Success: ${playlist.name}`);
return playlist;
});
}

function getAllUserPlaylists(username, { logger } = {}) {
Expand All @@ -45,10 +58,10 @@ function getAllUserPlaylists(username, { logger } = {}) {
}, { logger });
}

function getAllPlaylistTracks(username, playlistId, { logger } = {}) {
function getAllPlaylistTracks(playlistId, { logger } = {}) {
debug(logger, `Fetching playlist tracks (${playlistId})`);
return createPaginationStream(function getPlaylistTracks(options) {
return api.getPlaylistTracks(username, playlistId, options);
return api.getPlaylistTracks(playlistId, options);
}, { logger });
}

Expand Down
Loading

0 comments on commit 3dc7747

Please sign in to comment.