@kokiito0926/superhacker 0.0.2 → 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.
- package/README.md +2 -2
- package/index.js +33 -44
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ $ npm install --global @kokiito0926/superhacker
|
|
|
11
11
|
|
|
12
12
|
## 使用方法
|
|
13
13
|
|
|
14
|
-
ストーリーの一覧を表示します。
|
|
14
|
+
ストーリーの一覧を表示します。
|
|
15
15
|
--limitのオプションで表示件数を制限することもできます。
|
|
16
16
|
|
|
17
17
|
```bash
|
|
@@ -25,7 +25,7 @@ $ superhacker list --limit 10
|
|
|
25
25
|
$ superhacker comment --id 46726480
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
コメントのIDを指定して、すべての返信を再帰的に取得します。
|
|
28
|
+
コメントのIDを指定して、すべての返信を再帰的に取得します。
|
|
29
29
|
デフォルトでは、ツリー構造で取得されますが、フラットな形式で取得することもできます。
|
|
30
30
|
|
|
31
31
|
```bash
|
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 {
|
|
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
|
|
51
|
-
const
|
|
52
|
-
const
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
let ids = await idsResponse.json();
|
|
55
|
+
const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
|
|
56
|
+
let ids = await idsResponse.json();
|
|
59
57
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
58
|
+
if (limit > 0) {
|
|
59
|
+
ids = ids.slice(0, limit);
|
|
60
|
+
}
|
|
63
61
|
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
79
|
+
const rootItem = await getItem(id);
|
|
80
|
+
if (!rootItem) {
|
|
81
|
+
console.log(JSON.stringify([]));
|
|
82
|
+
process.exit(0);
|
|
83
|
+
}
|
|
91
84
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
}
|