netease.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var express = require('express');
  2. var request = require('superagent');
  3. var router = express.Router();
  4. const util = require('../utils/utils')
  5. const links = {
  6. song: '/weapi/v3/song/detail',
  7. song_url: '/weapi/song/enhance/player/url',
  8. playlist: '/weapi/v3/playlist/detail'
  9. }
  10. /* GET users listing. */
  11. router.get('/:channel', function(req, res, next) {
  12. const id = req.query.id
  13. const br = req.query.br || 999000
  14. const channel = req.params['channel']
  15. let config = {
  16. path: links[channel],
  17. params: {
  18. "ids": [id],
  19. "br": 999000,
  20. "csrf_token": ""
  21. }
  22. }
  23. switch (channel) {
  24. case 'playlist':
  25. config['params'] = {
  26. "id": id,
  27. "offset": 0,
  28. "total": true,
  29. "limit": 1000,
  30. "n": 1000,
  31. "csrf_token": ""
  32. }
  33. break;
  34. case 'song':
  35. config['params'] = {
  36. 'c': JSON.stringify([{ id: id }]),
  37. "ids": '[' + id + ']',
  38. "csrf_token": ""
  39. }
  40. break;
  41. default:
  42. res.send({
  43. data: {},
  44. status: {
  45. code: -1,
  46. msg: 'no support url. Please set `song` or `playlist`'
  47. }
  48. })
  49. break;
  50. }
  51. util.requestServer(config).then(ret => {
  52. if (channel == 'song') {
  53. config['path'] = links.song_url
  54. config['params'] = {
  55. "ids": [id],
  56. "br": br,
  57. "csrf_token": ""
  58. }
  59. util.requestServer(config).then(rt => {
  60. let song = ret.songs[0]
  61. song['mp3'] = rt.data[0]
  62. res.send({
  63. data: song,
  64. status: {
  65. code: 200,
  66. msg: ''
  67. }
  68. })
  69. }).catch(ex => {
  70. res.send({
  71. data: {},
  72. status: {
  73. code: -1,
  74. msg: 'something happend. Please checked your id or url'
  75. }
  76. })
  77. })
  78. } else {
  79. res.send(ret)
  80. }
  81. }).catch(err => {
  82. res.send({
  83. data: {},
  84. status: {
  85. code: -1,
  86. msg: 'something happend. Please checked your id or url'
  87. }
  88. })
  89. })
  90. // res.header("Content-Type", "application/json;charset=utf-8");
  91. // //console.log('ref:' + req.header('referer'));
  92. // var id = req.query.id;
  93. // var playlist_id = req.query.playlist_id;
  94. // var headers = {};
  95. // headers['Cookie'] = 'appver=1.5.0.75771;';
  96. // headers['referer'] = 'http://music.163.com';
  97. // headers['User-Agent'] = req.headers['user-agent'];
  98. // var url = 'http://music.163.com/api/song/detail/?id=' + id + '&ids=%5B' + id + '%5D';
  99. // if (playlist_id) {
  100. // url = 'http://music.163.com/api/playlist/detail/?id=' + playlist_id;
  101. // }
  102. // netease_http(headers, url, next, function(data) {
  103. // var songs = data.songs;
  104. // songs.map(function(item) {
  105. // var url = item['mp3Url'];
  106. // item['sslUrl'] = url.replace('http://m', 'https://p');
  107. // return item;
  108. // });
  109. // data.songs = songs;
  110. // res.send(data)
  111. // // var output = {
  112. // // data: data,
  113. // // status: {
  114. // // code: 200,
  115. // // message: ''
  116. // // }
  117. // // };
  118. // // if (req.query.callback) {
  119. // // return res.jsonp(output)
  120. // // } else {
  121. // // return res.send(output);
  122. // // }
  123. // });
  124. });
  125. function netease_http(headers, url, next, callback) {
  126. request.get(url).set(headers).end(function(err, res) {
  127. var body = {};
  128. if (res && res.text) {
  129. body = res.text;
  130. } else if (res && res.body) {
  131. body = res.body;
  132. }
  133. if (typeof body === 'string') {
  134. try {
  135. body = JSON.parse(body);
  136. } catch (e) {}
  137. }
  138. if (!err && res.statusCode == 200) {
  139. callback && callback(body);
  140. } else {
  141. var error = new Error(err);
  142. error.status = 404;
  143. next(error);
  144. }
  145. });
  146. }
  147. module.exports = router;