8000 Initial commit v1.0.0 · rvpanoz/sndc@47dfdf2 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Initial commit v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rvpanoz committed Aug 18, 2018
1 parent 60f4a80 commit 47dfdf2
Show file tree
Hide file tree
Showing 7 changed files with 5,150 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": ["env"],
"plugins": [
"transform-object-rest-spread"
]
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ typings/

# next.js build output
.next

# prettier
prettier.config.js

# Downloads
downloads
13 changes: 13 additions & 0 deletions config.js
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
82 changes: 82 additions & 0 deletions lib/index.js
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
}
})
}
Loading

0 comments on commit 47dfdf2

Please sign in to comment.
0