@otohime-site/parser 24.1.1 → 26.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  ### The Otohime Parser
2
2
 
3
- The parser will take the `HTMLDocument` or text (parsing with `DOMParser`) and return an object for each function.
3
+ The parser will take the `Document` or text (parsing with `DOMParser`) and return an object for each function.
4
4
 
5
5
  You need polyfill for `DOMParser` if you are not under browser environment.
6
6
 
7
7
  #### Implemented parser
8
8
 
9
- ##### maimai DX Int'l Version
9
+ ##### maimai
10
10
 
11
- It is believed that it can be also used to parse from Japanese version of maimai DX Splash,
12
- but it is currently untested so use it carefully.
11
+ We do our best to support both the Japanese and International versions of maimai DX NET.
12
+ It is currently tested up to maimai DX MAGiCAL.
13
13
 
14
14
  ```js
15
15
  // Providing text from https://maimaidx-eng.com/maimai-mobile/home/...
@@ -18,5 +18,21 @@ const player = parsePlayer(playerText)
18
18
 
19
19
  // Providing text from https://maimaidx-eng.com/maimai-mobile/record/musicGenre/search/?genre=99&diff=3...
20
20
  import { parseScores } from "@otohime-site/parser/dx_intl"
21
+ // Played charts only, typed as ScoresParseEntryWithScore[]
21
22
  const scores = parseScores(scoresText)
23
+ // Also include unplayed charts, typed as ScoresParseEntry[]
24
+ const allScores = parseScores(scoresText, { includeUnplayed: true })
22
25
  ```
26
+
27
+ All functions and result types are exported from `@otohime-site/parser/dx_intl`:
28
+
29
+ ```ts
30
+ import type {
31
+ PlayerParseResult,
32
+ ScoresParseEntry,
33
+ ScoresParseEntryWithScore,
34
+ } from "@otohime-site/parser/dx_intl"
35
+ ```
36
+
37
+ Played score entries include `dx_score` and `max_dx_score`. `dx_score` can be 0
38
+ for charts that were only played before maimai DX.
@@ -1,2 +1,4 @@
1
- export { default as parsePlayer } from "./player.js";
2
- export { default as parseScores } from "./scores.js";
1
+ export { parsePlayer } from "./player.js";
2
+ export type { PlayerParseResult, PlayerParseResultBase, PlayerParseResultLegacy, PlayerParseResultNew, } from "./player.js";
3
+ export { parseScores } from "./scores.js";
4
+ export type { ParseScoresOptions, ScoresParseEntry, ScoresParseEntryWithoutScore, ScoresParseEntryWithScore, } from "./scores.js";
package/dx_intl/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { default as parsePlayer } from "./player.js";
2
- export { default as parseScores } from "./scores.js";
1
+ export { parsePlayer } from "./player.js";
2
+ export { parseScores } from "./scores.js";
@@ -1,4 +1,4 @@
1
- interface PlayerParseResultBase {
1
+ export interface PlayerParseResultBase {
2
2
  card_name: string;
3
3
  rating: number;
4
4
  rating_legacy: boolean;
@@ -12,5 +12,5 @@ export interface PlayerParseResultNew extends PlayerParseResultBase {
12
12
  course_rank: number;
13
13
  class_rank: number;
14
14
  }
15
- declare const parsePlayer: (content: string | HTMLDocument) => PlayerParseResultLegacy | PlayerParseResultNew;
16
- export default parsePlayer;
15
+ export type PlayerParseResult = PlayerParseResultLegacy | PlayerParseResultNew;
16
+ export declare function parsePlayer(content: string | Document): PlayerParseResult;
package/dx_intl/player.js CHANGED
@@ -7,28 +7,38 @@ const isLegacyRating = (rating, ref) => (ref === "green" && rating < 3000) ||
7
7
  (ref === "silver" && rating < 13000) ||
8
8
  (ref === "gold" && rating < 14000) ||
9
9
  (ref === "rainbow" && rating < 15000);
10
- const parsePlayer = (content) => {
11
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
10
+ export function parsePlayer(content) {
12
11
  const document = typeof content === "string"
13
12
  ? new DOMParser().parseFromString(content, "text/html")
14
13
  : content;
15
- const cardName = (_b = (_a = document.querySelector(".name_block")) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : "";
14
+ const cardName = document.querySelector(".name_block")?.textContent ?? "";
16
15
  // On marquee mode, the title will use another HTML structure...
17
- const title = (_d = (_c = document.querySelector(".trophy_inner_block > span, .trophy_inner_block > div > div")) === null || _c === void 0 ? void 0 : _c.textContent) !== null && _d !== void 0 ? _d : "".trim();
18
- const trophy = ((_f = (_e = document
19
- .querySelector(".trophy_block")) === null || _e === void 0 ? void 0 : _e.className.match(/Normal|Bronze|Silver|Gold|Rainbow/)) !== null && _f !== void 0 ? _f : [
16
+ // Do not trim: a gold title consists of full-width spaces (U+3000), the
17
+ // officially listed name of the song by x0o0x_ known as "Kisaragi Station"
18
+ const title = document.querySelector(".trophy_inner_block > span, .trophy_inner_block > div > div")?.textContent ?? "";
19
+ const trophy = (document
20
+ .querySelector(".trophy_block")
21
+ ?.className.match(/Normal|Bronze|Silver|Gold|Rainbow/) ?? [
20
22
  "",
21
23
  ])[0].toLowerCase();
22
24
  const ratingBlock = document.querySelector(".rating_block");
23
- const rawRating = (_g = ratingBlock === null || ratingBlock === void 0 ? void 0 : ratingBlock.textContent) !== null && _g !== void 0 ? _g : "";
24
- const ratingImageRef = ((_k = (_j = (_h = document
25
- .querySelector('img[src *= "/img/rating_base_"]')) === null || _h === void 0 ? void 0 : _h.getAttribute("src")) === null || _j === void 0 ? void 0 : _j.match(/_([a-z]+)\.png/)) !== null && _k !== void 0 ? _k : ["", ""])[1];
26
- const rawCourseRank = ((_o = (_m = (_l = document
27
- .querySelector('img[src *= "/img/course/"]')) === null || _l === void 0 ? void 0 : _l.getAttribute("src")) === null || _m === void 0 ? void 0 : _m.match(/course_rank_([0-9]{2})/)) !== null && _o !== void 0 ? _o : ["", ""])[1];
28
- const rawClassRank = ((_r = (_q = (_p = document
29
- .querySelector('img[src *= "/img/class/"]')) === null || _p === void 0 ? void 0 : _p.getAttribute("src")) === null || _q === void 0 ? void 0 : _q.match(/_([0-9]{2})/)) !== null && _r !== void 0 ? _r : ["", ""])[1];
30
- const rawGrade = ((_u = (_t = (_s = document
31
- .querySelector('img[src *= "/img/grade_"]')) === null || _s === void 0 ? void 0 : _s.getAttribute("src")) === null || _t === void 0 ? void 0 : _t.match(/grade_([0-9]{2})/)) !== null && _u !== void 0 ? _u : ["", ""])[1];
25
+ const rawRating = ratingBlock?.textContent ?? "";
26
+ const ratingImageRef = (document
27
+ .querySelector('img[src *= "/img/rating_base_"]')
28
+ ?.getAttribute("src")
29
+ ?.match(/_([a-z]+)\.png/) ?? ["", ""])[1];
30
+ const rawCourseRank = (document
31
+ .querySelector('img[src *= "/img/course/"]')
32
+ ?.getAttribute("src")
33
+ ?.match(/course_rank_([0-9]{2})/) ?? ["", ""])[1];
34
+ const rawClassRank = (document
35
+ .querySelector('img[src *= "/img/class/"]')
36
+ ?.getAttribute("src")
37
+ ?.match(/_([0-9]{2})/) ?? ["", ""])[1];
38
+ const rawGrade = (document
39
+ .querySelector('img[src *= "/img/grade_"]')
40
+ ?.getAttribute("src")
41
+ ?.match(/grade_([0-9]{2})/) ?? ["", ""])[1];
32
42
  assertNonEmpty(cardName, "card_name");
33
43
  assertNonEmpty(title, "title");
34
44
  assertNonEmpty(trophy, "trophy");
@@ -78,5 +88,4 @@ const parsePlayer = (content) => {
78
88
  course_rank: courseRank,
79
89
  class_rank: classRank,
80
90
  };
81
- };
82
- export default parsePlayer;
91
+ }
@@ -8,9 +8,25 @@ export interface ScoresParseEntryWithoutScore {
8
8
  }
9
9
  export interface ScoresParseEntryWithScore extends ScoresParseEntryWithoutScore {
10
10
  score: number;
11
+ /** Can be 0 if the chart was only played before maimai DX */
12
+ dx_score: number;
13
+ /** Always a multiple of 3 (3 * total notes) */
14
+ max_dx_score: number;
11
15
  combo_flag: "" | "fc" | "fc+" | "ap" | "ap+";
12
16
  sync_flag: "" | "s" | "fs" | "fs+" | "fdx" | "fdx+";
13
17
  }
14
18
  export type ScoresParseEntry = ScoresParseEntryWithoutScore | ScoresParseEntryWithScore;
15
- declare const parseScores: (content: string | Document, categoryTo?: number, withNoScore?: boolean) => ScoresParseEntry[];
16
- export default parseScores;
19
+ export interface ParseScoresOptions {
20
+ /** Number of categories (genres) expected on the page, default 6 */
21
+ categoryCount?: number;
22
+ /** Also return charts that are not played yet, default false */
23
+ includeUnplayed?: boolean;
24
+ }
25
+ export declare function parseScores(content: string | Document, options?: ParseScoresOptions & {
26
+ includeUnplayed?: false;
27
+ }): ScoresParseEntryWithScore[];
28
+ export declare function parseScores(content: string | Document, options: ParseScoresOptions & {
29
+ includeUnplayed: true;
30
+ }): ScoresParseEntry[];
31
+ export declare function parseScores(content: string | Document, options?: ParseScoresOptions): ScoresParseEntry[];
32
+ export {};
package/dx_intl/scores.js CHANGED
@@ -24,7 +24,7 @@ const LEVELS = [
24
24
  "14+",
25
25
  "15",
26
26
  ];
27
- const parseScores = (content, categoryTo = 6, withNoScore = false) => {
27
+ export function parseScores(content, { categoryCount = 6, includeUnplayed = false } = {}) {
28
28
  const document = typeof content === "string"
29
29
  ? new DOMParser().parseFromString(content, "text/html")
30
30
  : content;
@@ -37,18 +37,22 @@ const parseScores = (content, categoryTo = 6, withNoScore = false) => {
37
37
  throw new Error("Cannot read scores!");
38
38
  }
39
39
  const { result } = entries.reduce((prev, curr) => {
40
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
41
40
  if (curr.className.includes("screw_block")) {
42
41
  // XXX: This assume none of the category will be skipped
43
- return Object.assign(Object.assign({}, prev), { currentCategory: prev.currentCategory + 1 });
42
+ return {
43
+ ...prev,
44
+ currentCategory: prev.currentCategory + 1,
45
+ };
44
46
  }
45
- const rawMusicDifficulty = ((_e = (_b = (_a = curr.className) === null || _a === void 0 ? void 0 : _a.match(/_(basic|advanced|expert|master|remaster)/)) !== null && _b !== void 0 ? _b : (_d = (_c = curr
46
- .querySelector("div")) === null || _c === void 0 ? void 0 : _c.className) === null || _d === void 0 ? void 0 : _d.match(/_(basic|advanced|expert|master|remaster)/)) !== null && _e !== void 0 ? _e : [
47
+ const rawMusicDifficulty = (curr.className?.match(/_(basic|advanced|expert|master|remaster)/) ??
48
+ curr
49
+ .querySelector("div")
50
+ ?.className?.match(/_(basic|advanced|expert|master|remaster)/) ?? [
47
51
  "",
48
52
  "",
49
53
  ])[1];
50
54
  const category = prev.currentCategory;
51
- const title = (_g = (_f = curr.querySelector(".music_name_block")) === null || _f === void 0 ? void 0 : _f.textContent) !== null && _g !== void 0 ? _g : "";
55
+ const title = curr.querySelector(".music_name_block")?.textContent ?? "";
52
56
  const difficulty = [
53
57
  "basic",
54
58
  "advanced",
@@ -56,11 +60,12 @@ const parseScores = (content, categoryTo = 6, withNoScore = false) => {
56
60
  "master",
57
61
  "remaster",
58
62
  ].indexOf(rawMusicDifficulty);
59
- const rawLevel = (_j = (_h = curr.querySelector(".music_lv_block")) === null || _h === void 0 ? void 0 : _h.textContent) !== null && _j !== void 0 ? _j : "";
63
+ const rawLevel = curr.querySelector(".music_lv_block")?.textContent ?? "";
60
64
  // If only one type: determine by .music_kind_icon (standard.png vs dx.png)
61
65
  // Two types: determine by id (sta_xx vs dx_xx)
62
- const rawDeluxe = (_l = (_k = curr.querySelector(".music_kind_icon")) === null || _k === void 0 ? void 0 : _k.getAttribute("src")) !== null && _l !== void 0 ? _l : ((_m = curr.id.match(/sta|dx/)) !== null && _m !== void 0 ? _m : [""])[0];
63
- assertBetween(category, 1, categoryTo, "category");
66
+ const rawDeluxe = curr.querySelector(".music_kind_icon")?.getAttribute("src") ??
67
+ (curr.id.match(/sta|dx/) ?? [""])[0];
68
+ assertBetween(category, 1, categoryCount, "category");
64
69
  assertNonEmpty(title, "title");
65
70
  assertBetween(difficulty, 0, 4, "difficulty");
66
71
  assertNonEmpty(rawDeluxe, "deluxe");
@@ -71,11 +76,21 @@ const parseScores = (content, categoryTo = 6, withNoScore = false) => {
71
76
  throw new Error("Level is not matched!");
72
77
  }
73
78
  const level = LEVELS[levelIndex];
74
- const rawScore = ((_p = (_o = curr.querySelector(".music_score_block")) === null || _o === void 0 ? void 0 : _o.textContent) !== null && _p !== void 0 ? _p : "").replace("%", "");
75
- if (rawScore.length === 0) {
79
+ // First block: score (e.g. 100.5000%), second block: DX score (e.g. 1,343 / 1,479)
80
+ // Both blocks are absent if the chart is not played yet
81
+ const scoreBlocks = [
82
+ ...curr.querySelectorAll(".music_score_block").values(),
83
+ ];
84
+ const rawScore = (scoreBlocks[0]?.textContent ?? "")
85
+ .replace("%", "")
86
+ .trim();
87
+ const rawDxScore = (scoreBlocks[1]?.textContent ?? "").trim();
88
+ if (rawScore.length === 0 && rawDxScore.length === 0) {
76
89
  // Skip if it is not played yet
77
- return withNoScore
78
- ? Object.assign(Object.assign({}, prev), { result: [
90
+ return includeUnplayed
91
+ ? {
92
+ ...prev,
93
+ result: [
79
94
  ...prev.result,
80
95
  {
81
96
  category,
@@ -84,61 +99,82 @@ const parseScores = (content, categoryTo = 6, withNoScore = false) => {
84
99
  difficulty,
85
100
  level,
86
101
  },
87
- ] }) : Object.assign({}, prev);
102
+ ],
103
+ }
104
+ : { ...prev };
88
105
  }
89
106
  const score = parseFloat(rawScore);
90
107
  assertBetween(score, 0, 101, "score");
108
+ const dxScoreMatches = rawDxScore
109
+ .replace(/,/g, "")
110
+ .match(/^(\d+)\s*\/\s*(\d+)$/);
111
+ if (dxScoreMatches === null) {
112
+ throw new Error("Cannot get the value of dx_score");
113
+ }
114
+ const dxScore = parseInt(dxScoreMatches[1], 10);
115
+ const maxDxScore = parseInt(dxScoreMatches[2], 10);
116
+ if (maxDxScore <= 0 || maxDxScore % 3 !== 0) {
117
+ throw new Error(`max_dx_score should be a positive multiple of 3, received: ${maxDxScore}`);
118
+ }
119
+ // dx_score can be 0 if the chart was only played before maimai DX
120
+ assertBetween(dxScore, 0, maxDxScore, "dx_score");
91
121
  const flagImages = [...curr.querySelectorAll("img.f_r").values()];
92
122
  const flags = flagImages.reduce((prevFlags, currFlagImg) => {
93
- var _a, _b;
94
- const comboMatches = ((_a = currFlagImg.getAttribute("src")) !== null && _a !== void 0 ? _a : "").match(/(fc|fcp|ap|app)\.png/);
123
+ const comboMatches = (currFlagImg.getAttribute("src") ?? "").match(/(fc|fcp|ap|app)\.png/);
95
124
  if (comboMatches !== null) {
96
125
  switch (comboMatches[1]) {
97
126
  case "fc":
98
- return Object.assign(Object.assign({}, prevFlags), { combo_flag: "fc" });
127
+ return { ...prevFlags, combo_flag: "fc" };
99
128
  case "fcp":
100
- return Object.assign(Object.assign({}, prevFlags), { combo_flag: "fc+" });
129
+ return { ...prevFlags, combo_flag: "fc+" };
101
130
  case "ap":
102
- return Object.assign(Object.assign({}, prevFlags), { combo_flag: "ap" });
131
+ return { ...prevFlags, combo_flag: "ap" };
103
132
  case "app":
104
- return Object.assign(Object.assign({}, prevFlags), { combo_flag: "ap+" });
133
+ return { ...prevFlags, combo_flag: "ap+" };
105
134
  }
106
135
  }
107
- const syncMatches = ((_b = currFlagImg.getAttribute("src")) !== null && _b !== void 0 ? _b : "").match(/(sync|fs|fsp|fsd|fsdp|fdx|fdxp)\.png/);
136
+ const syncMatches = (currFlagImg.getAttribute("src") ?? "").match(/(sync|fs|fsp|fsd|fsdp|fdx|fdxp)\.png/);
108
137
  if (syncMatches !== null) {
109
138
  switch (syncMatches[1]) {
110
139
  case "sync":
111
- return Object.assign(Object.assign({}, prevFlags), { sync_flag: "s" });
140
+ return { ...prevFlags, sync_flag: "s" };
112
141
  case "fs":
113
- return Object.assign(Object.assign({}, prevFlags), { sync_flag: "fs" });
142
+ return { ...prevFlags, sync_flag: "fs" };
114
143
  case "fsp":
115
- return Object.assign(Object.assign({}, prevFlags), { sync_flag: "fs+" });
144
+ return { ...prevFlags, sync_flag: "fs+" };
116
145
  case "fsd":
117
146
  case "fdx":
118
- return Object.assign(Object.assign({}, prevFlags), { sync_flag: "fdx" });
147
+ return { ...prevFlags, sync_flag: "fdx" };
119
148
  case "fsdp":
120
149
  case "fdxp":
121
- return Object.assign(Object.assign({}, prevFlags), { sync_flag: "fdx+" });
150
+ return { ...prevFlags, sync_flag: "fdx+" };
122
151
  }
123
152
  }
124
- return Object.assign({}, prevFlags);
153
+ return { ...prevFlags };
125
154
  }, {
126
155
  combo_flag: "",
127
156
  sync_flag: "",
128
157
  });
129
- return Object.assign(Object.assign({}, prev), { result: [
158
+ return {
159
+ ...prev,
160
+ result: [
130
161
  ...prev.result,
131
- Object.assign({ category,
162
+ {
163
+ category,
132
164
  title,
133
165
  deluxe,
134
166
  difficulty,
135
167
  level,
136
- score }, flags),
137
- ] });
168
+ score,
169
+ dx_score: dxScore,
170
+ max_dx_score: maxDxScore,
171
+ ...flags,
172
+ },
173
+ ],
174
+ };
138
175
  }, {
139
176
  result: [],
140
177
  currentCategory: 0,
141
178
  });
142
179
  return result;
143
- };
144
- export default parseScores;
180
+ }
package/package.json CHANGED
@@ -1,38 +1,39 @@
1
1
  {
2
2
  "name": "@otohime-site/parser",
3
- "version": "24.1.1",
3
+ "version": "26.11.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
- "types": "index.d.ts",
7
6
  "homepage": "https://github.com/otohime-site/parser#readme",
8
- "exports": {
9
- ".": "./index.js",
10
- "./dx_intl": "./dx_intl/index.js",
11
- "./dx_intl/*": "./dx_intl/*.js"
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "github:otohime-site/parser"
12
10
  },
13
- "scripts": {
14
- "lint": "eslint",
15
- "test": "jest --coverage",
16
- "build": "rm -rf dist/ && tsc && cp README.md dist/ && jq 'del(.scripts.prepare)' package.json > dist/package.json"
11
+ "sideEffects": false,
12
+ "exports": {
13
+ "./dx_intl": "./dx_intl/index.js"
17
14
  },
18
15
  "devDependencies": {
19
- "@typescript-eslint/eslint-plugin": "^6.19.0",
20
- "@typescript-eslint/parser": "^6.19.0",
21
- "eslint": "^8.16.0",
22
- "eslint-config-prettier": "^9.0.0",
23
- "eslint-import-resolver-typescript": "^3.6.1",
24
- "eslint-plugin-import": "^2.29.1",
25
- "eslint-plugin-jest": "^27.6.3",
26
- "husky": "^8.0.1",
27
- "jest": "^29.7.0",
28
- "jest-environment-jsdom": "^29.7.0",
29
- "lint-staged": "^15.2.0",
30
- "prettier": "^3.2.2",
31
- "ts-jest": "^29.1.1",
32
- "typescript": "^5.3.3"
16
+ "@eslint/js": "^10.0.1",
17
+ "@types/node": "^26.6.6",
18
+ "@vitest/coverage-v8": "^5.1.1",
19
+ "@vitest/eslint-plugin": "^1.6.28",
20
+ "eslint": "^10.15.0",
21
+ "eslint-config-prettier": "^10.1.8",
22
+ "husky": "^9.1.7",
23
+ "jsdom": "^31.0.1",
24
+ "lint-staged": "^17.6.1",
25
+ "prettier": "^3.10.1",
26
+ "typescript": "^6.0.3",
27
+ "typescript-eslint": "^8.73.0",
28
+ "vitest": "^5.1.1"
33
29
  },
34
30
  "lint-staged": {
35
31
  "*.js,*.ts": "eslint --cache --fix",
36
32
  "*.{js,ts,css,md}": "prettier --write"
33
+ },
34
+ "scripts": {
35
+ "lint": "eslint",
36
+ "test": "vitest run --coverage",
37
+ "build": "rm -rf dist/ && tsc && cp README.md dist/ && jq 'del(.scripts.prepare)' package.json > dist/package.json"
37
38
  }
38
- }
39
+ }
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/index.js DELETED
@@ -1 +0,0 @@
1
- export {};