LotteryAutoScript/lib/BiliAPI.js
2020-12-26 13:22:38 +08:00

560 lines
19 KiB
JavaScript

const Ajax = require('./Ajax');
const Base = require('./Base');
const config = require("./config");
const GlobalVar = require('./GlobalVar.json');
/**
* 网络请求
*/
const BiliAPI = {
/**
* 判断是否成功登录
* @returns {Promise<boolean>}
*/
getMyinfo: async () => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.bilibili.com/x/space/myinfo',
hasCookies: true,
success: responseText => {
let res = Base.strToJson(responseText);
if (res.code === 0) {
resolve(true);
} else {
resolve(false);
}
}
})
});
},
/**
* 获取被at的信息
* @returns {
Promise<{
at_time: number;
id: number;
nickname: string;
business: string;
source_content: string;
url: string
}[] | []>
}
*/
getMyAtInfo: async () => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.bilibili.com/x/msgfeed/at',
hasCookies: true,
success: responseText => {
const res = Base.strToJson(responseText);
const atInfo = [];
if (res.code === 0) {
const items = res.data.items;
if (items.length !== 0) {
items.forEach(i => {
const { at_time, id, item, user } = i,
{ nickname } = user,
{ business, uri: url, source_content } = item;
atInfo.push({
at_time,
id,
nickname,
business,
source_content,
url
});
});
}
resolve(atInfo);
} else {
resolve(atInfo);
}
}
});
});
},
/**
* 删除at信息
* @param {number} id
* @returns {Promise<void>}
*/
delAtinfo: async (id) => {
return new Promise((resolve) => {
Ajax.post({
url: 'https://api.bilibili.com/x/msgfeed/del',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
tp: 2,
id,
csrf: GlobalVar.csrf
},
success: responseText => {
let res = Base.strToJson(responseText);
if (res.code === 0) {
console.log('删除at信息成功');
resolve();
} else {
console.log(`删除at信息失败`);
resolve();
}
}
})
});
},
/**
* 获取关注列表
* @param {number} uid
* @returns {Promise<string | null>}
*/
getAttentionList: uid => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.vc.bilibili.com/feed/v1/feed/get_attention_list',
queryStringsObj: {
uid: uid
},
hasCookies: true,
success: responseText => {
let res = Base.strToJson(responseText);
if (res.code === 0) {
console.log('[获取关注列表]成功');
resolve(res.data.list.toString());
} else {
console.log(`[获取关注列表]失败\n${responseText}`);
resolve(null);
}
}
});
});
},
/**
* 获取一组动态的信息
* @param {number} UID
* 被查看者的uid
* @param {string} offset
* 此动态偏移量
* 初始为 0
* @returns {Promise<string>}
*/
getOneDynamicInfoByUID: (UID, offset) => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history',
queryStringsObj: {
visitor_uid: GlobalVar.myUID,
host_uid: UID,
offset_dynamic_id: offset,
},
hasCookies: true,
success: responseText => {
/* 鉴别工作交由modifyDynamicRes完成 */
resolve(responseText);
}
});
});
},
/**
* 通过tag名获取tag的id
* @param {string} tagename
* tag名
* @returns {Promise<number | -1>}
* 正确:tag_ID
* 错误:-1
*/
getTagIDByTagName: tagename => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.bilibili.com/x/tag/info',
queryStringsObj: {
tag_name: tagename
},
hasCookies: false,
success: responseText => {
const res = Base.strToJson(responseText);
if (res.code !== 0) {
console.log('获取TagID失败');
resolve(-1);
}
resolve(res.data.tag_id);
}
});
});
},
/**
* 获取tag下的热门动态以及一条最新动态
* @param {number} tagid
* @returns {Promise<string>}
*/
getHotDynamicInfoByTagID: tagid => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_new',
queryStringsObj: {
topic_id: tagid
},
hasCookies: true,
success: responseText => {
resolve(responseText);
}
});
});
},
/**
* 获取tag下的最新动态
* @param {string} tagname
* @param {string} offset
* @returns {Promise<string>}
*/
getOneDynamicInfoByTag: (tagname, offset) => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.vc.bilibili.com/topic_svr/v1/topic_svr/topic_history',
queryStringsObj: {
topic_name: tagname,
offset_dynamic_id: offset
},
hasCookies: true,
success: responseText => {
resolve(responseText);
}
});
});
},
/**
* 获取关注数
* @param {number} uid
* @returns {Promise<number | 0>}
*/
getUserInfo: uid => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.bilibili.com/x/web-interface/card',
queryStringsObj: {
mid: uid,
photo: false
},
hasCookies: true,
success: responseText => {
const res = Base.strToJson(responseText);
if (res.code === 0) {
resolve(res.data.follower);
} else {
console.log('获取关注数出错,可能是访问过频繁');
resolve(0);
}
}
});
});
},
/**
* 获取开奖信息
* @param {string} dyid
* 动态id
* @returns {
Promise<{
ts:number|0;
text:string|'获取开奖信息失败';
item:string|'null';
isMe:string|'未知';
}>
} 开奖时间
*/
getLotteryNotice: dyid => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.vc.bilibili.com/lottery_svr/v1/lottery_svr/lottery_notice',
queryStringsObj: {
dynamic_id: dyid
},
hasCookies: false,
success: responseText => {
const res = Base.strToJson(responseText);
/(?<=_prize_cmt":").*(?=")/.exec();
if (res.code === 0) {
const timestamp10 = res.data.lottery_time,
timestamp13 = timestamp10 * 1000,
time = new Date(timestamp13);
const remain = (() => {
const timestr = ((timestamp13 - Date.now()) / 86400000).toString(),
timearr = timestr.replace(/(\d+)\.(\d+)/, "$1,0.$2").split(',');
const text = timearr[0][0] === '-' ? `开奖时间已过${timearr[0].substring(1)}天余${parseInt(timearr[1] * 24)}小时` : `还有${timearr[0]}天余${parseInt(timearr[1] * 24)}小时`;
return text;
})();
let isMeB = (new RegExp(GlobalVar.myUID)).test(responseText);
const isMe = isMeB ? '中奖了!!!' : '未中奖';
const iteminfo = res.data.first_prize_cmt || '' + ' ' + res.data.second_prize_cmt || '' + ' ' + res.data.third_prize_cmt || '';
resolve({
ts: timestamp10,
text: `开奖时间: ${time.toLocaleString()} ${remain}`,
item: iteminfo,
isMe: isMe
});
} else {
console.log(`获取开奖信息失败\n${responseText}`);
resolve({
ts: 0,
text: '获取开奖信息失败',
item: 'null',
isMe: '未知'
});
}
}
});
});
},
/**
* 之前不检查是否重复关注
* 自动关注
* 并转移分组
* @param {Number} uid
* 被关注者的UID
* @returns {Promise<1|0>}
*/
autoAttention: uid => {
return new Promise((resolve) => {
Ajax.post({
url: 'https://api.bilibili.com/x/relation/modify',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
fid: uid,
act: 1,
re_src: 11,
csrf: GlobalVar.csrf
},
success: responseText => {
/* 重复关注code also equal 0 */
if (/^{"code":0/.test(responseText)) {
console.log('[自动关注]关注+1');
resolve(1);
} else {
console.log(`[自动关注]失败\n${responseText}`);
resolve(0);
}
}
});
});
},
/**
* 移动分区
* @param {number} uid
* @param {number} tagid 关注分区的ID
* @returns {Promise<1|0>}
*/
movePartition: (uid, tagid) => {
return new Promise((resolve) => {
Ajax.post({
url: 'https://api.bilibili.com/x/relation/tags/addUsers',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
fids: uid,
tagids: tagid,
csrf: GlobalVar.csrf
},
success: responseText => {
/* 重复移动code also equal 0 */
if (/^{"code":0/.test(responseText)) {
console.log('[移动分区]up主分区移动成功');
resolve(1);
} else {
console.log(`[移动分区]up主分区移动失败\n${responseText}`);
resolve(0);
}
}
});
});
},
/**
* 取消关注
* @param {number} uid
* @returns {void}
*/
cancelAttention: uid => {
Ajax.post({
url: 'https://api.bilibili.com/x/relation/modify',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
fid: `${uid}`,
act: 2,
re_src: 11,
jsonp: 'jsonp',
csrf: GlobalVar.csrf
},
success: responseText => {
const res = Base.strToJson(responseText);
if (res.code === 0) {
console.log('[自动取关]取关成功');
} else {
console.log(`[自动取关]取关失败\n${responseText}`);
}
}
});
},
/**
* 动态自动点赞
* @param {string} dyid
* @returns {Promise<1|0>}
*/
autolike: dyid => {
return new Promise((resolve) => {
Ajax.post({
url: 'https://api.vc.bilibili.com/dynamic_like/v1/dynamic_like/thumb',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
uid: GlobalVar.myUID,
dynamic_id: dyid,
up: 1,
csrf: GlobalVar.csrf
},
success: responseText => {
if (/^{"code":0/.test(responseText)) {
console.log('[自动点赞]点赞成功');
resolve(1);
} else {
console.log(`[转发动态]点赞失败\n${responseText}`);
resolve(0);
}
}
});
});
},
/**
* 转发前因查看是否重复转发
* 自动转发
* @param {Number} uid
* 自己的UID
* @param {string} dyid
* 动态的ID
* @returns {Promise<1|0>}
*/
autoRelay: (uid, dyid) => {
return new Promise((resolve) => {
Ajax.post({
url: 'https://api.vc.bilibili.com/dynamic_repost/v1/dynamic_repost/repost',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
uid: `${uid}`,
dynamic_id: dyid,
content: Base.getRandomStr(config.relay),
extension: '{"emoji_type":1}',
csrf: GlobalVar.csrf
},
success: responseText => {
if (/^{"code":0/.test(responseText)) {
console.log('[转发动态]成功转发一条动态');
resolve(1);
} else {
console.log(`[转发动态]转发动态失败\n${responseText}`);
resolve(0);
}
}
});
});
},
/**
* 移除动态
* @param {string} dyid
* @returns {void}
*/
rmDynamic: dyid => {
Ajax.post({
url: 'https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/rm_dynamic',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
dynamic_id: dyid,
csrf: GlobalVar.csrf
},
success: responseText => {
if (/^{"code":0/.test(responseText)) {
console.log('[删除动态]成功删除一条动态');
} else {
console.log(`[删除动态]删除动态失败\n${responseText}`);
}
}
});
},
/**
* 发送评论
* @param {string} rid
* cid_str
* @param {string} msg
* @param {number} type
* 1(视频)
* 11(有图)
* 17(无图)
* @returns {Promise<1|0>}
*/
sendChat: (rid, msg, type, show = true) => {
return new Promise((resolve) => {
Ajax.post({
url: 'https://api.bilibili.com/x/v2/reply/add',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
oid: rid,
type: type,
message: msg,
csrf: GlobalVar.csrf
},
success: responseText => {
if (/^{"code":0/.test(responseText)) {
show ? console.log('[自动评论]评论成功') : void 0;
resolve(1);
} else {
show ? console.log('[自动评论]评论失败') : void 0;
resolve(0);
}
}
});
});
},
/**
* 检查分区
* 不存在指定分区时创建
* 获取到tagid添加为对象的属性
* @returns {Promise<number>}
*/
checkMyPartition: () => {
return new Promise((resolve) => {
Ajax.get({
url: 'https://api.bilibili.com/x/relation/tags',
hasCookies: true,
success: responseText => {
if (!/此处存放因抽奖临时关注的up/.test(responseText)) {
/* 如果不存在就新建一个 */
Ajax.post({
url: 'https://api.bilibili.com/x/relation/tag/create',
hasCookies: true,
dataType: 'application/x-www-form-urlencoded',
data: {
tag: '此处存放因抽奖临时关注的up',
csrf: GlobalVar.csrf
},
success: responseText => {
let obj = Base.strToJson(responseText);
if (obj.code === 0) {
console.log('[新建分区]分区新建成功');
let tagid = obj.data.tagid; /* 获取tagid */
resolve(tagid);
}
}
});
} else {
/* 此处可能会出现问题 */
let tagid = /[0-9]*(?=,"name":"此处存放因抽奖临时关注的up")/.exec(responseText)[0]; /* 获取tagid */
resolve(Number(tagid));
}
}
});
});
},
};
module.exports = BiliAPI;