playlistDetail.js 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
const http = require('http')
const express = require("express")
const router = express()
const { createWebAPIRequest } = require("../util/util")

router.get("/", (req, res) => {
  const cookie = req.get('Cookie') ? req.get('Cookie') : ''
  let detail, imgurl
  const data = {
    "id": req.query.id,
    "offset": 0,
    "total": true,
    "limit": 1000,
    "n": 1000,
    "csrf_token": ""
  }

  createWebAPIRequest(
    'music.163.com',
    '/weapi/v3/playlist/detail',
    'POST',
    data,
    cookie,
24
    music_req => {
25 26 27 28
      console.log(music_req)
      detail = music_req
      mergeRes()
    },
29
    err => {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
      res.status(502).send('fetch error')
    }
  )

  // FIXME:i dont know the api to get coverimgurl
  // so i get it by parsing html
  const http_client = http.get({
    hostname: 'music.163.com',
    path: '/playlist?id=' + req.query.id,
    headers: {
      'Referer': 'http://music.163.com',
    },
  }, function (res) {
    res.setEncoding('utf8')
    let html = ''
    res.on('data', function (chunk) {
      html += chunk
    })
    res.on('end', function () {
      console.log('end', html)
      const regImgCover = /\<img src=\"(.*)\" class="j-img"/ig
      imgurl = regImgCover.exec(html)[1]
      mergeRes()

    })
  })

  function mergeRes() {
    if (imgurl != undefined && detail != undefined) {
      detail = JSON.parse(detail)
      detail.playlist.picUrl = imgurl
      res.send(detail)
    }
  }
})



module.exports = router