@kokiito0926/superhacker 0.0.0 → 0.0.1
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 +16 -0
- package/index.js +14 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
3
3
|
スーパーハッカー(superhacker)を用いれば、Hacker NewsのAPIをかんたんに呼び出すことができます。
|
|
4
4
|
Hacker Newsの投稿などをJSON形式で、コマンドラインに表示することができるので便利かもしれません。
|
|
5
5
|
|
|
6
|
+
## インストール
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
$ npm install --global @kokiito0926/superhacker
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## 実行方法
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
$ superhacker list
|
|
16
|
+
$ superhacker list --limit 10
|
|
17
|
+
$ superhacker comment --id 46726480
|
|
18
|
+
$ superhacker comments --id 46726480
|
|
19
|
+
$ superhacker comments --id 46726480 --format flat
|
|
20
|
+
```
|
|
21
|
+
|
|
6
22
|
## ライセンス
|
|
7
23
|
|
|
8
24
|
[MIT](LICENSE)
|
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// >> $ node ./
|
|
4
|
-
// >> $ node ./
|
|
5
|
-
// >> $ node ./
|
|
6
|
-
// >> $ node ./
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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}`);
|