@kokiito0926/superhacker 0.0.4 → 0.0.6
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 +5 -9
- package/index.js +23 -33
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
## superhacker
|
|
2
2
|
|
|
3
|
-
superhackerは、Hacker NewsのAPIを利用して、ストーリーやコメントをJSON形式で取得することができるコマンドラインのツールです。
|
|
3
|
+
superhackerは、[Hacker News](https://news.ycombinator.com/)のAPIを利用して、ストーリーやコメントをJSON形式で取得することができるコマンドラインのツールです。
|
|
4
4
|
JSON形式でストーリーやコメントを取得することができるので、jqなどでフィルタリングをしやすくなったり、大規模言語モデルに与えやすくなったりします。
|
|
5
5
|
|
|
6
6
|
## インストール
|
|
@@ -11,26 +11,22 @@ $ npm install --global @kokiito0926/superhacker
|
|
|
11
11
|
|
|
12
12
|
## 使用方法
|
|
13
13
|
|
|
14
|
-
ストーリーの一覧を表示します。
|
|
15
|
-
--limitのオプションで表示件数を制限することもできます。
|
|
14
|
+
ストーリーの一覧を表示します。
|
|
16
15
|
|
|
17
16
|
```bash
|
|
18
17
|
$ superhacker list
|
|
19
|
-
$ superhacker list --limit 10
|
|
20
18
|
```
|
|
21
19
|
|
|
22
20
|
コメントのIDを指定して、詳細を取得します。
|
|
23
21
|
|
|
24
22
|
```bash
|
|
25
|
-
$ superhacker comment
|
|
23
|
+
$ superhacker comment 46726480
|
|
26
24
|
```
|
|
27
25
|
|
|
28
|
-
コメントのIDを指定して、すべての返信を再帰的に取得します。
|
|
29
|
-
デフォルトでは、ツリー構造で取得されますが、フラットな形式で取得することもできます。
|
|
26
|
+
コメントのIDを指定して、すべての返信を再帰的に取得します。
|
|
30
27
|
|
|
31
28
|
```bash
|
|
32
|
-
$ superhacker comments
|
|
33
|
-
$ superhacker comments --id 46726480 --format flat
|
|
29
|
+
$ superhacker comments 46726480
|
|
34
30
|
```
|
|
35
31
|
|
|
36
32
|
## ライセンス
|
package/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// >> $ node ./index.js list
|
|
4
|
-
// >> $ node ./index.js
|
|
5
|
-
// >> $ node ./index.js
|
|
6
|
-
// >> $ node ./index.js comments --id 8863 --format flat
|
|
4
|
+
// >> $ node ./index.js comment <id>
|
|
5
|
+
// >> $ node ./index.js comments <id>
|
|
7
6
|
|
|
8
7
|
import { argv } from "zx";
|
|
9
8
|
|
|
@@ -67,9 +66,8 @@ if (!command) {
|
|
|
67
66
|
}
|
|
68
67
|
// console.log(command);
|
|
69
68
|
|
|
70
|
-
const
|
|
71
|
-
const
|
|
72
|
-
const flat = argv?.format ? argv?.format === "flat" : false;
|
|
69
|
+
const boardId = argv._[1];
|
|
70
|
+
const threadId = argv._[2];
|
|
73
71
|
|
|
74
72
|
if (command === "list") {
|
|
75
73
|
const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
|
|
@@ -79,10 +77,6 @@ if (command === "list") {
|
|
|
79
77
|
}
|
|
80
78
|
let ids = await idsResponse.json();
|
|
81
79
|
|
|
82
|
-
if (limit > 0) {
|
|
83
|
-
ids = ids.slice(0, limit);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
80
|
const stories = await Promise.all(ids.map((id) => getItem(id)));
|
|
87
81
|
const sortedStories = stories
|
|
88
82
|
.filter((s) => s !== null)
|
|
@@ -90,46 +84,42 @@ if (command === "list") {
|
|
|
90
84
|
|
|
91
85
|
console.log(JSON.stringify(sortedStories, null, 2));
|
|
92
86
|
} else if (command === "comment" || command === "item") {
|
|
93
|
-
if (!
|
|
94
|
-
console.error("Error: ID is required
|
|
87
|
+
if (!boardId) {
|
|
88
|
+
console.error("Error: ID is required.");
|
|
95
89
|
process.exit(1);
|
|
96
90
|
}
|
|
97
|
-
const item = await getItem(
|
|
91
|
+
const item = await getItem(parseInt(boardId));
|
|
98
92
|
if (!item) {
|
|
99
|
-
console.error(`Error: Item ${
|
|
93
|
+
console.error(`Error: Item ${boardId} not found.`);
|
|
100
94
|
process.exit(1);
|
|
101
95
|
}
|
|
102
96
|
console.log(JSON.stringify(item, null, 2));
|
|
103
97
|
} else if (command === "comments") {
|
|
104
|
-
if (!
|
|
105
|
-
console.error("Error: Story/Comment ID is required
|
|
98
|
+
if (!boardId) {
|
|
99
|
+
console.error("Error: Story/Comment ID is required.");
|
|
106
100
|
process.exit(1);
|
|
107
101
|
}
|
|
108
102
|
|
|
109
|
-
const rootItem = await getItem(
|
|
103
|
+
const rootItem = await getItem(parseInt(boardId));
|
|
110
104
|
if (!rootItem) {
|
|
111
|
-
console.error(`Error: Root item ${
|
|
105
|
+
console.error(`Error: Root item ${boardId} not found.`);
|
|
112
106
|
process.exit(1);
|
|
113
107
|
}
|
|
114
108
|
|
|
115
109
|
const flatComments = rootItem.kids ? await getCommentsRecursive(rootItem.kids) : [];
|
|
116
110
|
// console.log(flatComments);
|
|
117
111
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
2,
|
|
130
|
-
),
|
|
131
|
-
);
|
|
132
|
-
}
|
|
112
|
+
const tree = buildTree(flatComments, parseInt(boardId));
|
|
113
|
+
console.log(
|
|
114
|
+
JSON.stringify(
|
|
115
|
+
{
|
|
116
|
+
...rootItem,
|
|
117
|
+
replies: tree,
|
|
118
|
+
},
|
|
119
|
+
null,
|
|
120
|
+
2,
|
|
121
|
+
),
|
|
122
|
+
);
|
|
133
123
|
} else {
|
|
134
124
|
console.error(`Error: Unknown command "${command}". Available commands: list, comment, comments.`);
|
|
135
125
|
process.exit(1);
|