8000 Merge pull request #7 from rvpanoz/develop · rvpanoz/sndc@c00d507 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Merge pull request #7 from rvpanoz/develop
Browse files Browse the repository at this point in the history
create downloads folder if not exists, remove initial soundcloud url
  • Loading branch information
rvpanoz authored Aug 20, 2018
2 parents 4e2d6a3 + 90d5816 commit c00d507
Show file tree
Hide file tree
Showing 5 changed files with 899 additions and 1,359 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config = {
_wait: 500,
client_id: "KFSHpN5xEaAvIZZCrsrDjuFHOcArM91q",
hostApi: "api.soundcloud.com",
resolveUri: "http://api.soundcloud.com/resolve",
resolveUri: "http://api.soundcloud.com/resolve"
};

export default config;
20 changes: 9 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import config from "../config";
import request from "request";
import requestPromise from "request-promise";
import Progress from "cli-progress";

let length = 0;
import {statusCodes} from './statusCodes'

const __continueRequest = function(length) {
let downloaded = 0,
Expand All @@ -21,13 +20,6 @@ const __continueRequest = function(length) {
return this;
};

const statusCodes = [
{ code: 401, message: "Unauthorized request." },
{ code: 404, message: "Track not found" },
{ code: 500, message: "Server Error" }
];


const handleStatusCode = code => {
const result = statusCodes.filter(statusCode => statusCode.code === code)[0];

Expand Down Expand Up @@ -66,7 +58,13 @@ export const writeStream = (ctx, title, reply) => {
title
.replace(REGX_CleanSpecial, "")
.trim();
const filePath = path.join(`${cwd}/downloads/${fileName}.mp3`);
const downloadsFolder = path.join(`${cwd}/downloads/`);
const downloadsFolderExists = fs.existsSync(path.resolve(downloadsFolder))

if(!downloadsFolderExists) {
fs.mkdirSync(downloadsFolder)
}

const fileSize = parseInt(headers["content-length"], 10);
const length = fileSize.toFixed(2);
const result = handleStatusCode(+statusCode);
Expand All @@ -92,7 +90,7 @@ export const writeStream = (ctx, title, reply) => {

result
.call(ctx, length)
.pipe(fs.createWriteStream(filePath, { flags: "w+" }));
.pipe(fs.createWriteStream(path.join(downloadsFolder, `/${fileName}.mp3`), { flags: "w+" }));
} catch (err) {
throw new Error(err);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/statusCodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const statusCodes = [
{ code: 401, message: "Unauthorized request." },
{ code: 404, message: "Track not found" },
{ code: 500, message: "Server Error" }
];
Loading

0 comments on commit c00d507

Please sign in to comment.
0