From 7475920a270b5b940c8e71197ffab2bbec690f98 Mon Sep 17 00:00:00 2001 From: qixingchen Date: Sat, 2 Jun 2018 23:42:49 +0800 Subject: [PATCH] app:add name cache for bili items (#259) --- routes/bilibili/cache.js | 85 +++++++++++++++++++++++++++++++++++ routes/bilibili/coin.js | 16 +------ routes/bilibili/fav.js | 17 +------ routes/bilibili/followers.js | 16 +------ routes/bilibili/followings.js | 17 +------ routes/bilibili/liveRoom.js | 23 ++-------- routes/bilibili/reply.js | 17 +------ routes/bilibili/video.js | 17 +------ 8 files changed, 100 insertions(+), 108 deletions(-) create mode 100644 routes/bilibili/cache.js diff --git a/routes/bilibili/cache.js b/routes/bilibili/cache.js new file mode 100644 index 000000000..f8d85cf30 --- /dev/null +++ b/routes/bilibili/cache.js @@ -0,0 +1,85 @@ +const axios = require('../../utils/axios'); +const qs = require('querystring'); +const config = require('../../config'); +const iconv = require('iconv-lite'); +const cheerio = require('cheerio'); + +module.exports = { + getUsernameFromUID: async (ctx, uid) => { + const key = 'bili-username-from-uid-' + uid; + let name = await ctx.cache.get(key); + if (!name) { + const nameResponse = await axios({ + method: 'post', + url: 'https://space.bilibili.com/ajax/member/GetInfo', + headers: { + 'User-Agent': config.ua, + Referer: `https://space.bilibili.com/${uid}/`, + 'Content-Type': 'application/x-www-form-urlencoded', + }, + data: qs.stringify({ + mid: uid, + }), + }); + name = nameResponse.data.data.name; + ctx.cache.set(key, name, 24 * 60 * 60); + } + return name; + }, + getLiveIDFromShortID: async (ctx, shortID) => { + const key = `bili-liveID-from-shortID-${shortID}`; + let liveID = await ctx.cache.get(key); + if (!liveID) { + const liveIDResponse = await axios({ + method: 'get', + url: `https://api.live.bilibili.com/room/v1/Room/room_init?id=${shortID}`, + headers: { + 'User-Agent': config.ua, + Referer: `https://live.bilibili.com/${shortID}`, + }, + }); + liveID = liveIDResponse.data.data.room_id; + ctx.cache.set(key, liveID, 24 * 60 * 60); + } + return liveID; + }, + getUsernameFromLiveID: async (ctx, liveID) => { + const key = `bili-username-from-liveID-${liveID}`; + let name = await ctx.cache.get(key); + + if (!name) { + const nameResponse = await axios({ + method: 'get', + url: `https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${liveID}`, + headers: { + 'User-Agent': config.ua, + Referer: `https://live.bilibili.com/${liveID}`, + }, + }); + name = nameResponse.data.data.info.uname; + ctx.cache.set(key, name, 24 * 60 * 60); + } + return name; + }, + getVideoNameFromAid: async (ctx, aid) => { + const key = `bili-videoName-from-aid-${aid}`; + let name = await ctx.cache.get(key); + + if (!name) { + const nameResponse = await axios({ + method: 'get', + url: `https://www.bilibili.com/video/av${aid}`, + headers: { + 'User-Agent': config.ua, + }, + responseType: 'arraybuffer', + }); + 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, 24 * 60 * 60); + } + return name; + }, +}; diff --git a/routes/bilibili/coin.js b/routes/bilibili/coin.js index 99f62c7d6..9a2f01516 100644 --- a/routes/bilibili/coin.js +++ b/routes/bilibili/coin.js @@ -1,23 +1,11 @@ const axios = require('../../utils/axios'); -const qs = require('querystring'); const config = require('../../config'); +const cache = require('./cache'); module.exports = async (ctx) => { const uid = ctx.params.uid; - const nameResponse = await axios({ - method: 'post', - url: 'https://space.bilibili.com/ajax/member/GetInfo', - headers: { - 'User-Agent': config.ua, - Referer: `https://space.bilibili.com/${uid}/`, - 'Content-Type': 'application/x-www-form-urlencoded', - }, - data: qs.stringify({ - mid: uid, - }), - }); - const name = nameResponse.data.data.name; + const name = await cache.getUsernameFromUID(ctx, uid); const response = await axios({ method: 'get', diff --git a/routes/bilibili/fav.js b/routes/bilibili/fav.js index 486d1b141..71dce9090 100644 --- a/routes/bilibili/fav.js +++ b/routes/bilibili/fav.js @@ -1,23 +1,10 @@ const axios = require('../../utils/axios'); -const qs = require('querystring'); const config = require('../../config'); +const cache = require('./cache'); module.exports = async (ctx) => { const uid = ctx.params.uid; - - const nameResponse = await axios({ - method: 'post', - url: 'https://space.bilibili.com/ajax/member/GetInfo', - headers: { - 'User-Agent': config.ua, - Referer: `https://space.bilibili.com/${uid}/`, - 'Content-Type': 'application/x-www-form-urlencoded', - }, - data: qs.stringify({ - mid: uid, - }), - }); - const name = nameResponse.data.data.name; + const name = await cache.getUsernameFromUID(ctx, uid); const response = await axios({ method: 'get', diff --git a/routes/bilibili/followers.js b/routes/bilibili/followers.js index 83f5616b8..98a6bc2c0 100644 --- a/routes/bilibili/followers.js +++ b/routes/bilibili/followers.js @@ -1,23 +1,11 @@ const axios = require('../../utils/axios'); -const qs = require('querystring'); const config = require('../../config'); +const cache = require('./cache'); module.exports = async (ctx) => { const uid = ctx.params.uid; - const nameResponse = await axios({ - method: 'post', - url: 'https://space.bilibili.com/ajax/member/GetInfo', - headers: { - 'User-Agent': config.ua, - Referer: `https://space.bilibili.com/${uid}/`, - 'Content-Type': 'application/x-www-form-urlencoded', - }, - data: qs.stringify({ - mid: uid, - }), - }); - const name = nameResponse.data.data.name; + const name = await cache.getUsernameFromUID(ctx, uid); const countResponse = await axios({ method: 'get', diff --git a/routes/bilibili/followings.js b/routes/bilibili/followings.js index 4aa655c6a..82859c828 100644 --- a/routes/bilibili/followings.js +++ b/routes/bilibili/followings.js @@ -1,23 +1,10 @@ const axios = require('../../utils/axios'); -const qs = require('querystring'); const config = require('../../config'); +const cache = require('./cache'); module.exports = async (ctx) => { const uid = ctx.params.uid; - - const nameResponse = await axios({ - method: 'post', - url: 'https://space.bilibili.com/ajax/member/GetInfo', - headers: { - 'User-Agent': config.ua, - Referer: `https://space.bilibili.com/${uid}/`, - 'Content-Type': 'application/x-www-form-urlencoded', - }, - data: qs.stringify({ - mid: uid, - }), - }); - const name = nameResponse.data.data.name; + const name = await cache.getUsernameFromUID(ctx, uid); const countResponse = await axios({ method: 'get', diff --git a/routes/bilibili/liveRoom.js b/routes/bilibili/liveRoom.js index 64d894e0c..0e5d6aec4 100644 --- a/routes/bilibili/liveRoom.js +++ b/routes/bilibili/liveRoom.js @@ -1,32 +1,15 @@ const axios = require('../../utils/axios'); const config = require('../../config'); +const cache = require('./cache'); module.exports = async (ctx) => { let roomID = ctx.params.roomID; // 短号查询长号 if (parseInt(roomID, 10) < 10000) { - const roomIDResponse = await axios({ - method: 'get', - url: `https://api.live.bilibili.com/room/v1/Room/room_init?id=${roomID}`, - headers: { - 'User-Agent': config.ua, - Referer: `https://live.bilibili.com/${roomID}`, - }, - }); - roomID = roomIDResponse.data.data.room_id; + roomID = await cache.getLiveIDFromShortID(ctx, roomID); } - - const nameResponse = await axios({ - method: 'get', - url: `https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${roomID}`, - headers: { - 'User-Agent': config.ua, - Referer: `https://live.bilibili.com/${roomID}`, - }, - }); - - const name = nameResponse.data.data.info.uname; + const name = await cache.getUsernameFromLiveID(ctx, roomID); const response = await axios({ method: 'get', diff --git a/routes/bilibili/reply.js b/routes/bilibili/reply.js index cb4d4671e..b28c6a5d5 100644 --- a/routes/bilibili/reply.js +++ b/routes/bilibili/reply.js @@ -1,23 +1,10 @@ const axios = require('../../utils/axios'); const config = require('../../config'); -const cheerio = require('cheerio'); -const iconv = require('iconv-lite'); +const cache = require('./cache'); module.exports = async (ctx) => { const aid = ctx.params.aid; - - const nameResponse = await axios({ - method: 'get', - url: `https://www.bilibili.com/video/av${aid}`, - headers: { - 'User-Agent': config.ua, - }, - responseType: 'arraybuffer', - }); - const responseHtml = iconv.decode(nameResponse.data, 'UTF-8'); - const $ = cheerio.load(responseHtml); - let name = $('title').text(); - name = name.substr(0, name.indexOf('_哔哩哔哩')); + const name = await cache.getVideoNameFromAid(ctx, aid); const response = await axios({ method: 'get', diff --git a/routes/bilibili/video.js b/routes/bilibili/video.js index 4fda700df..88440e683 100644 --- a/routes/bilibili/video.js +++ b/routes/bilibili/video.js @@ -1,23 +1,10 @@ const axios = require('../../utils/axios'); -const qs = require('querystring'); const config = require('../../config'); +const cache = require('./cache'); module.exports = async (ctx) => { const uid = ctx.params.uid; - - const nameResponse = await axios({ - method: 'post', - url: 'https://space.bilibili.com/ajax/member/GetInfo', - headers: { - 'User-Agent': config.ua, - Referer: `https://space.bilibili.com/${uid}/`, - 'Content-Type': 'application/x-www-form-urlencoded', - }, - data: qs.stringify({ - mid: uid, - }), - }); - const name = nameResponse.data.data.name; + const name = await cache.getUsernameFromUID(ctx, uid); const response = await axios({ method: 'get',