@kokiito0926/channeling 0.0.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/LICENSE +21 -0
- package/README.md +7 -0
- package/index.js +195 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Koki Ito
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// >> $ ./index.js board
|
|
4
|
+
// >> $ ./index.js threads news4vip
|
|
5
|
+
// >> $ ./index.js thread news4vip 1770976897
|
|
6
|
+
|
|
7
|
+
import { argv } from "zx";
|
|
8
|
+
import iconv from "iconv-lite";
|
|
9
|
+
|
|
10
|
+
const command = argv._[0];
|
|
11
|
+
if (!command) {
|
|
12
|
+
console.error(`Error: Command is required (board, threads, thread).`);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const SC_MENU_URL = "http://menu.2ch.sc/bbsmenu.html";
|
|
17
|
+
|
|
18
|
+
async function getScBoardList() {
|
|
19
|
+
try {
|
|
20
|
+
const response = await fetch(SC_MENU_URL);
|
|
21
|
+
if (!response.ok) throw new Error(`HTTP Error: ${response.status}`);
|
|
22
|
+
|
|
23
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
24
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
25
|
+
|
|
26
|
+
// CP932でデコード
|
|
27
|
+
const html = iconv.decode(buffer, "CP932");
|
|
28
|
+
|
|
29
|
+
const categories = [];
|
|
30
|
+
let currentCategory = null;
|
|
31
|
+
|
|
32
|
+
// 行ごとに分割してパース
|
|
33
|
+
const lines = html.split("\n");
|
|
34
|
+
|
|
35
|
+
for (let line of lines) {
|
|
36
|
+
// カテゴリ名 (例: <B>ニュース</B>)
|
|
37
|
+
const catMatch = line.match(/<B>(.*?)<\/B>/);
|
|
38
|
+
if (catMatch) {
|
|
39
|
+
currentCategory = { category: catMatch[1], boards: [] };
|
|
40
|
+
categories.push(currentCategory);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 修正後の正規表現
|
|
45
|
+
// 1. HREF= の直後にある // から始まる文字列をキャプチャ
|
|
46
|
+
// 2. > までの間の文字を板名としてキャプチャ
|
|
47
|
+
const boardMatch = line.match(/<A HREF=(\/\/.*?\/)>(.*?)<\/A>/);
|
|
48
|
+
|
|
49
|
+
if (boardMatch && currentCategory) {
|
|
50
|
+
let boardUrl = boardMatch[1];
|
|
51
|
+
const boardName = boardMatch[2];
|
|
52
|
+
|
|
53
|
+
// //ai.2ch.sc/kokusai/ のような形式を http: に補完
|
|
54
|
+
if (boardUrl.startsWith("//")) {
|
|
55
|
+
boardUrl = "http:" + boardUrl;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
currentCategory.boards.push({
|
|
59
|
+
name: boardName,
|
|
60
|
+
url: boardUrl,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return categories;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error(err.message);
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function getBoardUrl(targetBoard) {
|
|
73
|
+
const categories = await getScBoardList();
|
|
74
|
+
for (const cat of categories) {
|
|
75
|
+
const board = cat.boards.find((b) => {
|
|
76
|
+
const boardDir = b.url.split("/").filter(Boolean).pop();
|
|
77
|
+
return boardDir === targetBoard;
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (board) return board.url;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function getScThreadList(url) {
|
|
86
|
+
const baseUrl = url.endsWith("/") ? url : url + "/";
|
|
87
|
+
const subjectUrl = `${baseUrl}subject.txt`;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const response = await fetch(subjectUrl);
|
|
91
|
+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
|
92
|
+
|
|
93
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
94
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
95
|
+
const text = iconv.decode(buffer, "CP932");
|
|
96
|
+
|
|
97
|
+
const threads = text
|
|
98
|
+
.split("\n")
|
|
99
|
+
.filter((line) => line.includes("<>"))
|
|
100
|
+
.map((line) => {
|
|
101
|
+
const [dat, titleAndCount] = line.split("<>");
|
|
102
|
+
const match = titleAndCount.match(/(.*)\s\((\d+)\)$/);
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
id: dat.replace(".dat", ""),
|
|
106
|
+
title: match ? match[1] : titleAndCount,
|
|
107
|
+
count: match ? match[2] : "0",
|
|
108
|
+
datUrl: `${baseUrl}dat/${dat}`,
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return threads;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error(err.message);
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function getScThreadContent(boardUrl, threadId) {
|
|
120
|
+
try {
|
|
121
|
+
const parts = boardUrl.split("/").filter(Boolean);
|
|
122
|
+
const server = parts[1]; // mi.2ch.sc
|
|
123
|
+
const board = parts[parts.length - 1]; // news4vip
|
|
124
|
+
|
|
125
|
+
const datUrl = `http://${server}/${board}/dat/${threadId}.dat`;
|
|
126
|
+
|
|
127
|
+
const response = await fetch(datUrl);
|
|
128
|
+
if (!response.ok) throw new Error(`HTTP Error: ${response.status}`);
|
|
129
|
+
|
|
130
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
131
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
132
|
+
const text = iconv.decode(buffer, "CP932");
|
|
133
|
+
|
|
134
|
+
const posts = text
|
|
135
|
+
.split("\n")
|
|
136
|
+
.filter((line) => line.includes("<>"))
|
|
137
|
+
.map((line, index) => {
|
|
138
|
+
const [name, mail, dateId, body, title] = line.split("<>");
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
number: index + 1,
|
|
142
|
+
name: name.replace(/<[^>]*>?/gm, ""), // タグ除去
|
|
143
|
+
dateId,
|
|
144
|
+
body: body.replace(/ <br> /g, "\n").trim(),
|
|
145
|
+
title: title || null,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return posts;
|
|
150
|
+
} catch (err) {
|
|
151
|
+
console.error(err.message);
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (command === "board") {
|
|
157
|
+
const menu = await getScBoardList();
|
|
158
|
+
console.log(JSON.stringify(menu, null, 2));
|
|
159
|
+
process.exit(0);
|
|
160
|
+
}
|
|
161
|
+
else if (command === "threads") {
|
|
162
|
+
const boardId = argv._[1];
|
|
163
|
+
if (!boardId) {
|
|
164
|
+
console.error("Error: Board ID is required (e.g. news4vip)");
|
|
165
|
+
process.exit(1);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const boardUrl = await getBoardUrl(boardId);
|
|
169
|
+
if (!boardUrl) {
|
|
170
|
+
console.error(`Error: Board not found: ${boardId}`);
|
|
171
|
+
process.exit(1);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const threads = await getScThreadList(boardUrl);
|
|
175
|
+
console.log(JSON.stringify(threads, null, 2));
|
|
176
|
+
process.exit(0);
|
|
177
|
+
}
|
|
178
|
+
else if (command === "thread") {
|
|
179
|
+
const boardId = argv._[1];
|
|
180
|
+
const threadId = argv._[2];
|
|
181
|
+
if (!boardId || !threadId) {
|
|
182
|
+
console.error("Error: Board ID and Thread ID are required (e.g. news4vip 1770975311)");
|
|
183
|
+
process.exit(1);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const boardUrl = await getBoardUrl(boardId);
|
|
187
|
+
if (!boardUrl) {
|
|
188
|
+
console.error(`Error: Board not found: ${boardId}`);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const posts = await getScThreadContent(boardUrl, threadId);
|
|
193
|
+
console.log(JSON.stringify(posts, null, 2));
|
|
194
|
+
process.exit(0);
|
|
195
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kokiito0926/channeling",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "2chのスレッドなどをJSON形式で取得することができるコマンドラインのツールです。",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"cli",
|
|
8
|
+
"bash",
|
|
9
|
+
"shell",
|
|
10
|
+
"script",
|
|
11
|
+
"nodejs",
|
|
12
|
+
"2ch",
|
|
13
|
+
"scraping",
|
|
14
|
+
"channeling"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://github.com/kokiito0926/channeling#readme",
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"channeling": "index.js"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/kokiito0926/channeling/issues"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/kokiito0926/channeling.git"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"author": "Koki Ito <kokiito0926@gmail.com>",
|
|
32
|
+
"type": "module",
|
|
33
|
+
"scripts": {
|
|
34
|
+
"test": "node --test"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"iconv-lite": "^0.7.2",
|
|
38
|
+
"zx": "^8.8.4"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=24.0.0"
|
|
42
|
+
}
|
|
43
|
+
}
|