steam-family-bot-core
Version:
一个用于新版 Steam 家庭的库存监控 Bot 插件
152 lines • 5.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetReqResult = exports.ISteamFamilyAPI = void 0;
const lodash_1 = __importDefault(require("lodash"));
__exportStar(require("./types"), exports);
const sleep = async (millSec = 300) => {
return new Promise((resolve, reject) => setTimeout(resolve, millSec));
};
class ISteamFamilyAPI {
token;
steamId;
// for wishlist larger than 50 items, same request may give different response item in a short time.
// it's really an annoyed bug. but steam didn't fix it for a long time.
// check this: https://steamcommunity.com/sharedfiles/filedetails/?id=1746978201
getSteamWishesByPage = async (id, page) => {
const url = `https://store.steampowered.com/wishlist/profiles/${id}/wishlistdata/?p=${page}&v=1`;
try {
const res = await fetch(url).then((res) => res.json());
// filter keys equal success
if (res['success']) {
return [];
}
return res;
}
catch (e) {
console.log(e);
console.log(url);
return null;
}
};
retry = async (func, times = 3) => {
let time = 0;
try {
let res = null;
while (!res && time < times) {
time++;
await sleep();
res = await func();
}
return res;
}
catch (e) {
return null;
}
};
getOnePlayersSteamWishes = async (id) => {
let page = 0;
let hasMore = true;
let appIds = [];
const appMaps = new Map();
while (hasMore) {
const res = await this.retry(() => this.getSteamWishesByPage(id, page));
if (res) {
if (res instanceof Array) {
hasMore = false;
continue;
}
Object.keys(res).forEach((key) => {
appMaps.set(key, res[key]);
});
appIds = appIds.concat(Object.keys(res));
page++;
}
else {
console.log(`unexpect error, ${id}, ${page}`);
hasMore = false;
}
}
return appIds.map((appId) => ({
wisher: id,
item: appMaps.get(appId),
appId: appId,
}));
};
async getSteamWishesByPlayerIds(playerIds) {
const wishesByPlayer = (await Promise.all(playerIds.map((it) => this.getOnePlayersSteamWishes(it))))
.flatMap((it) => it)
.filter((app) => app.appId !== 'success');
const wishes = wishesByPlayer.flatMap((wish) => wish);
const groupedWishes = lodash_1.default.groupBy(wishes, 'appId');
const appIds = Object.keys(groupedWishes);
console.log(`total wishes count ${wishes.length}`);
const finalWishes = appIds.map((appId) => {
const items = groupedWishes[appId];
return {
wishers: items.map((item) => item.wisher),
appId: appId,
itemInfo: items[0].item,
};
});
return {
data: finalWishes,
};
}
}
exports.ISteamFamilyAPI = ISteamFamilyAPI;
var ReqResultStatus;
(function (ReqResultStatus) {
ReqResultStatus[ReqResultStatus["Success"] = 0] = "Success";
ReqResultStatus[ReqResultStatus["NotMatch"] = 1] = "NotMatch";
ReqResultStatus[ReqResultStatus["NetworkError"] = 2] = "NetworkError";
})(ReqResultStatus || (ReqResultStatus = {}));
class NetReqResult {
data;
status;
msg;
static failed(reason) {
return new NetReqResult(null, ReqResultStatus.NetworkError, reason);
}
static success(data) {
return new NetReqResult(data, ReqResultStatus.Success, 'ok');
}
constructor(data, status, message) {
this.data = data;
this.status = status;
this.msg = message;
}
successOr(data) {
if (this.isSuccess()) {
return this.data;
}
return data;
}
isSuccess() {
return this.status === ReqResultStatus.Success;
}
isNetworkError() {
return this.status === ReqResultStatus.NetworkError;
}
isNotMatch() {
return this.status === ReqResultStatus.NotMatch;
}
}
exports.NetReqResult = NetReqResult;
//# sourceMappingURL=index.js.map