app:add name cache for bili items (#259)

This commit is contained in:
qixingchen
2018-06-02 23:42:49 +08:00
committed by DIYgod
parent 68b259f643
commit 7475920a27
8 changed files with 100 additions and 108 deletions
+85
View File
@@ -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;
},
};
+2 -14
View File
@@ -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',
+2 -15
View File
@@ -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',
+2 -14
View File
@@ -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',
+2 -15
View File
@@ -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',
+3 -20
View File
@@ -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',
+2 -15
View File
@@ -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',
+2 -15
View File
@@ -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',