Browse Source

getting bilibili danmaku and video by aid, close #37, close #34

DIYgod 8 years ago
parent
commit
9a25245221
2 changed files with 147 additions and 90 deletions
  1. 108 53
      nodejs/routes/bilibili.js
  2. 39 37
      nodejs/routes/video-bilibili.js

+ 108 - 53
nodejs/routes/bilibili.js

@@ -12,66 +12,121 @@ module.exports = function (req, res) {
 
     var query = url.parse(req.url,true).query;
     var aid = query.aid;
+    var cid = query.cid;
 
     function addZero(str, length){
         return new Array(length - str.length + 1).join("0") + str;
     }
 
-    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}`);
+    if (cid) {
+        redis.client.get(`bilibilicid2dan${cid}`, function(err, reply) {
+            if (reply) {
+                logger.info(`Bilibili cid2dan ${cid} form redis, IP: ${ip}`);
+                res.send(reply);
+            }
+            else {
+                logger.info(`Bilibili cid2dan ${cid} form origin, IP: ${ip}`);
 
-            var dan = {
-                code: 1,
-                danmaku: []
-            };
+                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);
+                fetch(`http://comment.bilibili.com/${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';
                                 }
-                                var sendDan = JSON.stringify(dan);
-                                res.send(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);
+                            }
+                            var sendDan = JSON.stringify(dan);
+                            res.send(sendDan);
 
-                                redis.set(`bilibili${aid}`, sendDan);
-                            });
-                        }
-                    ).catch(
-                        e => logger.error("Bilibilib Error: getting danmaku", e)
-                    );
-                }
-            ).catch(
-                e => logger.error("Bilibilib Error: getting cid", e)
-            );
-        }
-    });
+                            redis.set(`bilibilicid2dan${cid}`, sendDan);
+                        });
+                    }
+                ).catch(
+                    e => logger.error("Bilibilib Error: getting danmaku", e)
+                );
+            }
+        });
+    }
+    else {
+        redis.client.get(`bilibiliaid2dan${aid}`, function(err, reply) {
+            if (reply) {
+                logger.info(`Bilibili aid2dan ${aid} form redis, IP: ${ip}`);
+                res.send(reply);
+            }
+            else {
+                logger.info(`Bilibili aid2dan ${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(`bilibiliaid2dan${aid}`, sendDan);
+                                });
+                            }
+                        ).catch(
+                            e => logger.error("Bilibilib Error: getting danmaku", e)
+                        );
+                    }
+                ).catch(
+                    e => logger.error("Bilibilib Error: getting cid", e)
+                );
+            }
+        });
+    }
 };

+ 39 - 37
nodejs/routes/video-bilibili.js

@@ -4,6 +4,20 @@ var redis = require('../tools/redis');
 var fetch = require('node-fetch');
 var md5 = require('blueimp-md5');
 
+var appkey = '4ebafd7c4951b366';
+var secret = '8cb98205e9b2ad3669aad0fce12a4c13';
+function getData(cid, res) {
+    var sign = md5(`appkey=${appkey}&cid=${cid}&otype=json&quality=2&type=mp4${secret}`);
+    fetch(`https://interface.bilibili.com/playurl?appkey=${appkey}&cid=${cid}&otype=json&quality=2&type=mp4&sign=${sign}`).then(
+        response => response.text()
+    ).then((data) => {
+            res.send(data);
+        }
+    ).catch(
+        e => logger.error("Bilibilib Error: getting data", e)
+    );
+}
+
 module.exports = function (req, res) {
     var ip = req.headers['x-forwarded-for'] ||
         req.connection.remoteAddress ||
@@ -12,43 +26,31 @@ module.exports = function (req, res) {
 
     var query = url.parse(req.url,true).query;
     var aid = query.aid;
-    var appkey = '4ebafd7c4951b366';
-    var secret = '8cb98205e9b2ad3669aad0fce12a4c13';
+    var cid = query.cid;
 
-    redis.client.get(`bilibilicid${aid}`, function(err, reply) {
-        if (reply) {
-            logger.info(`Video Bilibili AV${aid} form redis, IP: ${ip}`);
+    if (cid) {
+        logger.info(`Bilibili cid2video ${cid}, IP: ${ip}`);
+        getData(cid, res);
+    }
+    else {
+        redis.client.get(`bilibiliaid2cid${aid}`, function(err, reply) {
+            if (reply) {
+                logger.info(`Bilibili aid2video ${aid} form redis, IP: ${ip}`);
+                getData(reply, res);
+            }
+            else {
+                logger.info(`Bilibili aid2video ${aid} form origin, IP: ${ip}`);
 
-            var sign = md5(`appkey=${appkey}&cid=${reply}&otype=json&quality=2&type=mp4${secret}`);
-            fetch(`https://interface.bilibili.com/playurl?appkey=${appkey}&cid=${reply}&otype=json&quality=2&type=mp4&sign=${sign}`).then(
-                response => response.text()
-            ).then((data) => {
-                    res.send(data);
-                }
-            ).catch(
-                e => logger.error("Video Bilibilib Error: getting data", e)
-            );
-        }
-        else {
-            logger.info(`Video Bilibili AV${aid} form origin, IP: ${ip}`);
-
-            fetch(`http://www.bilibili.com/widget/getPageList?aid=${aid}`).then(
-                response => response.json()
-            ).then((data) => {
-                    redis.set(`bilibilicid${aid}`, data[0].cid);
-                    var sign = md5(`appkey=${appkey}&cid=${data[0].cid}&otype=json&quality=2&type=mp4${secret}`);
-                    fetch(`https://interface.bilibili.com/playurl?appkey=${appkey}&cid=${data[0].cid}&otype=json&quality=2&type=mp4&sign=${sign}`).then(
-                        response => response.text()
-                    ).then((data) => {
-                            res.send(data);
-                        }
-                    ).catch(
-                        e => logger.error("Video Bilibilib Error: getting data", e)
-                    );
-                }
-            ).catch(
-                e => logger.error("Video Bilibilib Error: getting cid", e)
-            );
-        }
-    });
+                fetch(`http://www.bilibili.com/widget/getPageList?aid=${aid}`).then(
+                    response => response.json()
+                ).then((data) => {
+                        redis.set(`bilibiliaid2cid${aid}`, data[0].cid);
+                        getData(data[0].cid, res);
+                    }
+                ).catch(
+                    e => logger.error("Bilibili aid2video Error: getting cid", e)
+                );
+            }
+        });
+    }
 };