bilibili.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var url = require('url');
  2. var logger = require('../tools/logger');
  3. var redis = require('../tools/redis');
  4. var fetch = require('node-fetch');
  5. var parseString = require('xml2js').parseString;
  6. module.exports = function (req, res) {
  7. var ip = req.headers['x-forwarded-for'] ||
  8. req.connection.remoteAddress ||
  9. req.socket.remoteAddress ||
  10. req.connection.socket.remoteAddress;
  11. var query = url.parse(req.url,true).query;
  12. var aid = query.aid;
  13. var cid = query.cid;
  14. function addZero(str, length){
  15. return new Array(Math.max(length - str.length + 1, 0)).join("0") + str;
  16. }
  17. if (cid) {
  18. redis.client.get(`bilibilicid2dan${cid}`, function(err, reply) {
  19. if (reply) {
  20. logger.info(`Bilibili cid2dan ${cid} form redis, IP: ${ip}`);
  21. res.send(reply);
  22. }
  23. else {
  24. logger.info(`Bilibili cid2dan ${cid} form origin, IP: ${ip}`);
  25. var dan = {
  26. code: 1,
  27. danmaku: []
  28. };
  29. fetch(`http://comment.bilibili.com/${cid}.xml`).then(
  30. response => response.text()
  31. ).then((data) => {
  32. parseString(data, function (err, result) {
  33. var danOriginal = result.i.d;
  34. for (var i = 0; i < danOriginal.length; i++) {
  35. var info = danOriginal[i].$.p.split(',');
  36. var type = '';
  37. if (info[1] === '4') {
  38. type = 'bottom';
  39. }
  40. else if (info[1] === '5') {
  41. type = 'top';
  42. }
  43. else {
  44. type = 'right';
  45. }
  46. var danOne = {
  47. author: 'bilibili' + info[6],
  48. time: info[0],
  49. text: danOriginal[i]._,
  50. color: '#' + addZero(parseInt(info[3]).toString(16), 6),
  51. type: type
  52. };
  53. dan.danmaku.push(danOne);
  54. }
  55. var sendDan = JSON.stringify(dan);
  56. res.send(sendDan);
  57. redis.set(`bilibilicid2dan${cid}`, sendDan);
  58. });
  59. }
  60. ).catch(
  61. e => logger.error("Bilibilib Error: getting danmaku", e)
  62. );
  63. }
  64. });
  65. }
  66. else {
  67. redis.client.get(`bilibiliaid2dan${aid}`, function(err, reply) {
  68. if (reply) {
  69. logger.info(`Bilibili aid2dan ${aid} form redis, IP: ${ip}`);
  70. res.send(reply);
  71. }
  72. else {
  73. logger.info(`Bilibili aid2dan ${aid} form origin, IP: ${ip}`);
  74. var dan = {
  75. code: 1,
  76. danmaku: []
  77. };
  78. fetch(`http://www.bilibili.com/widget/getPageList?aid=${aid}`).then(
  79. response => response.json()
  80. ).then((data) => {
  81. fetch(`http://comment.bilibili.com/${data[0].cid}.xml`).then(
  82. response => response.text()
  83. ).then((data) => {
  84. parseString(data, function (err, result) {
  85. var danOriginal = result.i.d;
  86. for (var i = 0; i < danOriginal.length; i++) {
  87. var info = danOriginal[i].$.p.split(',');
  88. var type = '';
  89. if (info[1] === '4') {
  90. type = 'bottom';
  91. }
  92. else if (info[1] === '5') {
  93. type = 'top';
  94. }
  95. else {
  96. type = 'right';
  97. }
  98. var danOne = {
  99. author: 'bilibili' + info[6],
  100. time: info[0],
  101. text: danOriginal[i]._,
  102. color: '#' + addZero(parseInt(info[3]).toString(16), 6),
  103. type: type
  104. };
  105. dan.danmaku.push(danOne);
  106. }
  107. var sendDan = JSON.stringify(dan);
  108. res.send(sendDan);
  109. redis.set(`bilibiliaid2dan${aid}`, sendDan);
  110. });
  111. }
  112. ).catch(
  113. e => logger.error("Bilibilib Error: getting danmaku", e)
  114. );
  115. }
  116. ).catch(
  117. e => logger.error("Bilibilib Error: getting cid", e)
  118. );
  119. }
  120. });
  121. }
  122. };