Skip to content

Commit

Permalink
Merge pull request #139 from phyzical/hotfix/fix-episode-type
Browse files Browse the repository at this point in the history
flag for manual logic option
  • Loading branch information
SwapnilSoni1999 authored Oct 16, 2021
2 parents b651262 + 9689e17 commit 5aafb69
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 32 deletions.
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,30 @@ $ spotifydl https://open.spotify.com/track/xyz
```

#### Options
| Flag | Long Flag | Usage |
| ----- | ----------------- | --------------------------------------------------------------------- |
| --o | --output | takes valid output path argument |
| --es | --extra-search | takes extra search string/term to be used for youtube search |
| --oo | --output-only | enforces all downloaded songs in the output dir |
| --st | --saved-tracks | download spotify saved tracks |
| --ss | --saved-songs | download spotify saved shows |
| --sp | --saved-playlists | download spotify saved playlists |
| --sa | --saved-albums | download spotify saved albums |
| --u | --username | spotify username (only needed in non tty) |
| --p | --password | spotify password (only needed in non tty) |
| --cf | --cache-file | takes valid output file name path argument |
| --dr | --download-report | output a download report of what files failed |
| --cof | --cookie-file | takes valid file name path argument to a txt file for youtube cookies |
| --v | --version | returns current version |
| --h | --help | outputs help text |
| Flag | Long Flag | Usage |
| ----- | ----------------- | ----------------------------------------------------------------------- |
| --o | --output | takes valid output path argument |
| --es | --extra-search | takes extra search string/term to be used for youtube search |
| --oo | --output-only | enforces all downloaded songs in the output dir |
| --st | --saved-tracks | download spotify saved tracks |
| --ss | --saved-songs | download spotify saved shows |
| --sp | --saved-playlists | download spotify saved playlists |
| --sa | --saved-albums | download spotify saved albums |
| --l | --login | Requests a login in an external window (non tty should use --u and --p) |
| --u | --username | spotify username for headless long |
| --p | --password | spotify password |
| --cf | --cache-file | takes valid output file name path argument |
| --dr | --download-report | output a download report of what files failed |
| --cof | --cookie-file | takes valid file name path argument to a txt file for youtube cookies |
| --v | --version | returns current version |
| --h | --help | outputs help text |
<hr>

## Notes

if you receive 'Got a spotify api error WebapiRegularError: An error occurred while communicating with Spotify's Web API
Details: non existing id' you may need to provide auth either use `--l` for manual login prompt or `--u username --p password` for headless login

If you receive a 429 error please provide a cookies file given the `--cof` flag, to generate a cookies file please refer to [Chrome](https://chrome.google.com/webstore/detail/njabckikapfpffapmjgojcnbfjonfjfg) or [Firefox](https://github.com/rotemdan/ExportCookies)

## Docker
Expand Down
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
downloadReport: true,
output: process.cwd(),
extraSearch: '',
login: false,
password: '',
username: '',
savedAlbums: false,
Expand All @@ -16,4 +17,9 @@ export default {
savedShows: false,
outputOnly: false,
},
spotifyApi: {
clientId: 'acc6302297e040aeb6e4ac1fbdfd62c3',
clientSecret: '0e8439a1280a43aba9a5bc0a16f3f009',
},
isTTY: process.stdout.isTTY,
};
20 changes: 17 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ import open from 'open';
import express from 'express';
import puppeteer from 'puppeteer';
import { cliInputs } from './setup.js';
import Config from '../config.js';
import Constants from '../util/constants.js';
import { logInfo, logFailure } from '../util/log-helper.js';

const {
spotifyApi: {
clientId,
clientSecret,
},
} = Config;

const {
AUTH: {
SCOPES: {
Expand All @@ -21,8 +29,8 @@ const {
} = Constants;

const spotifyApi = new SpotifyWebApi({
clientId: 'acc6302297e040aeb6e4ac1fbdfd62c3',
clientSecret: '0e8439a1280a43aba9a5bc0a16f3f009',
clientId,
clientSecret,
redirectUri: `http://${HOST}:${PORT}${CALLBACK_URI}`,
});

Expand Down Expand Up @@ -62,6 +70,7 @@ const checkCredentials = async () => {
inputs,
username,
password,
login,
} = cliInputs();

const requiresLogin = inputs.find(input =>
Expand All @@ -71,7 +80,7 @@ const checkCredentials = async () => {
input.type == INPUT_TYPES.EPISODE.SAVED_SHOWS,
);

const requestingLogin = username && password;
const requestingLogin = (username && password) || login;

if (requiresLogin || requestingLogin) {
await requestAuthorizedTokens();
Expand Down Expand Up @@ -106,6 +115,10 @@ const requestAuthorizedTokens = async () => {

let browser = null;

logInfo(
'Performing Spotify Auth Please Wait...',
);

if (autoLogin) {
browser = await puppeteer.launch({
headless: true,
Expand Down Expand Up @@ -344,6 +357,7 @@ export async function extractEpisodes(episodeIds) {
chunkedEpisodes[x],
)).body.episodes,
);
episodesResult = episodesResult.filter(episode => episode);
episodes.push(...episodesResult);
}
return episodes.map(episode => parseEpisode(episode));
Expand Down
7 changes: 3 additions & 4 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
logStart, updateSpinner, logInfo, logSuccess,
} from '../util/log-helper.js';

const { youtubeDLConfig, isTTY } = Config;
const sponsorBlock = new SponsorBlock(1234);
const {
SPONSOR_BLOCK: {
Expand Down Expand Up @@ -75,15 +76,13 @@ const sponsorComplexFilter = async link => {
const progressFunction = (_, downloaded, total) => {
const downloadedMb = (downloaded / 1024 / 1024).toFixed(2);
const toBeDownloadedMb = (total / 1024 / 1024).toFixed(2);
const isTTY = process.stdout.isTTY;
const downloadText = `Downloaded ${downloadedMb}/${toBeDownloadedMb} MB`;
if (isTTY || (downloadedMb % 1 == 0 || toBeDownloadedMb == downloadedMb)) {
updateSpinner(downloadText);
}
};

const getYoutubeDLConfig = () => {
const config = Config.youtubeDLConfig;
const { cookieFile } = cliInputs();
if (fs.existsSync(cookieFile)) {
const cookieFileContents = fs
Expand All @@ -97,13 +96,13 @@ const getYoutubeDLConfig = () => {
return cookie;
}, '')
.trim();
config.requestOptions = {
youtubeDLConfig.requestOptions = {
headers: {
Cookie: cookieFileContents,
},
};
}
return config;
return youtubeDLConfig;
};

/**
Expand Down
14 changes: 13 additions & 1 deletion lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function cliInputs() {
if ((flags.savedAlbums ||
flags.savedPlaylists ||
flags.savedShows ||
flags.savedSongs) && !process.stdout.isTTY
flags.savedSongs) && !Config.isTTY
) {
return true;
}
Expand Down Expand Up @@ -145,6 +145,17 @@ export function cliInputs() {
'eg. $ spotifydl <url> --extra-search "lyrics"',
],
},
login: {
alias: 'l',
type: 'boolean',
default: flagsConfig.login,
helpText: [
'--login or -l',
'* will perform a spotify login in an external window for permission',
'* allows spotify premium access restricted things',
'eg. $ spotifydl --l',
],
},
username: {
alias: 'u',
type: 'string',
Expand Down Expand Up @@ -302,6 +313,7 @@ export function cliInputs() {
cookieFile: inputFlags.cookieFile,
downloadReport: inputFlags.downloadReport,
outputOnly: inputFlags.outputOnly,
login: inputFlags.login,
username: inputFlags.username,
password: inputFlags.password,
};
Expand Down
3 changes: 2 additions & 1 deletion util/log-helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ora from 'ora';
import Config from '../config.js';

const spinner = ora('Searching… Please be patient :)\n').start();

Expand All @@ -19,7 +20,7 @@ export function logFailure(message) {
}

export function updateSpinner(message) {
if (process.stdout.isTTY) {
if (Config.isTTY) {
spinner.text = message;
} else {
spinner.info(message);
Expand Down
21 changes: 14 additions & 7 deletions util/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,20 @@ const run = async () => {
}
case INPUT_TYPES.EPISODE.EPISODE: {
const episode = await getEpisode(URL);
lists.push({
items: [
episode,
],
name: `${episode.name} ${episode.album_name}`,
type: input.type,
});
if (episode) {
lists.push({
items: [
episode,
],
name: `${episode.name} ${episode.album_name}`,
type: input.type,
});
} else {
logFailure(
'Failed to find episode, you may need to use auth',
);
}

break;
}
case INPUT_TYPES.EPISODE.SHOW: {
Expand Down

0 comments on commit 5aafb69

Please sign in to comment.