Skip to content

Commit 0fa76db

Browse files
authored
Merge pull request #1 from DreamOfIce/dev
Merge branch dev into main
2 parents 761f2c3 + 3732581 commit 0fa76db

22 files changed

Lines changed: 644 additions & 373 deletions

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.git
2+
.github
23
.husky
34
logs
45
node_modules

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Example environment variables
22
# For more information, see the documentation
33

4-
GITHUB_AUTH=Username:Token
5-
WEBHOOK_SECRET=someRandomString
4+
RA_GITHUB_AUTH=username:token
5+
RA_WEBHOOK_SECRET=someRandomString

.eslintrc.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
2+
"env": {
3+
"es6": true
4+
},
25
"parserOptions": {
36
"ecmaVersion": "latest",
47
"sourceType": "module"
58
},
69
"rules": {
710
"import/extensions": "off",
8-
"operator-linebreak": [
11+
"no-underscore-dangle": "off",
12+
"valid-typeof": [
913
"error",
10-
"after",
1114
{
12-
"overrides": {
13-
"?": "before",
14-
":": "before"
15-
}
15+
"requireStringLiterals": false
1616
}
1717
]
1818
},

.github/dependabot.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

.github/workflows/docker-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
file: ./Dockerfile
3737
platforms: linux/amd64,linux/arm64
3838
push: true
39-
tags: docker.io/dreamofice/hoyorandom:dev,swr.cn-south-1.myhuaweicloud.com/dreamofice/hoyorandom:dev
39+
tags: docker.io/dreamofice/randomapi:dev,swr.cn-south-1.myhuaweicloud.com/dreamofice/randomapi:dev

.github/workflows/docker-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
id: generate-tags
2727
env:
2828
INPUT_TAG: ${{ inputs.tag }}
29-
REGISTRYS: "docker.io/dreamofice/hoyorandom swr.cn-south-1.myhuaweicloud.com/dreamofice/hoyorandom"
29+
REGISTRYS: "docker.io/dreamofice/randomapi swr.cn-south-1.myhuaweicloud.com/dreamofice/randomapi"
3030
UPDATE_LATEST: ${{ env.GITHUB_REF_TYPE == 'tag' || inputs.updateLatest }}
3131
run: |
3232
update_latest=${UPDATE_LATEST};

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ ENV NODE_ENV=production
44
COPY . /app/
55
RUN if [ $(yarn) ];then yarn --registry=https://registry.npmmirror.com ; fi
66
EXPOSE 8006
7-
CMD yarn start
7+
CMD yarn start print-log

README.md

Lines changed: 61 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,101 @@
1-
# HoYoRandom
1+
# RandomAPI
22

3-
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/dreamofice/HoYoRandom) ![GitHub Repo stars](https://img.shields.io/github/stars/dreamofice/HoYoRandom) ![GitHub forks](https://img.shields.io/github/forks/dreamofice/HoYoRandom)
3+
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/dreamofice/randomapi) ![GitHub Repo stars](https://img.shields.io/github/stars/dreamofice/randomAPI) ![GitHub forks](https://img.shields.io/github/forks/dreamofice/randomapi)
44

55
一个使用 node.js 编写的随机 API:
66

7-
- 丰富的选项
8-
- API 和资源文件分离,自动更新
9-
- 支持自部署,可以使用你自己的资源(图片、视频、一言)
7+
- 开箱即用,同时拥有丰富的选项
8+
- 独立资源存储库,从 Github 自动拉取更新
9+
- 支持自部署,轻松自定义你自己的资源(图片、视频、一言)
1010
- 模块化架构,易于扩展
1111

12-
包括原神&崩坏 3 精选图片、音乐、视频以及一言.
12+
默认的资源(`HoYoRandom`)包括原神&崩坏 3 精选图片、音乐、视频以及一言.
1313
喜欢的话麻烦点个`Star`吧ヾ(≧▽≦\\\*)o
1414
欢迎贡献!
1515

16-
## 链接
16+
## Links
1717

18-
> 默认的资源存储在[DreamOfIce/HoYoRandomResources](https://github.com/DreamOfIce/HoYoRandomResources)
19-
> 详细文档位于[DreamOfIce/HoYoRandom-docs](https://github.com/DreamOfIce/HoYoRandom-docs)
20-
> 使用 PHP 编写的旧版本:[HoYoRandom-php](https://github.com/DreamOfIce/HoYoRandom-php)(不再更新)
18+
> DEMO: [点击进入](https://api.dreamofice.cn/hoyorandom)
19+
> 默认资源`HoYoRandom` [DreamOfIce/HoYoRandomResources](https://github.com/DreamOfIce/HoYoRandomResources)
20+
> 详细文档位于 [DreamOfIce/randomAPI-docs](https://github.com/DreamOfIce/randomAPI-docs)
21+
> 使用 PHP 编写的旧版本 [HoYoRandom-php](https://github.com/DreamOfIce/HoYoRandom-php)(_不再更新_)
2122
2223
## TODO
2324

2425
- [x] 一言接口
2526
- [x] 媒体文件接口
26-
- [ ] 自部署文档
27+
- [x] 自部署文档
2728
- [ ] 开发文档
2829
- [ ] 标签(tag)支持
2930
- [ ] 统计系统
30-
- [ ] `TypeScript`重写
31+
- [ ] `TypeScript`重写(v2.x)
3132

32-
## API 端点
33+
## 资源存储库
3334

34-
我提供的公共 API,服务器位于美国,目前没有速率限制:
35+
- 默认的配置使用[HoYoRandomResources](https://github.com/DreamOfIce/HoYoRandomResources), 包含了原神&崩坏 3 的图片、音乐、视频和一言
36+
- 通过修改`config.json`,可以使用自己的资源存储库,详见[文档-部署](https://docs.dreamofice.cn/hoyorandom/deploy/config)
37+
- 关于资源存储库的格式,请参考[文档-自定义资源](https://docs.dreamofice.cn/hoyorandom/resource)
3538

36-
> 本站已启用`HSTS`,并加入`Preload List`,故仅支持 https 调用
37-
> https://api.dreamofice.cn/hoyorandom/
38-
39-
---
40-
41-
## 接口
42-
43-
支持的参数列表请见[参数](#参数)一节
44-
45-
> 以下为默认的 API 接口,你可以自行部署不同的接口
39+
## 参数
4640

47-
### 图片
41+
目前支持三种方式传入参数:
4842

49-
- 路径: `/img`
50-
- 类型: `media`
51-
- 资源: [HoYoRandomResources/img](https://github.com/DreamOfIce/HoYoRandomResources/tree/master/img)
43+
> 优先级为从上到下递减
5244
53-
返回示例:
45+
- `URL`参数,如:
46+
`https://api.dreamofice.cn/hoyorandom/img?c=ys&f=json`
47+
- `URL`路径:
48+
> 路径与参数的对应关系依次为:`/endpoint/${category}/${tag}/${format}/${encoding}/${seletor}`
49+
> 如: `https://api.dreamofice.cn/hoyorandom/img/ys/all/json`
50+
- `POST`负载(支持`application/json``application/x-www-form-urlencoded`):
5451

55-
- Raw
56-
![试着刷新页面,这是随机的哦](https://api.dreamofice.cn/hoyorandom/img)
57-
- JSON
5852
```json
59-
{
60-
"category": "ys",
61-
"name": "韶光抚月,天下人间",
62-
"url": "https://cf.dreamofice.cn/p/HoYoRandom/img/ys/%E9%9F%B6%E5%85%89%E6%8A%9A%E6%9C%88%EF%BC%8C%E5%A4%A9%E4%B8%8B%E4%BA%BA%E9%97%B4.webp"
63-
}
53+
{ "category": "ys", "format": "json" }
6454
```
6555

66-
### 音乐
56+
### Media 类型
6757

68-
- 路径: `/music`
69-
- 类型: `media`
70-
- 资源: [HoYoRandomResources/music](https://github.com/DreamOfIce/HoYoRandomResources/tree/master/music)
58+
| 参数名 | 简写 | 默认值 | 合法值 | 描述 | 备注 |
59+
| :------: | :--: | :-----: | :--------------------------------------------------------------------------------: | :----------------------------: | :----------------------------------------------: |
60+
| category | c | `all` | `all`或有效的类型或其组成的数组 | 资源类型,`all`代指所有 | |
61+
| tag | t | `all` | `all`或有效的标签或其组成的数组 | 资源的标签(tag)(**开发中**) | 输入多个值时随机返回符合任意一个条件的资源 |
62+
| format | f | `raw` | `raw`,`json` | 返回的格式 | 具体示例参见[返回格式-Media](#Media) |
63+
| encoding | e | `utf-8` | 任意[支持的字符编码](https://github.com/ashtuchkin/iconv-lite#supported-encodings) | `format=json`时,返回文本的编码 | |
7164

72-
返回示例:
65+
### Hitokoto 类型
7366

74-
- Raw
75-
<audio controls src='https://api.dreamofice.cn/hoyorandom/music' >当前环境似乎不支持播放呢(っ °Д °;)っ</audio>
76-
- JSON
77-
```json
78-
{
79-
"category": "ys",
80-
"name": "HOYO-MiX - Ruu's Melody 阿瑠的歌",
81-
"url": "https://cf.dreamofice.cn/p/hoyorandom/music/ys/HOYO-MiX%20-%20Ruu's%20Melody%20%E9%98%BF%E7%91%A0%E7%9A%84%E6%AD%8C.mp3"
82-
}
83-
```
67+
| 参数名 | 简写 | 默认值 | 合法值 | 描述 | 备注 |
68+
| :------: | :--: | :---------: | :----------------------------------------------------------------------------------------------------: | :--------------------: | :----------------------------------------------: |
69+
| category | c | `all` | `all`或有效的类型或其组成的数组 | 资源类型,`all`代指所有 | |
70+
| tag | t | `all` | `all`或有效的标签或其组成的数组 (**开发中**) | 一言的标签(tag) | 输入多个值时随机返回符合任意一个条件的资源 |
71+
| format | f | `json` | `js`,`json`,`text` | 返回的格式 | 具体示例参见[返回格式-Hitokoto](#Hitokoto) |
72+
| encoding | e | `utf-8` | 任意[支持的字符编码](https://github.com/ashtuchkin/iconv-lite#supported-encodings) | 返回文本的编码 | |
73+
| seletor | s | `#hitokoto` | `format=js`时,要插入一言的元素的[CSS 选择器](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) | |
8474

85-
### 视频
75+
---
8676

87-
- 路径: `/video`
88-
- 类型: `media`
89-
- 资源: [HoYoRandomResources/video](https://github.com/DreamOfIce/HoYoRandomResources/tree/master/video)
77+
## 返回格式
9078

91-
返回示例:
79+
# Media
9280

93-
- Raw
94-
<video controls src="https://api.dreamofice.cn/hoyorandom/video"></video>
95-
- JSON
81+
- `raw` 直接 302 重定向到对应的资源:
82+
![raw格式](https://api.dreamofice.cn/hoyorandom/img)
83+
- `json`返回一个 JSON:
9684
```json
9785
{
9886
"category": "ys",
99-
"name": "295850674-1-208",
100-
"url": "https://cf.dreamofice.cn/p/hoyorandom/video/ys/295850674-1-208.mp4"
87+
"name": "韶光抚月,天下人间",
88+
"url": "https://cf.dreamofice.cn/p/HoYoRandom/img/ys/%E9%9F%B6%E5%85%89%E6%8A%9A%E6%9C%88%EF%BC%8C%E5%A4%A9%E4%B8%8B%E4%BA%BA%E9%97%B4.webp"
10189
}
10290
```
10391

104-
### 一言
105-
106-
- 路径: `/hitokoto`
107-
- 类型: `hitokoto`
108-
- 资源: [HoYoRandomResources/hitokoto](https://github.com/DreamOfIce/HoYoRandomResources/tree/master/hitokoto)
109-
110-
返回示例:
92+
# Hitokoto
11193

112-
- text
94+
- `text`直接返回文本
11395
```text
11496
烟花易逝,人情长存
11597
```
116-
- JSON
98+
- `json` 返回一个 JSON:
11799
```json
118100
{
119101
"hitokoto": "愿风神忽悠你",
@@ -123,46 +105,20 @@
123105
- js
124106

125107
```javascript
126-
document
127-
.querySeletor('#hitokoto')
128-
.innerText(
129-
'你不明白「牺牲是无法避免的」意味着什么,真正可怕的,并不是这个世界夺走了多少人的性命,而是它让多少人对失去生命这件事,习以为常',
130-
);
108+
document.querySeletor('#hitokoto').innerText('你不明白「牺牲是无法避免的」意味着什么,真正可怕的,并不是这个世界夺走了多少人的性命,而是它让多少人对失去生命这件事,习以为常');
131109
```
132110

133-
## 参数
134-
135-
目前支持三种方式传入参数:
136-
137-
> 优先级为从上到下递减
138-
139-
- `URL`参数:
140-
`https://api.dreamofice.cn/hoyorandom/img?c=ys&f=json`
141-
- `URL`路径:
142-
> 路径与参数的对应关系依次为:`/endpoint/${category}/${tag}/${format}/${encoding}/${seletor}` > `https://api.dreamofice.cn/hoyorandom/img/ys/all/json`
143-
- `POST`负载(支持`application/json``application/x-www-form-urlencoded`):
144-
```json
145-
{ "category": "ys", "format": "json" }
146-
```
111+
## HoYoRandom
147112

148-
### Media
113+
### API 端点
149114

150-
| 参数名 | 简写 | 默认值 | 合法值 | 描述 | 备注 |
151-
| :------: | :--: | :-----: | :--------------------------------------------------------------------------------: | :----------------------------: | :----------------------------------------------: |
152-
| category | c | `all` | `all` & 有效的类型值(`ys`,`bh3`)或其组成的数组 | 资源类型,`all`代指所有 | |
153-
| tag | t | `all` | `all` & 有效的标签或其组成的数组 | 资源的标签(tag)(**开发中**) | 输入多个值时随机返回符合任意一个条件的资源 |
154-
| format | f | `raw` | `raw`,`json` | 返回的格式 | 具体示例参见[接口-图片](#图片)一节的返回示例部分 |
155-
| encoding | e | `utf-8` | 任意[支持的字符编码](https://github.com/ashtuchkin/iconv-lite#supported-encodings) | `format=json`时,返回文本的编码 | |
115+
我提供的公共 `HoYoRandom` API,目前没有速率限制:
156116

157-
### Hitokoto
117+
> 此域名已启用`HSTS`,并加入`HSTS Preload List`,故仅支持 https 调用
118+
> https://api.dreamofice.cn/hoyorandom/
158119
159-
| 参数名 | 简写 | 默认值 | 合法值 | 描述 | 备注 |
160-
| :------: | :--: | :---------: | :----------------------------------------------------------------------------------------------------: | :--------------------: | :----------------------------------------------: |
161-
| category | c | `all` | `all`或有效的类型字符串(`ys`,`bh3`)或其组成的数组 | 资源类型,`all`代指所有 | |
162-
| tag | t | `all` | `all`或有效的标签或其组成的数组 (**开发中**) | 一言的标签(tag) | 输入多个值时随机返回符合任意一个条件的资源 |
163-
| format | f | `json` | `js`,`json`,`text` | 返回的格式 | 具体示例参见[接口-一言](#一言)的返回示例部分部分 |
164-
| encoding | e | `utf-8` | 任意[支持的字符编码](https://github.com/ashtuchkin/iconv-lite#supported-encodings) | 返回文本的编码 | |
165-
| seletor | s | `#hitokoto` | `format=js`时,要插入一言的元素的[CSS 选择器](https://developer.mozilla.org/docs/Web/CSS/CSS_Selectors) | |
120+
### 文档
121+
> 敬请期待
166122
167123
## 常见问题(FAQ)
168124

api/hitokoto.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
import iconv from 'iconv-lite';
2-
import { logger, resources } from '../lib/globals.js';
32
import { isCategoryInclude } from '../lib/utils.js';
43

5-
const hitokoto = (req, res, next) => {
4+
const hitokoto = ({ resources }, req, res, next) => {
65
const {
76
category = 'all',
87
format = 'json',
98
encoding = 'utf-8',
109
seletor = '#hitokoto',
1110
} = req.parsedParams;
1211

13-
// check encoding
12+
// Check encoding
1413
if (!iconv.encodingExists(encoding)) {
15-
res.status(400).send(`Unsupported encoding:${encoding}`);
16-
logger.debug(
17-
`[ERROR] ${req.method} ${req.baseUrl}${req.url} Unsupport encoding:${encoding}!`,
18-
);
14+
res.status(400);
15+
next(`Unsupported encoding: ${encoding}`);
1916
}
2017

21-
// selete a random hitokoto
18+
// Selete a random hitokoto
2219
const hitokotos = [];
2320
Object.entries(resources[req.baseUrl]).forEach(([c, h]) => {
2421
if (isCategoryInclude(c, category)) {
2522
hitokotos.push(...h.map((obj) => ({ ...obj, category: c })));
2623
}
2724
});
2825
if (hitokotos.length === 0) {
29-
res.status(500).send('No matching hitokoto found');
30-
logger.debug(
31-
`[ERROR] ${req.method} ${req.baseUrl}${req.url} No matching hitokoto found!`,
32-
);
26+
res.status(500);
27+
next('No matching hitokoto found');
3328
return;
3429
}
3530
const result = hitokotos[Math.floor(Math.random() * hitokotos.length)];
3631

37-
// send response
32+
// Send response
3833
switch (format) {
3934
case 'json':
4035
res
@@ -57,13 +52,10 @@ const hitokoto = (req, res, next) => {
5752
);
5853
break;
5954
default:
60-
res.status(400).send(`Invalid format: ${format}`);
61-
logger.debug(`[ERROR] Invalid format: ${format}!`);
55+
res.status(400);
56+
next(`Invalid format: ${format}`);
6257
break;
6358
}
64-
65-
// writing log
66-
logger.info(`${req.method} ${req.baseUrl}${req.url} ${res.statusCode}`);
6759
next();
6860
};
6961

0 commit comments

Comments
 (0)