Commit 466402a2 authored by nanahira's avatar nanahira

first

parent fae2bd45
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const posthtml_parser_1 = __importDefault(require("posthtml-parser"));
const user_1 = require("./user");
class Tx3Fetcher {
async fetchRole(id) {
//try {
const content = (await axios_1.default.get(`http://bang.tx3.163.com/bang/role/${id}`, {
responseType: "document"
})).data;
const parsedContent = posthtml_parser_1.default(content);
return new user_1.User(content);
/*} catch(e) {
console.error(`Errored fetching role data from ${id}: ${e.toString()}`);
return null;
}*/
}
}
async function main() {
const fetcher = new Tx3Fetcher();
console.log(JSON.stringify(await fetcher.fetchRole("28_20588"), null, 2));
}
main();
//# sourceMappingURL=fetcher.js.map
\ No newline at end of file
{"version":3,"file":"fetcher.js","sourceRoot":"","sources":["../fetcher.ts"],"names":[],"mappings":";;;;;AAAA,kDAA0B;AAE1B,sEAAwC;AACxC,iCAA8B;AAE9B,MAAM,UAAU;IACf,KAAK,CAAC,SAAS,CAAC,EAAU;QACzB,QAAQ;QACP,MAAM,OAAO,GAAG,CAAC,MAAM,eAAK,CAAC,GAAG,CAAC,qCAAqC,EAAE,EAAE,EAAE;YAC3E,YAAY,EAAE,UAAU;SACxB,CAAC,CAAC,CAAC,IAAI,CAAC;QACT,MAAM,aAAa,GAAG,yBAAS,CAAC,OAAO,CAAC,CAAC;QACzC,OAAO,IAAI,WAAI,CAAC,OAAO,CAAC,CAAC;QAC1B;;;WAGG;IACJ,CAAC;CACD;AAED,KAAK,UAAU,IAAI;IAClB,MAAM,OAAO,GAAG,IAAI,UAAU,EAAE,CAAC;IACjC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;AAC3E,CAAC;AACD,IAAI,EAAE,CAAC"}
\ No newline at end of file
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.User = void 0;
const posthtml_parser_1 = __importDefault(require("posthtml-parser"));
const underscore_1 = __importDefault(require("underscore"));
function getDepthOfTree(tree, indexList) {
if (indexList.length) {
const _indexList = underscore_1.default.clone(indexList);
const index = _indexList.splice(0, 1)[0];
const node = tree[index];
if (typeof (node) === "string" || !node.content) {
return [node];
}
return getDepthOfTree(node.content, _indexList);
}
else {
return tree;
}
}
function findNodeIndex(baseTree, condition, offset) {
let queue = [offset];
while (queue.length) {
const indexList = queue.splice(0, 1)[0];
const tree = getDepthOfTree(baseTree, indexList);
for (let i = 0; i < tree.length; ++i) {
const node = tree[i];
const newList = indexList.concat([i]);
if (condition(node)) {
console.log(newList);
return newList;
}
else if (typeof (node) !== "string") {
queue.push(newList);
}
}
}
return null;
}
function findNodeIndexByContent(baseTree, label, offset) {
return findNodeIndex(baseTree, (node) => {
return node === label;
}, offset);
}
function findNodeIndexByAttribute(baseTree, key, value, offset) {
return findNodeIndex(baseTree, (node) => {
return typeof (node) !== "string" && node.attrs && node.attrs[key] === value;
}, offset);
}
function findNodeIndexByTag(baseTree, tag, offset) {
return findNodeIndex(baseTree, (node) => {
return typeof (node) !== "string" && node.tag === tag;
}, offset);
}
class User {
constructor(content) {
this.content = posthtml_parser_1.default(content);
this.parse();
this.content = null;
}
parseNameAndLevel() {
let namePos = findNodeIndexByAttribute(this.content, "class", "sTitle", []);
this.name = getDepthOfTree(this.content, namePos.concat([0]))[0];
}
parseBasicAttributes() {
let hpStringPos = findNodeIndexByContent(this.content, "", []);
hpStringPos[hpStringPos.length - 2] += 2;
const datas = [];
for (let i = 0; i < 8; ++i) {
const rawData = getDepthOfTree(this.content, hpStringPos)[0];
console.log(rawData);
datas.push(parseInt(rawData.trim()));
hpStringPos[hpStringPos.length - 2] += 4;
}
this.hp = datas[0];
this.mp = datas[1];
this.li = datas[2];
this.ti = datas[3];
this.min = datas[4];
this.ji = datas[5];
this.hun = datas[6];
this.nian = datas[7];
}
parse() {
this.parseBasicAttributes();
}
}
exports.User = User;
//# sourceMappingURL=user.js.map
\ No newline at end of file
{"version":3,"file":"user.js","sourceRoot":"","sources":["../user.ts"],"names":[],"mappings":";;;;;;AAAA,sEAAwC;AACxC,4DAAsC;AAEtC,SAAS,cAAc,CAAC,IAAoB,EAAE,SAAmB;IAChE,IAAI,SAAS,CAAC,MAAM,EAAE;QACrB,MAAM,UAAU,GAAG,oBAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QACtC,MAAM,KAAK,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACzC,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC;QACzB,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;YAChD,OAAO,CAAC,IAAI,CAAC,CAAC;SACd;QACD,OAAO,cAAc,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;KAChD;SAAM;QACN,OAAO,IAAI,CAAC;KACZ;AACF,CAAC;AAED,SAAS,aAAa,CAAC,QAAwB,EAAE,SAA4C,EAAE,MAAgB;IAC9G,IAAI,KAAK,GAAG,CAAC,MAAM,CAAC,CAAC;IACrB,OAAO,KAAK,CAAC,MAAM,EAAE;QACpB,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACxC,MAAM,IAAI,GAAG,cAAc,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;QACjD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE;YACrC,MAAM,IAAI,GAAG,IAAI,CAAC,CAA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nB,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IACtB,CAAC;IACD,KAAK;QACJ,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC7B,CAAC;CAMD;AA3CD,oBA2CC"}
\ No newline at end of file
import axios from "axios";
import _ from "underscore";
import parseHTML from "posthtml-parser";
import { User } from "./user";
class Tx3Fetcher {
async fetchRole(id: string): Promise<User> {
//try {
const content = (await axios.get(`http://bang.tx3.163.com/bang/role/${id}`, {
responseType: "document"
})).data;
const parsedContent = parseHTML(content);
return new User(content);
/*} catch(e) {
console.error(`Errored fetching role data from ${id}: ${e.toString()}`);
return null;
}*/
}
}
async function main() {
const fetcher = new Tx3Fetcher();
console.log(JSON.stringify(await fetcher.fetchRole("28_20588"), null, 2));
}
main();
{
"name": "tx3-bang-reader",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/node": {
"version": "14.0.14",
"resolved": "https://registry.npm.taobao.org/@types/node/download/@types/node-14.0.14.tgz?cache=0&sync_timestamp=1592989417189&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fnode%2Fdownload%2F%40types%2Fnode-14.0.14.tgz",
"integrity": "sha1-JKC1lZ8WrBQa6wxbPNehW3xky84="
},
"@types/underscore": {
"version": "1.10.2",
"resolved": "https://registry.npm.taobao.org/@types/underscore/download/@types/underscore-1.10.2.tgz?cache=0&sync_timestamp=1593310825578&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Funderscore%2Fdownload%2F%40types%2Funderscore-1.10.2.tgz",
"integrity": "sha1-JCYVPgkmRu155Y8VMvQprq+mmk4="
},
"axios": {
"version": "0.19.2",
"resolved": "https://registry.npm.taobao.org/axios/download/axios-0.19.2.tgz",
"integrity": "sha1-PqNsXYgY0NX4qKl6bTa4bNwAyyc=",
"requires": {
"follow-redirects": "1.5.10"
}
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npm.taobao.org/debug/download/debug-3.1.0.tgz",
"integrity": "sha1-W7WgZyYotkFJVmuhaBnmFRjGcmE=",
"requires": {
"ms": "2.0.0"
}
},
"dom-serializer": {
"version": "0.2.2",
"resolved": "https://registry.npm.taobao.org/dom-serializer/download/dom-serializer-0.2.2.tgz",
"integrity": "sha1-GvuB9TNxcXXUeGVd68XjMtn5u1E=",
"requires": {
"domelementtype": "^2.0.1",
"entities": "^2.0.0"
}
},
"domelementtype": {
"version": "2.0.1",
"resolved": "https://registry.npm.taobao.org/domelementtype/download/domelementtype-2.0.1.tgz",
"integrity": "sha1-H4vf6R9aeAYydOgDtL3O326U+U0="
},
"domhandler": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-3.0.0.tgz",
"integrity": "sha512-eKLdI5v9m67kbXQbJSNn1zjh0SDzvzWVWtX+qEI3eMjZw8daH9k8rlj1FZY9memPwjiskQFbe7vHVVJIAqoEhw==",
"requires": {
"domelementtype": "^2.0.1"
}
},
"domutils": {
"version": "2.1.0",
"resolved": "https://registry.npm.taobao.org/domutils/download/domutils-2.1.0.tgz?cache=0&sync_timestamp=1589052559692&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdomutils%2Fdownload%2Fdomutils-2.1.0.tgz",
"integrity": "sha1-et4yAa9DcD/eFUlS46ho60tjXxY=",
"requires": {
"dom-serializer": "^0.2.1",
"domelementtype": "^2.0.1",
"domhandler": "^3.0.0"
}
},
"entities": {
"version": "2.0.3",
"resolved": "https://registry.npm.taobao.org/entities/download/entities-2.0.3.tgz",
"integrity": "sha1-XEh+V0Krk8Fau12iJ1m4WQ7AO38="
},
"follow-redirects": {
"version": "1.5.10",
"resolved": "https://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.5.10.tgz?cache=0&sync_timestamp=1592518405361&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ffollow-redirects%2Fdownload%2Ffollow-redirects-1.5.10.tgz",
"integrity": "sha1-e3qfmuov3/NnhqlP9kPtB/T/Xio=",
"requires": {
"debug": "=3.1.0"
}
},
"htmlparser2": {
"version": "4.1.0",
"resolved": "https://registry.npm.taobao.org/htmlparser2/download/htmlparser2-4.1.0.tgz",
"integrity": "sha1-mk7xYfLkYl6/ffvmwKL1LRilnng=",
"requires": {
"domelementtype": "^2.0.1",
"domhandler": "^3.0.0",
"domutils": "^2.0.0",
"entities": "^2.0.0"
}
},
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npm.taobao.org/inherits/download/inherits-2.0.4.tgz",
"integrity": "sha1-D6LGT5MpF8NDOg3tVTY6rjdBa3w="
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"posthtml-parser": {
"version": "0.4.2",
"resolved": "https://registry.npm.taobao.org/posthtml-parser/download/posthtml-parser-0.4.2.tgz",
"integrity": "sha1-oTK73wzUvBmdNPMi9cFZk4XXxsE=",
"requires": {
"htmlparser2": "^3.9.2"
},
"dependencies": {
"domelementtype": {
"version": "1.3.1",
"resolved": "https://registry.npm.taobao.org/domelementtype/download/domelementtype-1.3.1.tgz",
"integrity": "sha1-0EjESzew0Qp/Kj1f7j9DM9eQSB8="
},
"domhandler": {
"version": "2.4.2",
"resolved": "https://registry.npm.taobao.org/domhandler/download/domhandler-2.4.2.tgz",
"integrity": "sha1-iAUJfpM9ZehVRvcm1g9euItE+AM=",
"requires": {
"domelementtype": "1"
}
},
"domutils": {
"version": "1.7.0",
"resolved": "https://registry.npm.taobao.org/domutils/download/domutils-1.7.0.tgz?cache=0&sync_timestamp=1589052559692&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdomutils%2Fdownload%2Fdomutils-1.7.0.tgz",
"integrity": "sha1-Vuo0HoNOBuZ0ivehyyXaZ+qfjCo=",
"requires": {
"dom-serializer": "0",
"domelementtype": "1"
}
},
"entities": {
"version": "1.1.2",
"resolved": "https://registry.npm.taobao.org/entities/download/entities-1.1.2.tgz",
"integrity": "sha1-vfpzUplmTfr9NFKe1PhSKidf6lY="
},
"htmlparser2": {
"version": "3.10.1",
"resolved": "https://registry.npm.taobao.org/htmlparser2/download/htmlparser2-3.10.1.tgz",
"integrity": "sha1-vWedw/WYl7ajS7EHSchVu1OpOS8=",
"requires": {
"domelementtype": "^1.3.1",
"domhandler": "^2.3.0",
"domutils": "^1.5.1",
"entities": "^1.1.1",
"inherits": "^2.0.1",
"readable-stream": "^3.1.1"
}
}
}
},
"readable-stream": {
"version": "3.6.0",
"resolved": "https://registry.npm.taobao.org/readable-stream/download/readable-stream-3.6.0.tgz?cache=0&sync_timestamp=1581624324274&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Freadable-stream%2Fdownload%2Freadable-stream-3.6.0.tgz",
"integrity": "sha1-M3u9o63AcGvT4CRCaihtS0sskZg=",
"requires": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
}
},
"safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npm.taobao.org/safe-buffer/download/safe-buffer-5.2.1.tgz",
"integrity": "sha1-Hq+fqb2x/dTsdfWPnNtOa3gn7sY="
},
"string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npm.taobao.org/string_decoder/download/string_decoder-1.3.0.tgz",
"integrity": "sha1-QvEUWUpGzxqOMLCoT1bHjD7awh4=",
"requires": {
"safe-buffer": "~5.2.0"
}
},
"typescript": {
"version": "3.9.5",
"resolved": "https://registry.npm.taobao.org/typescript/download/typescript-3.9.5.tgz?cache=0&sync_timestamp=1593500095904&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ftypescript%2Fdownload%2Ftypescript-3.9.5.tgz",
"integrity": "sha1-WG8NujAM3ovlLdGsT34QCcGxPzY="
},
"underscore": {
"version": "1.10.2",
"resolved": "https://registry.npm.taobao.org/underscore/download/underscore-1.10.2.tgz?cache=0&sync_timestamp=1585603748606&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Funderscore%2Fdownload%2Funderscore-1.10.2.tgz",
"integrity": "sha1-c9aqNmjzGI5K2w8ZQ70Sz9fvqq8="
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npm.taobao.org/util-deprecate/download/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
}
}
}
{
"name": "tx3-bang-reader",
"version": "1.0.0",
"description": "Read TX3 bang and parse it",
"main": "build/run.js",
"scripts": {
"build": "./node_modules/.bin/tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/purerosefallen/tx3-bang-reader.git"
},
"keywords": [
"tx3"
],
"author": "Nanahira",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/purerosefallen/tx3-bang-reader/issues"
},
"homepage": "https://github.com/purerosefallen/tx3-bang-reader#readme",
"dependencies": {
"@types/node": "^14.0.14",
"@types/underscore": "^1.10.2",
"axios": "^0.19.2",
"domhandler": "^3.0.0",
"htmlparser2": "^4.1.0",
"posthtml-parser": "^0.4.2",
"typescript": "^3.9.5",
"underscore": "^1.10.2"
}
}
This diff is collapsed.
{
"compilerOptions": {
"outDir": "build",
"module": "commonjs",
"target": "esnext",
"esModuleInterop": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts"
]
}
import parseHTML from "posthtml-parser";
import _, { first } from "underscore";
function getDepthOfTree(tree: parseHTML.Tree, indexList: number[]): parseHTML.Tree {
if (indexList.length) {
const _indexList = _.clone(indexList);
const index = _indexList.splice(0, 1)[0];
const node = tree[index];
if (typeof (node) === "string" || !node.content) {
return [node];
}
return getDepthOfTree(node.content, _indexList);
} else {
return tree;
}
}
function findNodeIndex(baseTree: parseHTML.Tree, condition: (node: parseHTML.Node) => boolean, offset: number[]): number[] {
let queue = [offset];
while (queue.length) {
const indexList = queue.splice(0, 1)[0];
const tree = getDepthOfTree(baseTree, indexList);
for (let i = 0; i < tree.length; ++i) {
const node = tree[i];
const newList = indexList.concat([i]);
if (condition(node)) {
console.log(newList);
return newList;
} else if (typeof (node) !== "string") {
queue.push(newList);
}
}
}
return null;
}
function findNodeIndexByContent(baseTree: parseHTML.Tree, label: string, offset: number[]): number[] {
return findNodeIndex(baseTree, (node) => {
return node === label;
}, offset);
}
function findNodeIndexByAttribute(baseTree: parseHTML.Tree, key: string, value: string, offset: number[]): number[] {
return findNodeIndex(baseTree, (node) => {
return typeof (node) !== "string" && node.attrs && node.attrs[key] === value;
}, offset);
}
function findNodeIndexByTag(baseTree: parseHTML.Tree, tag: string, offset: number[]): number[] {
return findNodeIndex(baseTree, (node) => {
return typeof (node) !== "string" && node.tag === tag;
}, offset);
}
export class User {
content: parseHTML.Tree;
name: string;
hp: number;
mp: number;
li: number;
ti: number;
min: number;
ji: number;
hun: number;
nian: number;
parseNameAndLevel() {
let namePos = findNodeIndexByAttribute(this.content, "class", "sTitle", []);
this.name = getDepthOfTree(this.content, namePos.concat([0]))[0] as string;
}
parseBasicAttributes() {
let hpStringPos = findNodeIndexByContent(this.content, "", []);
hpStringPos[hpStringPos.length - 2] += 2;
const datas: number[] = []
for (let i = 0; i < 8; ++i) {
const rawData = getDepthOfTree(this.content, hpStringPos)[0] as string;
console.log(rawData);
datas.push(parseInt(rawData.trim()));
hpStringPos[hpStringPos.length - 2] += 4;
}
this.hp = datas[0];
this.mp = datas[1];
this.li = datas[2];
this.ti = datas[3];
this.min = datas[4];
this.ji = datas[5];
this.hun = datas[6];
this.nian = datas[7];
}
parse() {
this.parseBasicAttributes();
}
constructor(content: string) {
this.content = parseHTML(content);
this.parse();
this.content = null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment