-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
5,150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"presets": ["env"], | ||
"plugins": [ | ||
"transform-object-rest-spread" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,9 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# prettier | ||
prettier.config.js | ||
|
||
# Downloads | ||
downloads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// configuration file | ||
|
||
const config = { | ||
_wait: 500, | ||
client_id: 'KFSHpN5xEaAvIZZCrsrDjuFHOcArM91q', | ||
hostApi: 'api.soundcloud.com', | ||
resolveUri: 'http://api.soundcloud.com/resolve', | ||
testUrl: | ||
'https://soundcloud.com/desert-hearts-records/live-desert-hearts-atish-072?client_id=KFSHpN5xEaAvIZZCrsrDjuFHOcArM91q' | ||
} | ||
|
||
|
||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import config from '../config' | ||
import request from 'request' | ||
import requestPromise from 'request-promise' | ||
|
||
const __continueRequest = function(length) { | ||
let downloaded = 0, progress = 0; | ||
|
||
this.on('data', (chunk) => { | ||
let c = chunk.length | ||
downloaded += c | ||
progress = ((100 * downloaded) / length).toFixed(2) | ||
console.log(progress) | ||
}) | ||
|
||
return this; | ||
} | ||
|
||
const statusCodes = [ | ||
{code: 200, run: __continueRequest}, | ||
{code: 401, message: 'Unauthoriz E397 ed request.'}, | ||
{code: 404, message: 'Track not found'}, | ||
{code: 500, message: 'Server Error'}] | ||
|
||
const handleStatusCode = (code) => { | ||
const result = statusCodes.filter(statusCode=>statusCode.code===code)[0] | ||
const run = result && result.run | ||
return run && typeof run === 'function' ? run : result.message | ||
} | ||
|
||
export const resolve = (url) => { | ||
const options = { | ||
uri: config.resolveUri, | ||
qs: { | ||
client_id: config.client_id, | ||
url | ||
}, | ||
json: true | ||
} | ||
|
||
return requestPromise(options) | ||
} | ||
|
||
export const writeStream = (ctx, title, reply) => { | ||
|
||
try { | ||
const now = Date.now().toString(), cwd = process.cwd(); | ||
const replyObj = reply.toJSON() | ||
const {headers, statusCode} = replyObj | ||
const fileName = title && title.replace(/[`~!@#$%^&*()_|+\-=÷¿?;:'",.<>\{ }{\}\[\]\\\/]/gi, '').trim(); | ||
const filePath = path.join(`${cwd}/downloads/${fileName}.mp3`) | ||
|
||
let fileSize = parseInt(headers['content-length'], 10) | ||
let len = fileSize.toFixed(2) | ||
|
||
const result = handleStatusCode(+statusCode) | ||
|
||
if (result && typeof result === 'string') { | ||
throw result | ||
} | ||
|
||
result.call(ctx, len).pipe(fs.createWriteStream(filePath, {flags: 'w+'})) | ||
} catch(err) { | ||
throw new Error(err) | ||
} | ||
|
||
} | ||
|
||
export const download = (streamUrl) => { | ||
/* | ||
STREAMING THE RESPONSE with request-promise (e.g. .pipe(...)) is DISCOURAGED | ||
because Request-Promise would grow the memory footprint for large requests | ||
unnecessarily high. Use the original Request library for that. | ||
*/ | ||
return request({ | ||
url: streamUrl, | ||
qs: { | ||
client_id: config.client_id | ||
} | ||
}) | ||
} |
Oops, something went wrong.