Back-end: use redis

This commit is contained in:
DIYgod 2016-10-12 23:19:28 +08:00
parent b31bc4c1dc
commit ded7f98976
No known key found for this signature in database
GPG Key ID: F8797DD1088C6506
10 changed files with 107 additions and 65 deletions

2
dist/DPlayer.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,6 @@
FROM node:4.4-onbuild
EXPOSE 1207
RUN apt-get install redis-server
RUN redis-server
RUN npm install -g forever
ENTRYPOINT forever --spinSleepTime 1000 --minUptime 1000 index.js

View File

@ -11,6 +11,7 @@
"log4js": "^0.6.36",
"mongoose": "^4.1.9",
"node-fetch": "^1.6.3",
"redis": "^2.6.2",
"xml2js": "^0.4.17"
}
}

View File

@ -1,5 +1,6 @@
var url = require('url');
var logger = require('../tools/logger');
var redis = require('../tools/redis');
var fetch = require('node-fetch');
var parseString = require('xml2js').parseString;
@ -8,58 +9,69 @@ module.exports = function (req, res) {
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
logger.info(`bilibili form IP: ${ip}`);
var query = url.parse(req.url,true).query;
var aid = query.aid;
var dan = {
code: 1,
danmaku: []
};
function addZero(str, length){
return new Array(length - str.length + 1).join("0") + str;
}
fetch(`http://www.bilibili.com/widget/getPageList?aid=${aid}`).then(
response => response.json()
).then((data) => {
fetch(`http://comment.bilibili.com/${data[0].cid}.xml`).then(
response => response.text()
).then((data) => {
parseString(data, function (err, result) {
var danOriginal = result.i.d;
for (var i = 0; i < danOriginal.length; i++) {
console.log(danOriginal[i].$.p);
var info = danOriginal[i].$.p.split(',');
console.log(info);
var type = '';
if (info[1] === '4') {
type = 'bottom';
redis.client.get(`bilibili${aid}`, function(err, reply) {
if (reply) {
logger.info(`Bilibili AV${aid} form redis, IP: ${ip}`);
res.send(reply);
}
else {
logger.info(`Bilibili AV${aid} form origin, IP: ${ip}`);
var dan = {
code: 1,
danmaku: []
};
fetch(`http://www.bilibili.com/widget/getPageList?aid=${aid}`).then(
response => response.json()
).then((data) => {
fetch(`http://comment.bilibili.com/${data[0].cid}.xml`).then(
response => response.text()
).then((data) => {
parseString(data, function (err, result) {
var danOriginal = result.i.d;
for (var i = 0; i < danOriginal.length; i++) {
var info = danOriginal[i].$.p.split(',');
var type = '';
if (info[1] === '4') {
type = 'bottom';
}
else if (info[1] === '5') {
type = 'top';
}
else {
type = 'right';
}
var danOne = {
author: 'bilibili' + info[6],
time: info[0],
text: danOriginal[i]._,
color: '#' + addZero(parseInt(info[3]).toString(16), 6),
type: type
};
dan.danmaku.push(danOne);
}
var sendDan = JSON.stringify(dan);
res.send(sendDan);
redis.set(`bilibili${aid}`, sendDan);
});
}
else if (info[1] === '5') {
type = 'top';
}
else {
type = 'right';
}
var danOne = {
author: 'bilibili' + info[6],
time: info[0],
text: danOriginal[i]._,
color: '#' + addZero(parseInt(info[3]).toString(16), 6),
type: type
};
dan.danmaku.push(danOne);
}
res.send(JSON.stringify(dan));
});
}
).catch(
e => console.log("获取弹幕失败", e)
);
}
).catch(
e => console.log("获取cid失败", e)
);
).catch(
e => logger.error("Bilibilib Error: getting danmaku", e)
);
}
).catch(
e => logger.error("Bilibilib Error: getting cid", e)
);
}
});
};

View File

@ -1,32 +1,41 @@
var url = require('url');
var logger = require('../tools/logger');
var danmaku = require('../models/danmaku');
var redis = require('../tools/redis');
module.exports = function (req, res) {
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
logger.info(`GET form IP: ${ip}`);
var query = url.parse(req.url,true).query;
var id = query.id;
var max = query.max;
var length;
danmaku.find({player: id}, function (err, data) {
if (err) {
logger.error(err);
}
var json = `{"code": 1,"danmaku":[`;
length = max ? Math.min(data.length, max) : data.length;
for (var i = 0; i < length; i++) {
json += JSON.stringify(data[i]);
if (i !== length - 1) {
json += `,`;
}
redis.client.get(`dplayer${id}`, function(err, reply) {
if (reply) {
logger.info(`DPlayer id ${id} form redis, IP: ${ip}`);
res.send(reply);
}
json += `]}`;
res.send(json);
})
else {
logger.info(`DPlayer id ${id} form mongodb, IP: ${ip}`);
danmaku.find({player: id}, function (err, data) {
if (err) {
logger.error(err);
}
var dan = {
code: 1,
danmaku: []
};
dan.danmaku = max ? data.slice(0, max) : data;
var sendDan = JSON.stringify(dan);
res.send(sendDan);
redis.set(`dplayer${id}`, sendDan);
})
}
});
};

View File

@ -2,6 +2,7 @@ var url = require('url');
var fs = require('fs');
var logger = require('../tools/logger');
var danmaku = require('../models/danmaku');
var redis = require('../tools/redis');
function htmlEncode(str) {
return str.replace(/&/g, "&amp;")
@ -104,6 +105,7 @@ module.exports = function (req, res) {
}
else {
res.send(`{"code": 1, "data": ${JSON.stringify(d)}}`);
redis.client.del(`dplayer${htmlEncode(jsonStr.player)}`);
}
});
}

16
nodejs/tools/redis.js Normal file
View File

@ -0,0 +1,16 @@
var logger = require('./logger');
var redis = require("redis");
var client = redis.createClient();
client.on("error", function (err) {
logger.error('Redis Error ' + err);
});
module.exports = {
set: function (key, value) {
client.set(key, value, redis.print);
client.expire(key, 86400);
logger.info('Set redis: ' + key);
},
client: client
};

View File

@ -1,6 +1,6 @@
{
"name": "dplayer",
"version": "1.1.1",
"version": "1.1.2",
"description": "Wow, such a lovely HTML5 danmaku video player",
"main": "dist/DPlayer.min.js",
"scripts": {

View File

@ -1,4 +1,4 @@
console.log("\n %c DPlayer 1.1.1 %c http://dplayer.js.org \n\n","color: #fadfa3; background: #030307; padding:5px 0;","background: #fadfa3; padding:5px 0;");
console.log("\n %c DPlayer 1.1.2 %c http://dplayer.js.org \n\n","color: #fadfa3; background: #030307; padding:5px 0;","background: #fadfa3; padding:5px 0;");
require('./DPlayer.scss');