first version

This commit is contained in:
Jay
2019-08-13 07:39:49 +08:00
commit 1fda2ff1f6
14 changed files with 2590 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
const store = {}
const storeFunc = {
/**
* set store key vlaue
* @param {string} key
* @param {object} val
* @return {boolean}
*/
set: (key, val) => {
if (typeof key !== 'string' || key.length === 0) return false
store[key] = val
return true
},
/**
* get store key value
* @param {string} key
* @return {object?}
*/
get: (key) => {
if (typeof key !== 'string' || key.length === 0) return null
return store[key] || null
},
/**
* del store key vlaue
* @param {string} key
* @return {boolean}
*/
del: (key) => {
if (typeof key !== 'string' || key.length === 0) return false
delete store[key]
return true
}
}
module.exports = storeFunc
+40
View File
@@ -0,0 +1,40 @@
const exec = require('child_process').exec
const command = 'youtube-dl'
const cmdOpts = ['--id', '-x', '--audio-format', 'mp3']
const config = require('@config/index.js')
const cwd = config.download_location
/**
* download youtube video
* @param {string} url
* @return {boolean}
*/
module.exports.download = async (url = '') => {
if (typeof url !== 'string' || url.trim().length === 0) return false
const result = await new Promise(resolve => {
exec(`${command} ${cmdOpts.join(' ')} "${url}"`, { cwd }, (err, sout, serr) => {
if (err) return resolve(false)
return resolve(true)
})
})
return result
}
/**
* get youtube video title
* @param {string} url
* @return {string}
*/
module.exports.getTitle = async (url = '') => {
if (typeof url !== 'string' || url.trim().length === 0) return ''
const result = await new Promise(resolve => {
exec(`${command} -e "${url}"`, { cwd }, (err, sout, serr) => {
if (err) return resolve('')
resolve(sout)
})
})
return result
}