diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index 82c84e3d4..7f233d8ed 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -1,20 +1,16 @@ const got = require('@/utils/got'); -const iconv = require('iconv-lite'); -const cheerio = require('cheerio'); module.exports = { - getUsernameFromUID: async (ctx, uid) => { + getUsernameFromUID: (ctx, uid) => { const key = 'bili-username-from-uid-' + uid; - let name = await ctx.cache.get(key); - if (!name) { - const nameResponse = await got({ - method: 'get', - url: `https://api.bilibili.com/x/space/acc/info?mid=${uid}&jsonp=jsonp`, + return ctx.cache.tryGet(key, async () => { + const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&jsonp=jsonp`, { + headers: { + Referer: `https://space.bilibili.com/${uid}`, + }, }); - name = nameResponse.data.data.name; - ctx.cache.set(key, name); - } - return name; + return nameResponse.data.name; + }); }, getUsernameAndFaceFromUID: async (ctx, uid) => { const nameKey = 'bili-username-from-uid-' + uid; @@ -22,9 +18,10 @@ module.exports = { let name = await ctx.cache.get(nameKey); let face = await ctx.cache.get(faceKey); if (!name) { - const nameResponse = await got({ - method: 'get', - url: `https://api.bilibili.com/x/space/acc/info?mid=${uid}&jsonp=jsonp`, + const nameResponse = await got(`https://api.bilibili.com/x/space/acc/info?mid=${uid}&jsonp=jsonp`, { + headers: { + Referer: `https://space.bilibili.com/${uid}`, + }, }); name = nameResponse.data.data.name; face = nameResponse.data.data.face; @@ -33,80 +30,55 @@ module.exports = { } return [name, face]; }, - getLiveIDFromShortID: async (ctx, shortID) => { + getLiveIDFromShortID: (ctx, shortID) => { const key = `bili-liveID-from-shortID-${shortID}`; - let liveID = await ctx.cache.get(key); - if (!liveID) { - const liveIDResponse = await got({ - method: 'get', - url: `https://api.live.bilibili.com/room/v1/Room/room_init?id=${shortID}`, + return ctx.cache.tryGet(key, async () => { + const { data: liveIDResponse } = await got(`https://api.live.bilibili.com/room/v1/Room/room_init?id=${shortID}`, { headers: { Referer: `https://live.bilibili.com/${shortID}`, }, }); - liveID = liveIDResponse.data.data.room_id; - ctx.cache.set(key, liveID); - } - return liveID; + return liveIDResponse.data.room_id; + }); }, - getUsernameFromLiveID: async (ctx, liveID) => { + getUsernameFromLiveID: (ctx, liveID) => { const key = `bili-username-from-liveID-${liveID}`; - let name = await ctx.cache.get(key); - - if (!name) { - const nameResponse = await got({ - method: 'get', - url: `https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${liveID}`, + return ctx.cache.tryGet(key, async () => { + const { data: nameResponse } = await got(`https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${liveID}`, { headers: { Referer: `https://live.bilibili.com/${liveID}`, }, }); - name = nameResponse.data.data.info.uname; - ctx.cache.set(key, name); - } - return name; + return nameResponse.data.info.uname; + }); }, - getVideoNameFromId: async (ctx, aid, bvid) => { + getVideoNameFromId: (ctx, aid, bvid) => { const key = `bili-videoname-from-id-${bvid || aid}`; - let name = await ctx.cache.get(key); - - if (!name) { - const nameResponse = await got({ - method: 'get', - url: `https://www.bilibili.com/video/${bvid || `av${aid}`}`, - responseType: 'buffer', - }); - const responseHtml = iconv.decode(nameResponse.data, 'UTF-8'); - const $ = cheerio.load(responseHtml); - name = $('title').text(); - name = name.substr(0, name.indexOf('_哔哩哔哩')); - ctx.cache.set(key, name); - } - return name; - }, - getCidFromId: async (ctx, aid, pid, bvid) => { - const key = `bili-cid-from-id-${bvid || aid}-${pid}`; - let cid = await ctx.cache.get(key); - if (!cid) { - const cidResponse = await got({ - method: 'get', - url: `https://api.bilibili.com/x/web-interface/view?${bvid ? `bvid=${bvid}` : `aid=${aid}`}`, - headers: { - Referer: `https://www.bilibili.com/video/${bvid || `av${aid}`}`, + return ctx.cache.tryGet(key, async () => { + const { data } = await got(`https://api.bilibili.com/x/web-interface/view`, { + searchParams: { + aid: aid ? aid : undefined, + bvid: bvid ? bvid : undefined, }, + referer: `https://www.bilibili.com/video/${bvid || `av${aid}`}`, }); - cid = cidResponse.data.data.pages[pid - 1].cid; - ctx.cache.set(key, cid); - } - return cid; + return data.data.title; + }); + }, + getCidFromId: (ctx, aid, pid, bvid) => { + const key = `bili-cid-from-id-${bvid || aid}-${pid}`; + return ctx.cache.tryGet(key, async () => { + const { data } = await got(`https://api.bilibili.com/x/web-interface/view?${bvid ? `bvid=${bvid}` : `aid=${aid}`}`, { + referer: `https://www.bilibili.com/video/${bvid || `av${aid}`}`, + }); + return data.data.pages[pid - 1].cid; + }); }, getAidFromBvid: async (ctx, bvid) => { const key = `bili-cid-from-bvid-${bvid}`; let aid = await ctx.cache.get(key); if (!aid) { - const response = await got({ - method: 'get', - url: `https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`, + const response = await got(`https://api.bilibili.com/x/web-interface/view?bvid=${bvid}`, { headers: { Referer: `https://www.bilibili.com/video/${bvid}`, },