diff --git a/src/provider/qq.js b/src/provider/qq.js index 20a9ced22a..0e1a80a3da 100644 --- a/src/provider/qq.js +++ b/src/provider/qq.js @@ -3,10 +3,58 @@ const select = require('./select'); const request = require('../request'); const { getManagedCacheStorage } = require('../cache'); +let COOKIE = process.env.NEW_QQ_COOKIE || process.env.QQ_COOKIE; const headers = { origin: 'http://y.qq.com/', referer: 'http://y.qq.com/', - cookie: process.env.QQ_COOKIE || null, // 'uin=; qm_keyst=', + cookie: COOKIE || null, // 'uin=; qm_keyst=', +}; + +const refresh = () => { + const refresh_token = + (COOKIE.match(/qqrefresh_token=([^;]*)/) || [])[1] || ''; + const musickey = (COOKIE.match(/qqmusic_key=([^;]*)/) || [])[1] || ''; + const musicid = parseInt((COOKIE.match(/uin=(\d+)/) || [])[1] || 0, 10); + + const body = { + req: { + module: 'music.login.LoginServer', + method: 'Login', + param: { + refresh_token: refresh_token, + musickey: musickey, + musicid: musicid, + }, + }, + }; + const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'; + return request( + 'POST', + url, + { + Cookie: COOKIE, + 'Content-Type': 'application/json', + }, + JSON.stringify(body) + ) + .then((response) => response.json()) + .then((jsonBody) => { + const data = jsonBody.req?.data || {}; + const newMusickey = data.musickey; + const newMusickeyCreateTime = data.musickeyCreateTime; + if (!newMusickey || !newMusickeyCreateTime) return; + let newCookie = COOKIE; + newCookie = newCookie + .replace(/(qqmusic_key=)[^;]*/, `$1${newMusickey}`) + .replace(/(qm_keyst=)[^;]*/, `$1${newMusickey}`) + .replace( + /(psrf_musickey_createtime=)[^;]*/, + `$1${newMusickeyCreateTime}` + ); + process.env.NEW_QQ_COOKIE = newCookie; + headers.cookie = newCookie; + COOKIE = newCookie; + }); }; const format = (song) => ({ @@ -109,6 +157,13 @@ const track = (id) => { }; const cs = getManagedCacheStorage('provider/qq'); -const check = (info) => cs.cache(info, () => search(info)).then(track); +const check = (info) => { + const now = Math.floor(Date.now() / 1000); + const musickey_createtime = + ((COOKIE || '').match(/musickey_createtime=(\d+)/) || [])[1] || ''; + const createTime = parseInt(musickey_createtime, 10) + 259200; + if (musickey_createtime && createTime - now < 600) refresh(); + return cs.cache(info, () => search(info)).then(track); +}; module.exports = { check, track };