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
#!/usr/bin/env node | |
"use strict" | |
const exec = require('child_process').spawn; | |
const http = require('http'); | |
const vm = require('vm'); | |
const rl = require('readline'); | |
console.log('=== I 💓 panda ==\n'); | |
let pd = { | |
api: 'http://vdn.live.cntv.cn/api2/liveHtml5.do?channel=pa://cctv_p2p_hdipanda', | |
store: new Map(), | |
playback: (url)=> { | |
//exec('mpv', ['--no-resume-playback', url], { stdio: 'inherit' }); | |
let shell = exec('mpv', | |
['--no-resume-playback', '--no-terminal', url], | |
{ detached: true, stdio: 'ignore' }); | |
shell.unref(); | |
}, | |
ask: (question, callback)=> { | |
let r = rl.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
return new Promise((resolve, error) => { | |
r.question('\n' + question + '\n', (answer)=> { | |
r.close(); | |
resolve(answer); | |
}); | |
}); | |
}, | |
menu: ()=> { | |
//console.log(this.data.flv_url); | |
let idx = 0; | |
Object.keys(this.data).map((k) => { | |
if (k.includes('url')) { | |
let quality = this.data[k]; | |
let arr = Object.keys(quality).map((p)=> { | |
if (quality[p]) { | |
idx++; | |
pd.store.set(idx, quality[p]); | |
return `[${idx}]: ` + quality[p]; | |
} | |
}).filter(d=> !!d).join('\n'); | |
console.log('\n' +k.trim() + ':\n' + arr); | |
} | |
}); | |
pd.ask('Which one?').then((idx)=> { | |
let p = pd.store.get(parseInt(idx)); | |
//console.log(pd.store, '\n\n===' + p); | |
if (p) { | |
console.log('Now Playing: ' + p); | |
pd.playback(p) | |
} | |
}); | |
}, | |
parseJSONP: (str)=> { | |
let sandbox = { | |
getHtml5VideoData: (s)=> { | |
this.data = JSON.parse(s); | |
} | |
} | |
let context = new vm.createContext(sandbox); | |
let script = new vm.Script(str); | |
script.runInContext(context); | |
}, | |
fetch: ()=> { | |
http.get(pd.api, (res)=> { | |
res.setEncoding('utf8'); | |
let body = ''; | |
res.on('data', (chunk)=> { | |
body += chunk; | |
}).on('end', ()=> { | |
pd.parseJSONP(body); | |
pd.menu(); | |
}) | |
}).on('error', (e)=> { | |
console.log('err: ' + e.message); | |
}) | |
} | |
} | |
pd.fetch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment