first version
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user