Skip to content

Commit

Permalink
Merge branch 'baobao1270:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
HunYuan2333 authored Oct 15, 2023
2 parents 502e32c + 8b8159e commit eec8f34
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 72 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ jobs:
with:
python-version: '3.10'
- run: npm i -g wrangler
- run: python3 make.py
- run: python3 make.py --publish
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: 171f09ad4e5e98bf4de28d465e116904
- uses: softprops/action-gh-release@v1
with:
files: |
dist/fortune-lyric-banner.bash
dist/fortune-lyric.bash
dist/fortune-lyric-minified.json
dist/fortune-lyric.sh
dist/fortune-lyric-multiline.sh
dist/fortune-lyric.json
dist/fortune-lyric-source.txt
dist/fortune-lyric.min.json
dist/fortune-lyric.txt
dist/database.txt
17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Lint Database
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
name: Lint Database
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: npm i -g wrangler
- run: python3 make.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
.DS_Store
67 changes: 36 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,41 @@

## 使用方法

支持的使用方法包括:
- 安装在电脑中,通过命令行输入 `fortune-lyric`,该命令将从数据库中随机选取一个歌词输出。(需要安装 bash;Windows 需要单独安装 bash,Linux/macOS 一般自带)
- 也可以配置为 MOTD,在每次打开终端或登陆 SSH 时,显示随机歌词。
- 通过网页 API 调用,在自己的博客或 App 中,嵌入随机歌词。(请遵守下文 #[版权](#版权) 一节的相关规定)
- 在自己的程序中嵌入数据库,自行实现随机歌词。(请遵守下文 #[版权](#版权) 一节的相关规定)

### 在 bash 中使用
Fortune Lyric 支持多种使用方式:
- **在 Bash 中使用**:直接输出歌词、作为 SSH Banner (MOTD) 或通过管道输出给其他程序。
- **通过 API 调用**:适用于个人网站等网页场景。
- **下载数据库**:在自己的程序中嵌入数据库,自行实现随机歌词。

无论您使用何种方式,都应遵守本项目的版权规定。请参见 [版权](#版权) 一节。

您可以通过下面的链接获取最新的发布版本,如果 GitHub 下载过慢,请使用我们的 API 下载。

| GitHub Release | API | Description |
| -------------------------------- | ------------------------- | --------------------- |
| [fortune-lyric.sh] | [bash] | 单行歌词 Shell 脚本 |
| [fortune-lyric-multiline.sh] | [bash-multiline] | 多行歌词 Shell 脚本 |
| [fortune-lyric.json] | - | JSON 格式 |
| [fortune-lyric.min.json] | [json] | JSON 格式(最小化) |
| [fortune-lyric.txt] | [text] | 单行文本格式 |
| - | GET https://lty.vc/lyric | API 调用接口 |

[fortune-lyric.sh]: https://github.com/luotianyi-dev/fortune-lyric/releases/latest/download/fortune-lyric.sh
[fortune-lyric-multiline.sh]: https://github.com/luotianyi-dev/fortune-lyric/releases/latest/download/fortune-lyric-multiline.sh
[fortune-lyric.json]: https://github.com/luotianyi-dev/fortune-lyric/releases/latest/download/fortune-lyric.json
[fortune-lyric.min.json]: https://github.com/luotianyi-dev/fortune-lyric/releases/latest/download/fortune-lyric.min.json
[fortune-lyric.txt]: https://github.com/luotianyi-dev/fortune-lyric/releases/latest/download/fortune-lyric.txt
[database.txt]: https://github.com/luotianyi-dev/fortune-lyric/releases/latest/download/database.txt
[bash]: https://lty.vc/lyric/bash
[bash-multiline]: https://lty.vc/lyric/bash-multiline
[json]: https://lty.vc/lyric/json
[text]: https://lty.vc/lyric/text
[database]: https://lty.vc/lyric/database

### 在 Bash 中使用

**单行歌词版:**
```bash
curl -L https://lty.vc/lotd/bash -o /usr/local/bin/fortune-lyric && chmod +x /usr/local/bin/fortune-lyric
curl -L https://lty.vc/lyric/bash -o /usr/local/bin/fortune-lyric && chmod +x /usr/local/bin/fortune-lyric
```
显示效果如:
```
Expand All @@ -32,7 +56,7 @@ $ fortune-lyric

**多行歌词版:**
```bash
curl -L https://lty.vc/lotd/banner-bash -o /usr/local/bin/fortune-lyric && chmod +x /usr/local/bin/fortune-lyric
curl -L https://lty.vc/lyric/bash-multiline -o /usr/local/bin/fortune-lyric && chmod +x /usr/local/bin/fortune-lyric
```
显示效果如:
```
Expand All @@ -59,35 +83,16 @@ curl -L <安装地址> -o /etc/update-motd.d/25-fortune-lyric && chmod +x /etc/u
```

### 通过网页调用
直接打开 https://lty.vc/lotd 即可获得随机歌词。
直接打开 https://lty.vc/lyric 即可获得随机歌词。

```
$ curl -L lty.vc/lotd
$ curl -L lty.vc/lyric
思念的含义在无尽生命中淡去
帷幕落下喝彩响起 片刻后都沉寂
```

### 下载数据库
请在 GitHub Release 页面下载数据库文件。


| Release File | Description |
| ----------------------------- | ---------------------- |
| `fortune-lyric.json` | JSON 格式 |
| `fortune-lyric-minified.json` | JSON 格式(最小化) |
| `fortune-lyric.txt` | 单行文本格式 |
| `fortune-lyric-source.txt` | 人类可读 (源代码) 格式 |


如果 GitHub 下载过慢,也可以使用我们的镜像:

| URL | Description |
| --------------------------------- | ---------------------- |
| https://lty.vc/lotd/json | JSON 格式 |
| https://lty.vc/lotd/minified-json | JSON 格式(最小化) |
| https://lty.vc/lotd/txt | 单行文本格式 |
| https://lty.vc/lotd/source-txt | 人类可读 (源代码) 格式 |
详细的 API 文档请参见 [API 文档](https://docs.lty.vc/)

## 数据库格式与约定
数据收录要求为:
Expand Down
97 changes: 61 additions & 36 deletions make.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
import sys
import json
import shutil

import argparse

class FILENAMES:
SOURCE_DATABASE = 'database.txt'
TEMPLATE_SHELL_SCRIPT = 'template.sh'
DIST_SHELL = 'dist/fortune-lyric.sh'
DIST_SHELL_MULTILINE = 'dist/fortune-lyric-multiline.sh'
DIST_JSON = 'dist/fortune-lyric.json'
DIST_JSON_MINIFIED = 'dist/fortune-lyric.min.json'
DIST_TEXT = 'dist/fortune-lyric.txt'
DIST_DB = 'dist/database.txt'

def colored(color: str, text: str) -> str:
colors = {
Expand Down Expand Up @@ -35,8 +45,16 @@ def print_file_size(filename: str):


def load():
with open('database.txt', encoding='utf-8') as f:
database = f.read().strip()
with open(FILENAMES.SOURCE_DATABASE, "rb") as f:
if b'\r' in f.read():
print(colored('red', 'Error:'), '数据库文件中不得包含 CR (\\r) 字符')
sys.exit(-2)
with open(FILENAMES.SOURCE_DATABASE, encoding='utf-8') as f:
database = f.read()
if not database.endswith('\n'):
print(colored('red', 'Error:'), '数据库文件应该以 LF (\\n) 结尾')
sys.exit(-2)
database = database.strip()
database = [i.strip() for i in database.split('-' * 30)]
if (database[0] == '') or (database[-1] == ''):
print(colored('red', 'Error:'), '遇到了空的歌词块 (quoteblock)')
Expand Down Expand Up @@ -104,25 +122,25 @@ def load():


def build_json(database: list[dict]):
os.makedirs('dist', exist_ok=True)
with open('dist/fortune-lyric.json', 'w+', encoding='utf-8') as f:
os.makedirs(os.path.dirname(FILENAMES.DIST_JSON), exist_ok=True)
with open(FILENAMES.DIST_JSON, 'w+', encoding='utf-8') as f:
f.write(json.dumps(database, ensure_ascii=False, indent=4))
print_file_size('dist/fortune-lyric.json')
with open('dist/fortune-lyric-minified.json', 'w+', encoding='utf-8') as f:
f.write(json.dumps(database, ensure_ascii=False))
print_file_size('dist/fortune-lyric-minified.json')
print_file_size(FILENAMES.DIST_JSON)
with open(FILENAMES.DIST_JSON_MINIFIED, 'w+', encoding='utf-8') as f:
f.write(json.dumps(database, ensure_ascii=True))
print_file_size(FILENAMES.DIST_JSON_MINIFIED)


def build_plain(database: list[dict]):
def build_text(database: list[dict]):
lines = [
' '.join(quote['lines']).replace(' ', '\u3000')
for quote in database
]
lines = '\n'.join(lines)
os.makedirs('dist', exist_ok=True)
with open('dist/fortune-lyric.txt', 'w+', encoding='utf-8') as f:
os.makedirs(os.path.dirname(FILENAMES.DIST_TEXT), exist_ok=True)
with open(FILENAMES.DIST_TEXT, 'w+', encoding='utf-8') as f:
f.write(lines)
print_file_size('dist/fortune-lyric.txt')
print_file_size(FILENAMES.DIST_TEXT)


def build_bash(database: list[dict]):
Expand All @@ -131,55 +149,62 @@ def build_bash(database: list[dict]):
for quote in database
]
lines = '\n'.join(lines)
os.makedirs('dist', exist_ok=True)
with open('tmpl.sh', encoding='utf-8') as f:
os.makedirs(os.path.dirname(FILENAMES.DIST_SHELL), exist_ok=True)
with open(FILENAMES.TEMPLATE_SHELL_SCRIPT, encoding='utf-8') as f:
tmpl = f.read().strip()
with open('dist/fortune-lyric.bash', 'w+', encoding='utf-8') as f:
with open(FILENAMES.DIST_SHELL, 'w+', encoding='utf-8') as f:
f.write(tmpl.replace('%%DATABASE%%', lines) + "\n")
print_file_size('dist/fortune-lyric.bash')
os.chmod('dist/fortune-lyric.bash', 0o755)
print_file_size(FILENAMES.DIST_SHELL)
os.chmod(FILENAMES.DIST_SHELL, 0o755)


def build_bash_banner(database: list[dict]):
def build_bash_multiline(database: list[dict]):
lines = [
" \"" + '\\n'.join(quote['lines']) + "\\n\\n" +
(" " * (max([len(i.encode("gb18030")) for i in quote['lines']]) - 8)) +
f"-- " + "/".join(quote['author']) + f"《{quote['title']}》 ({quote['year']})\\n" + "\""
for quote in database
]
lines = '\n'.join(lines)
os.makedirs('dist', exist_ok=True)
with open('tmpl.sh', encoding='utf-8') as f:
os.makedirs(os.path.dirname(FILENAMES.DIST_SHELL_MULTILINE), exist_ok=True)
with open(FILENAMES.TEMPLATE_SHELL_SCRIPT, encoding='utf-8') as f:
tmpl = f.read().strip()
with open('dist/fortune-lyric-banner.bash', 'w+', encoding='utf-8') as f:
with open(FILENAMES.DIST_SHELL_MULTILINE, 'w+', encoding='utf-8') as f:
f.write(tmpl.replace('%%DATABASE%%', lines) + "\n")
print_file_size('dist/fortune-lyric-banner.bash')
os.chmod('dist/fortune-lyric-banner.bash', 0o755)
print_file_size(FILENAMES.DIST_SHELL_MULTILINE)
os.chmod(FILENAMES.DIST_SHELL_MULTILINE, 0o755)


def build_copy():
shutil.copy('database.txt', 'dist/fortune-lyric-source.txt')
print_file_size('dist/fortune-lyric-source.txt')
shutil.copy(FILENAMES.SOURCE_DATABASE, FILENAMES.DIST_DB)
print_file_size(FILENAMES.DIST_DB)


def build_cloudflare_kv_publish(namespace_id: str):
if not os.path.exists("dist/fortune-lyric.json"):
print(colored('red', 'Error:'),
'dist/fortune-lyric.json 不存在,'
'build_cloudflare_kv_publish() 必须在 build_json() 之后调用')
def build_publish(namespace_id: str):
if not os.path.exists(FILENAMES.DIST_JSON):
print(colored('red', 'Error:'), f"文件 {FILENAMES.DIST_JSON} 不存在, build_publish 必须在 build_json 之后执行")
sys.exit(-2)
result = os.system(f"wrangler kv:key put --namespace-id {namespace_id} fortune-lyric --path dist/fortune-lyric.json")
print(colored('yellow', 'Info:'), "命令返回值:", result)
result = os.system(f"wrangler kv:key put --namespace-id {namespace_id} fortune-lyric --path {FILENAMES.DIST_JSON}")
if result != 0:
print(colored('yellow', 'Info:'), "命令返回值:", result)
print(colored('red', 'Error:'), 'wrangler kv:key put 执行失败')
sys.exit(-2)
print(colored('green', 'Success:'), 'wrangler kv:key put 执行成功')


if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--publish', action='store_true', help='发布到 Cloudflare KV')
args = parser.parse_args()

if not args.publish:
print(colored('yellow', 'Info:'), '如需发布到 Cloudflare KV,请使用 --publish 参数。')

database = load()
build_json(database)
build_plain(database)
build_text(database)
build_bash(database)
build_bash_banner(database)
build_bash_multiline(database)
build_copy()
build_cloudflare_kv_publish("0addc370401c4b77b57c9d40fddf9ad6")
if args.publish:
build_publish("0addc370401c4b77b57c9d40fddf9ad6")
File renamed without changes.

0 comments on commit eec8f34

Please sign in to comment.