bilibili.js 5.6 KB

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