Skip to content

Commit

Permalink
feat: YouDao WordList
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Sep 24, 2023
1 parent a12899b commit c04c8f1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 153 deletions.
69 changes: 1 addition & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1 @@
# Pot-App 生词本插件模板仓库 (以 [Anki](https://apps.ankiweb.net) 为例)

### [English](./README_EN.md) | 简体中文

### 此仓库为模板仓库,编写插件时可以直接由此仓库创建插件仓库

## 插件编写指南

### 1. 插件仓库创建

- 以此仓库为模板创建一个新的仓库
- 仓库名为 `pot-app-collection-plugin-<插件名>`,例如 `pot-app-collection-plugin-anki`

### 2. 插件信息配置

编辑 `info.json` 文件,修改以下字段:

- `id`:插件唯一 id,必须以`[plugin]`开头,例如 `[plugin].com.pot-app.anki`
- `homepage`: 插件主页,填写你的仓库地址即可,例如 `https://github.com/pot-app/pot-app-collection-plugin-template`
- `display`: 插件显示名称,例如 `Anki`
- `icon`: 插件图标,例如 `anki.svg`
- `needs`: 插件依赖,一个数组,每个依赖为一个对象,包含以下字段:
- `key`: 依赖 key,对应该项依赖在配置文件中的名称,例如 `port`
- `display`: 依赖显示名称,对应用户显示的名称,例如 `端口号`

### 3. 插件编写/编译

编辑 `src/lib.rs` 实现 `collection` 函数

#### 输入参数

```rust
source: &str, // 原文
target: &str, // 译文
needs: HashMap<String, String>, // 插件需要的其他配置信息,由info.json定义
```

#### 返回值

```rust
// 返回用Value包裹的true
return Ok(Value::Bool(true));
```

#### 测试/编译

```bash
cargo test --package plugin --lib -- tests --nocapture # 运行测试用例
cargo build --release # 编译
```

### 4. 打包 pot 插件

1.`target/release`目录找到`plugin.dll`(Windows)/`libplugin.dylib`(MacOS)/`libplugin.so`(Linux)文件,统一删除`lib`前缀.

2.`plugin.dll`/`libplugin.dylib`/`libplugin.so`文件和`info.json`以及图标文件压缩为 zip 文件。

3. 将文件重命名为`<插件id>.potext`,例如`[plugin].com.pot-app.anki.potext`,即可得到 pot 需要的插件。

## 自动编译打包

本仓库配置了 Github Actions,可以实现推送后自动编译打包插件。

每次将仓库推送到 GitHub 之后 actions 会自动运行,将打包好的插件上传到 artifact,在 actions 页面可以下载

每次提交 Tag 之后,actions 会自动运行,将打包好的插件上传到 release,在 release 页面可以下载打包好的插件

> 注意需要在仓库设置中添加一个名为`TOKEN`的 secret,值为一个有权限的 GitHub Token,用于上传 release
# Pot-App 的 有道单词本插件
69 changes: 1 addition & 68 deletions README_EN.md
Original file line number Diff line number Diff line change
@@ -1,68 +1 @@
# Pot-App Collection Plugin Template Repository (Take [Anki](https://apps.ankiweb.net) for example)

### English | [简体中文](./README.md)

### This repository is a template repository. You can directly create a plugin repository from this repository when writing plugins

## Plugin Writing Guide

### 1. Create a plugin repository

- Create a new repository using this repository as a template
- Named `pot-app-collection-plugin-<name>`,Eg: `pot-app-collection-plugin-anki`

### 2. Plugin information configuration

Edit the `info.json` file and modify the following fields:

- `id`: The unique ID of the plugin, which must start with `[plugin]`, for example `[plugin].com.pot-app.anki`
- `homepage`: The homepage of the plugin, simply fill in your repository address, for example `https://github.com/pot-app/pot-app-collection-plugin-template`
- `display`: The display name of the plugin, for example `Anki`
- `icon`: The icon of the plugin, for example `anki.svg`
- `needs`: Dependencies required by the plugin. It is an array where each dependency is an object that includes the following fields:
- `key`: The key of the dependency, corresponding to its name in the configuration file. For example,`port`.
- `display`: The display name of the dependency as shown to users. For example,`Port`.

### 3. Plugin writing/compiling

Edit `src/lib.rs` to implement `collection` function

#### Input parameters

```rust
source: &str, // source text
target: &str, // target text
needs: HashMap<String, String>, // Additional configuration information required by the plugin, defined by info.json
```

#### Return value

```rust
// Return the true wrapped by Value
return Ok(Value::Bool(true))
```

#### Test/Compile

```bash
cargo test --package plugin --lib -- tests --nocapture # run the test case
cargo build --release # Compile
```

### 4. Package pot Plugin

1. Find the `plugin.dll` (Windows)/`libplugin.dylib` (MacOS)/`libplugin.so` (Linux) file in the `target/release` directory and delete the prefix `lib`.

2. Compress the `plugin.dll`/`libplugin.dylib`/`libplugin.so`, with the `info.json` and icon files, into a zip file.

3. Rename the file as `<plugin id>.potext`, for example `[plugin].com.pot-app.anki.potext`, to obtain the plugin required by pot.

## Automatic Compilation and Package

This repository is configured with Github Actions, which allows for automatic compilation and packaging of plugins after pushing.

Every time the commit is pushed to GitHub, actions will run automatically and upload the packaged plugin to artifacts. The packaged plugin can be downloaded from the actions page.

After each tag submission, actions will also run automatically and upload the packaged plugin to releases. The packaged plugin can be downloaded from the release page.

> Please note that you need to add a secret named `TOKEN` in the repository settings. The value should be a GitHub Token with appropriate permissions, which will be used for uploading releases.
# Pot-App YouDao WordList Plugin
1 change: 0 additions & 1 deletion anki.svg

This file was deleted.

10 changes: 5 additions & 5 deletions info.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "[plugin].com.pot-app.anki",
"id": "[plugin].com.pot-app.youdao_wordlist",
"plugin_type": "collection",
"icon": "anki.svg",
"display": "Anki",
"homepage": "https://github.com/pot-app/pot-app-collection-plugin-template",
"needs": [{ "key": "port", "display": "端口号" }]
"icon": "youdao.svg",
"display": "有道单词本",
"homepage": "https://github.com/pot-app/pot-app-collection-plugin-youdao",
"needs": [{ "key": "cookie", "display": "Cookie" }]
}
35 changes: 24 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,33 @@ use std::error::Error;
#[no_mangle]
pub fn collection(
source: &str,
to: &str,
_to: &str,
needs: HashMap<String, String>,
) -> Result<Value, Box<dyn Error>> {
let client = reqwest::blocking::ClientBuilder::new().build()?;
let port = match needs.get("port") {
Some(port) => port.to_string(),
None => "8765".to_string(),
let cookie = match needs.get("cookie") {
Some(cookie) => cookie.to_string(),
None => return Err("cookie is required".into()),
};

let url = format!("http://localhost:{port}");

// Impl...

Ok(Value::Bool(true))
let res: Value = client
.get("https://dict.youdao.com/wordbook/webapi/v2/ajax/add")
.header("Cookie", cookie)
.query(&[("word", source), ("lan", "en")])
.send()?
.json()?;
if let Some(json) = res.as_object() {
if let Some(code) = json.get("code") {
if code.as_i64().unwrap() == 0 {
return Ok(Value::Bool(true));
} else {
if let Some(msg) = json.get("msg") {
return Ok(Value::String(msg.as_str().unwrap().to_string()));
}
}
}
}
Ok(Value::Bool(false))
}

#[cfg(test)]
Expand All @@ -27,8 +40,8 @@ mod tests {
#[test]
fn try_request() {
let mut needs = HashMap::new();
needs.insert("port".to_string(), "8765".to_string());
let result = collection("Hello", "你好", needs).unwrap();
needs.insert("cookie".to_string(), "".to_string());
let result = collection("Hello", "en", needs).unwrap();
println!("{result}");
}
}
1 change: 1 addition & 0 deletions youdao.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c04c8f1

Please sign in to comment.