@kokiito0926/superhacker 0.0.1 → 0.0.3

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.
Files changed (3) hide show
  1. package/README.md +18 -4
  2. package/index.js +33 -44
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
- ## スーパーハッカー(superhacker
1
+ ## superhacker
2
2
 
3
- スーパーハッカー(superhacker)を用いれば、Hacker NewsのAPIをかんたんに呼び出すことができます。
4
- Hacker Newsの投稿などをJSON形式で、コマンドラインに表示することができるので便利かもしれません。
3
+ superhackerは、Hacker NewsのAPIを利用して、ストーリーやコメントをJSON形式で取得することができるコマンドラインのツールです。
4
+ JSON形式でストーリーやコメントを取得することができるので、jqなどでフィルタリングをしやすくなったり、大規模言語モデルに与えやすくなったりします。
5
5
 
6
6
  ## インストール
7
7
 
@@ -9,12 +9,26 @@ Hacker Newsの投稿などをJSON形式で、コマンドラインに表示す
9
9
  $ npm install --global @kokiito0926/superhacker
10
10
  ```
11
11
 
12
- ## 実行方法
12
+ ## 使用方法
13
+
14
+ ストーリーの一覧を表示します。
15
+ --limitのオプションで表示件数を制限することもできます。
13
16
 
14
17
  ```bash
15
18
  $ superhacker list
16
19
  $ superhacker list --limit 10
20
+ ```
21
+
22
+ コメントのIDを指定して、詳細を取得します。
23
+
24
+ ```bash
17
25
  $ superhacker comment --id 46726480
26
+ ```
27
+
28
+ コメントのIDを指定して、すべての返信を再帰的に取得します。
29
+ デフォルトでは、ツリー構造で取得されますが、フラットな形式で取得することもできます。
30
+
31
+ ```bash
18
32
  $ superhacker comments --id 46726480
19
33
  $ superhacker comments --id 46726480 --format flat
20
34
  ```
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  // >> $ node ./index.js comment --id 2921983
6
6
  // >> $ node ./index.js comments --id 8863 --format flat
7
7
 
8
- import { minimist, argv } from "zx";
8
+ import { argv } from "zx";
9
9
 
10
10
  const BASE_URL = "https://hacker-news.firebaseio.com/v0";
11
11
 
@@ -47,28 +47,22 @@ if (!command) {
47
47
  }
48
48
  // console.log(command);
49
49
 
50
- const args = minimist(process.argv.slice(2));
51
- const id = args.id;
52
- const limit = args.limit ? parseInt(args.limit) : null;
53
- const flat = args.format ? args.format === "flat" : false;
50
+ const id = argv?.id;
51
+ const limit = argv?.limit ? parseInt(argv?.limit) : null;
52
+ const flat = argv?.format ? argv?.format === "flat" : false;
54
53
 
55
54
  if (command === "list") {
56
- try {
57
- const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
58
- let ids = await idsResponse.json();
55
+ const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
56
+ let ids = await idsResponse.json();
59
57
 
60
- if (limit > 0) {
61
- ids = ids.slice(0, limit);
62
- }
58
+ if (limit > 0) {
59
+ ids = ids.slice(0, limit);
60
+ }
63
61
 
64
- const stories = await Promise.all(ids.map((id) => getItem(id)));
65
- const sortedStories = stories.sort((a, b) => b.score - a.score);
62
+ const stories = await Promise.all(ids.map((id) => getItem(id)));
63
+ const sortedStories = stories.sort((a, b) => b.score - a.score);
66
64
 
67
- console.log(JSON.stringify(sortedStories, null, 2));
68
- } catch (e) {
69
- console.error(`Error fetching list: ${e.message}`);
70
- process.exit(1);
71
- }
65
+ console.log(JSON.stringify(sortedStories, null, 2));
72
66
  } else if (command === "comment") {
73
67
  if (!id) {
74
68
  console.error("Error: ID is required via --id or stdin.");
@@ -82,32 +76,27 @@ if (command === "list") {
82
76
  process.exit(1);
83
77
  }
84
78
 
85
- try {
86
- const rootItem = await getItem(id);
87
- if (!rootItem) {
88
- console.log(JSON.stringify([]));
89
- process.exit(0);
90
- }
79
+ const rootItem = await getItem(id);
80
+ if (!rootItem) {
81
+ console.log(JSON.stringify([]));
82
+ process.exit(0);
83
+ }
91
84
 
92
- const flatComments = rootItem.kids ? await getCommentsRecursive(rootItem.kids) : [];
93
- // console.log(flatComments);
94
-
95
- if (flat) {
96
- console.log(JSON.stringify([rootItem, ...flatComments], null, 2));
97
- } else {
98
- console.log(
99
- JSON.stringify(
100
- {
101
- ...rootItem,
102
- replies: buildTree(flatComments, parseInt(id)),
103
- },
104
- null,
105
- 2,
106
- ),
107
- );
108
- }
109
- } catch (e) {
110
- console.error(`Error fetching comments: ${e.message}`);
111
- process.exit(1);
85
+ const flatComments = rootItem.kids ? await getCommentsRecursive(rootItem.kids) : [];
86
+ // console.log(flatComments);
87
+
88
+ if (flat) {
89
+ console.log(JSON.stringify([rootItem, ...flatComments], null, 2));
90
+ } else {
91
+ console.log(
92
+ JSON.stringify(
93
+ {
94
+ ...rootItem,
95
+ replies: buildTree(flatComments, parseInt(id)),
96
+ },
97
+ null,
98
+ 2,
99
+ ),
100
+ );
112
101
  }
113
102
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@kokiito0926/superhacker",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "private": false,
5
- "description": "スーパーハッカー(superhacker)を用いると、Hacker NewsのAPIをかんたんに呼び出すことができます。",
5
+ "description": "Hacker NewsのAPIを利用して、ストーリーやコメントをJSON形式で取得することができるコマンドラインのツールです。",
6
6
  "keywords": [
7
7
  "cli",
8
8
  "bash",