@kokiito0926/superhacker 0.0.0 → 0.0.2

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 +33 -3
  2. package/index.js +14 -19
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,7 +1,37 @@
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
+
6
+ ## インストール
7
+
8
+ ```bash
9
+ $ npm install --global @kokiito0926/superhacker
10
+ ```
11
+
12
+ ## 使用方法
13
+
14
+ ストーリーの一覧を表示します。
15
+ --limitのオプションで表示件数を制限することもできます。
16
+
17
+ ```bash
18
+ $ superhacker list
19
+ $ superhacker list --limit 10
20
+ ```
21
+
22
+ コメントのIDを指定して、詳細を取得します。
23
+
24
+ ```bash
25
+ $ superhacker comment --id 46726480
26
+ ```
27
+
28
+ コメントのIDを指定して、すべての返信を再帰的に取得します。
29
+ デフォルトでは、ツリー構造で取得されますが、フラットな形式で取得することもできます。
30
+
31
+ ```bash
32
+ $ superhacker comments --id 46726480
33
+ $ superhacker comments --id 46726480 --format flat
34
+ ```
5
35
 
6
36
  ## ライセンス
7
37
 
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // >> $ node ./example.js list
4
- // >> $ node ./example.js list --limit 5
5
- // >> $ node ./example.js comment --id 2921983
6
- // >> $ node ./example.js comments --id 8863 --format flat
3
+ // >> $ node ./index.js list
4
+ // >> $ node ./index.js list --limit 5
5
+ // >> $ node ./index.js comment --id 2921983
6
+ // >> $ node ./index.js comments --id 8863 --format flat
7
7
 
8
8
  import { minimist, argv } from "zx";
9
9
 
@@ -50,11 +50,9 @@ if (!command) {
50
50
  const args = minimist(process.argv.slice(2));
51
51
  const id = args.id;
52
52
  const limit = args.limit ? parseInt(args.limit) : null;
53
- // const limit = args.limit ? parseInt(args.limit) : 10;
54
53
  const flat = args.format ? args.format === "flat" : false;
55
54
 
56
55
  if (command === "list") {
57
- // const limit = argv.limit ? parseInt(argv.limit) : 10;
58
56
  try {
59
57
  const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
60
58
  let ids = await idsResponse.json();
@@ -63,11 +61,7 @@ if (command === "list") {
63
61
  ids = ids.slice(0, limit);
64
62
  }
65
63
 
66
- // console.log(ids.length);
67
- // process.exit(0);
68
-
69
64
  const stories = await Promise.all(ids.map((id) => getItem(id)));
70
- // const stories = await Promise.all(ids.slice(0, limit).map((id) => getItem(id)));
71
65
  const sortedStories = stories.sort((a, b) => b.score - a.score);
72
66
 
73
67
  console.log(JSON.stringify(sortedStories, null, 2));
@@ -76,7 +70,6 @@ if (command === "list") {
76
70
  process.exit(1);
77
71
  }
78
72
  } else if (command === "comment") {
79
- // const id = argv.id;
80
73
  if (!id) {
81
74
  console.error("Error: ID is required via --id or stdin.");
82
75
  process.exit(1);
@@ -84,7 +77,6 @@ if (command === "list") {
84
77
  const item = await getItem(id);
85
78
  console.log(JSON.stringify(item, null, 2));
86
79
  } else if (command === "comments") {
87
- // const id = argv.id;
88
80
  if (!id) {
89
81
  console.error("Error: Story ID is required.");
90
82
  process.exit(1);
@@ -96,20 +88,23 @@ if (command === "list") {
96
88
  console.log(JSON.stringify([]));
97
89
  process.exit(0);
98
90
  }
99
- // console.log(rootItem);
100
91
 
101
92
  const flatComments = rootItem.kids ? await getCommentsRecursive(rootItem.kids) : [];
102
93
  // console.log(flatComments);
103
94
 
104
95
  if (flat) {
105
96
  console.log(JSON.stringify([rootItem, ...flatComments], null, 2));
106
- // console.log(JSON.stringify(flatComments, null, 2));
107
97
  } else {
108
- const treeComments = {
109
- ...rootItem,
110
- replies: buildTree(flatComments, parseInt(id)),
111
- };
112
- console.log(JSON.stringify(treeComments, null, 2));
98
+ console.log(
99
+ JSON.stringify(
100
+ {
101
+ ...rootItem,
102
+ replies: buildTree(flatComments, parseInt(id)),
103
+ },
104
+ null,
105
+ 2,
106
+ ),
107
+ );
113
108
  }
114
109
  } catch (e) {
115
110
  console.error(`Error fetching comments: ${e.message}`);
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@kokiito0926/superhacker",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
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",