From 697dbdf5bd67ad931831c2763f6083c30261773f Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 20 May 2016 10:33:55 +0800 Subject: [PATCH] back-end: id list --- README.md | 15 ++++++++++++++- nodejs/index.js | 36 +++++++++++++++++++++++++++++++++++- package.json | 2 +- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8cbdfa8..e3929d4 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ var option = { pic: '若能绽放光芒.png' // Optional, music picture }, danmaku: { // Optional, showing danmaku - id: '9E2E3368B56CDBB4', // Required, danmaku id, MUST BE UNIQUE + id: '9E2E3368B56CDBB4', // Required, danmaku id, MUST BE UNIQUE, CAN NOT USE THESE IN YOUR NEW PLAYER: `https://dplayer.daoapp.io/list` api: 'https://dplayer.daoapp.io/', // Required, danmaku api token: 'tokendemo' // Optional, danmaku token for api } @@ -90,6 +90,19 @@ var dp = new DPlayer({ }); ``` +### Danmaku back-end + +**Ready-made API:** + +`https://dplayer.daoapp.io/` + +**Build yourself:** + +Required environment + +- Node.js +- Mongodb + ## Run in development ``` diff --git a/nodejs/index.js b/nodejs/index.js index 9608455..b965161 100644 --- a/nodejs/index.js +++ b/nodejs/index.js @@ -35,7 +35,9 @@ else { } var danmakuSchema = new mongoose.Schema({ - player: String, + player: { + type: [String], index: true + }, author: String, time: Number, text: String, @@ -218,4 +220,36 @@ app.post('/', function (req, res) { } }); +app.get('/list', function (req, res) { + mongoose.connect(mongodbUrl); + var db = mongoose.connection; + db.on('error', errorListener); + + db.once('open', function() { + cleanListener(); + danmaku.distinct('player', function (err, data) { + if (err) { + logger.error(err); + } + + var json = ``; + for (var i = 0; i < data.length; i++) { + json += data[i] + `
`; + } + res.send(json); + db.close(); + }) + }); + + function errorListener (err) { + cleanListener(); + logger.error(err); + res.send(`{"code": 0, "msg": "Error happens, please contact system administrator."}`); + } + + function cleanListener () { + db.removeListener('error', errorListener); + } +}); + app.listen(1207); \ No newline at end of file diff --git a/package.json b/package.json index d60ab47..a06f61c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dplayer", - "version": "0.1.0", + "version": "0.1.2", "description": "Wow, such a lovely HTML5 danmaku video player", "main": "dist/DPlayer.min.js", "scripts": {