Skip to content

Commit 3cb9a8f

Browse files
committed
Supporting CLI mode
1 parent babdfe9 commit 3cb9a8f

File tree

6 files changed

+115
-55
lines changed

6 files changed

+115
-55
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
insert_final_newline = true
9+
indent_style = space
10+
indent_size = 2
11+
trim_trailing_whitespace = true
12+
13+
[*.md]
14+
trim_trailing_whitespace = false

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
Node.js JSON generator for the simple and diligent HTML5 audio player [MUSE](https://github.com/moefront/muse).
77

88

9-
## Installation
9+
## Usage
1010

11+
### with Node.js API
1112
```bash
1213
$ npm install --save muse-json-generator
1314
```
1415

15-
## Usage
1616
Create a new file named `playlist.js`
1717

1818
```js
@@ -26,6 +26,18 @@ generator(477331181, 480097777).then(playlist => {
2626
node playlist.js > playlist.json
2727
```
2828

29+
### on CLI
30+
```bash
31+
$ npm install -g muse-json-generator
32+
```
33+
34+
```bash
35+
$ muse 477331181 480097777
36+
$ muse 477331181,480097777
37+
```
38+
39+
This action would generator a `playlisy.json` in your current working directory.
40+
2941
## API
3042

3143
```js
@@ -39,8 +51,9 @@ const generator = require('muse-json-generator');
3951
## Todo list
4052

4153
- [ ] Adjust translation
54+
- [ ] test
4255

4356

4457
## License
4558

46-
© 2017 MoeFront Studio | The MIT License (MIT).
59+
© 2017 MoeFront Studio | The MIT License (MIT).

bin/muse.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
/* eslint no-var: 0 */
3+
/* eslint prefer-arrow-callback: 0 */
4+
var fs = require('fs');
5+
var generator = require('../lib/generator');
6+
var pkg = require('../package.json');
7+
8+
var argv = process.argv.slice(2);
9+
10+
if (argv.length === 0) {
11+
console.log([pkg.name, pkg.version].join(' '));
12+
} else {
13+
if (argv.length === 1) {
14+
argv = argv[0].split(',');
15+
}
16+
17+
generator
18+
.apply(this, argv)
19+
.then(function(playlist) {
20+
fs.writeFileSync('playlist.json', playlist);
21+
console.log('playlist.json generated successfully');
22+
})
23+
.catch(function(err) {
24+
console.error(err);
25+
});
26+
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
"version": "1.0.2",
44
"description": "JSON generator for MUSE",
55
"main": "lib/generator.js",
6+
"bin": {
7+
"muse": "bin/muse.js",
8+
"muse-json-generator": "bin/muse.js"
9+
},
610
"scripts": {
711
"build": "npm run trash && babel src --out-dir lib",
812
"test": "echo \"Error: no test specified\" && exit 1",
913
"trash": "trash build stats lib",
1014
"watch": "babel src --watch --out-dir lib",
11-
"lint": "eslint ./src"
15+
"lint": "eslint ./src ./bin"
1216
},
1317
"repository": {
1418
"type": "git",
@@ -26,7 +30,7 @@
2630
"homepage": "https://github.com/moefront/muse-json-generator#readme",
2731
"dependencies": {
2832
"babel-runtime": "^6.26.0",
29-
"honoka": "^0.3.4"
33+
"honoka": "^0.3.5"
3034
},
3135
"devDependencies": {
3236
"babel-cli": "^6.26.0",

src/generator.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
import honoka from 'honoka';
22

33
function parseResponse(response) {
4-
try {
5-
response = JSON.parse(response);
6-
} catch (e) {
7-
throw new Error('response is not a valid json string');
8-
}
4+
try {
5+
response = JSON.parse(response);
6+
} catch (e) {
7+
throw new Error('response is not a valid json string');
8+
}
99

10-
if (response.code !== 200) {
11-
throw new Error('response code is not valid', response.code);
12-
}
10+
if (response.code !== 200) {
11+
throw new Error('response code is not valid', response.code);
12+
}
1313

14-
return response;
14+
return response;
1515
}
1616

1717
async function generator() {
18-
if (arguments.length === 0) {
19-
throw new Error('invalid input');
20-
}
21-
22-
const result = [];
23-
24-
for (const id of Array.from(arguments)) {
25-
const item = {};
26-
let songDetail = await honoka.get(
27-
`http://music.163.com/api/song/detail/?id=${id}&ids=%5B${id}%5D`
28-
);
29-
songDetail = parseResponse(songDetail);
30-
31-
songDetail = songDetail.songs[0];
32-
33-
item.title = songDetail.name;
34-
item.artist = songDetail.artists[0].name;
35-
item.cover = songDetail.album.picUrl;
36-
item.src = `https://api.kotori.love/netease/${id}.mp3`;
37-
38-
let lyricDetail = await honoka.get(
39-
`http://music.163.com/api/song/lyric?os=pc&id=${id}&lv=-1&kv=-1&tv=-1`
40-
);
41-
lyricDetail = parseResponse(lyricDetail);
42-
43-
item.lrc = lyricDetail.lrc.lyric;
44-
item.translation = lyricDetail.tlyric.lyric;
45-
46-
['lrc', 'translation'].forEach(key => {
47-
if (item[key] === null) {
48-
delete item[key];
49-
}
50-
});
51-
52-
result.push(item);
53-
}
54-
55-
return JSON.stringify(result, null, 2);
18+
if (arguments.length === 0) {
19+
throw new Error('invalid input');
20+
}
21+
22+
const result = [];
23+
24+
for (const id of Array.from(arguments)) {
25+
if (!/^[0-9]*[1-9][0-9]*$/.test(id)) {
26+
throw new Error('invalid song ID');
27+
}
28+
const item = {};
29+
let songDetail = await honoka.get(
30+
`http://music.163.com/api/song/detail/?id=${id}&ids=%5B${id}%5D`
31+
);
32+
songDetail = parseResponse(songDetail);
33+
34+
songDetail = songDetail.songs[0];
35+
36+
item.title = songDetail.name;
37+
item.artist = songDetail.artists[0].name;
38+
item.cover = songDetail.album.picUrl;
39+
item.src = `https://api.kotori.love/netease/${id}.mp3`;
40+
41+
let lyricDetail = await honoka.get(
42+
`http://music.163.com/api/song/lyric?os=pc&id=${id}&lv=-1&kv=-1&tv=-1`
43+
);
44+
lyricDetail = parseResponse(lyricDetail);
45+
46+
item.lyric = lyricDetail.lrc.lyric;
47+
item.translation = lyricDetail.tlyric.lyric;
48+
49+
['lyric', 'translation'].forEach(key => {
50+
if (item[key] === null) {
51+
delete item[key];
52+
}
53+
});
54+
55+
result.push(item);
56+
}
57+
58+
return JSON.stringify(result, null, 2);
5659
}
5760

5861
export default generator;

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,9 +1408,9 @@ home-or-tmp@^2.0.0:
14081408
os-homedir "^1.0.0"
14091409
os-tmpdir "^1.0.1"
14101410

1411-
honoka@^0.3.4:
1412-
version "0.3.4"
1413-
resolved "https://registry.npmjs.org/honoka/-/honoka-0.3.4.tgz#c7c98fc96a042651eba8a71faea77582de5f0b10"
1411+
honoka@^0.3.5:
1412+
version "0.3.5"
1413+
resolved "https://registry.npmjs.org/honoka/-/honoka-0.3.5.tgz#13355c00c3bb1f1d46bf8acf9d3a1078766594bd"
14141414
dependencies:
14151415
node-fetch "^1.7.2"
14161416
whatwg-fetch "^2.0.3"

0 commit comments

Comments
 (0)