diff --git a/README.md b/README.md index a462e0b..8f88461 100644 --- a/README.md +++ b/README.md @@ -1,70 +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, // 译文 - from: &str, // 原文语言代码 - to: &str, // 译文语言代码 - needs: HashMap, // 插件需要的其他配置信息,由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 扇贝生词本插件 diff --git a/README_EN.md b/README_EN.md index 748cb84..8406a05 100644 --- a/README_EN.md +++ b/README_EN.md @@ -1,70 +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-`,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 -from: &str, // source language code -to: &str, // target language code -needs: HashMap, // 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 `.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 Shanbay Collection Plugin diff --git a/anki.svg b/anki.svg deleted file mode 100644 index 9f81442..0000000 --- a/anki.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..319c668 Binary files /dev/null and b/icon.png differ diff --git a/info.json b/info.json index 1926bbb..854105f 100644 --- a/info.json +++ b/info.json @@ -1,8 +1,8 @@ { - "id": "[plugin].com.pot-app.anki", + "id": "[plugin].com.pot-app.shanbay", "plugin_type": "collection", - "icon": "anki.svg", - "display": "Anki", - "homepage": "https://github.com/pot-app/pot-app-collection-plugin-template", - "needs": [{ "key": "port", "display": "端口号" }] + "icon": "icon.png", + "display": "扇贝单词本", + "homepage": "https://github.com/pot-app/pot-app-collection-plugin-shanbay", + "needs": [{ "key": "auth_token", "display": "Auth Token" }] } diff --git a/src/lib.rs b/src/lib.rs index 321a001..0c94b4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,25 +1,52 @@ -use serde_json::Value; +use serde_json::{json, Value}; use std::collections::HashMap; use std::error::Error; #[no_mangle] pub fn collection( source: &str, - target: &str, - from: &str, - to: &str, + _target: &str, + _from: &str, + _to: &str, needs: HashMap, ) -> Result> { let client = reqwest::blocking::ClientBuilder::new().build()?; - let port = match needs.get("port") { - Some(port) => port.to_string(), - None => "8765".to_string(), + let token = match needs.get("auth_token") { + Some(token) => token.to_string(), + None => return Err("Please provide token".into()), }; + let body = json!({ + "business_id": 6, + "words":[source] + }); + let res: Value = client + .post("https://apiv3.shanbay.com/wordscollection/words_bulk_upload") + .header("Cookie", format!("auth_token={}", token)) + .header("Content-Type", "application/json;charset=UTF-8") + .json(&body) + .send()? + .json()?; - let url = format!("http://localhost:{port}"); - - // Impl... - + if let Some(json) = res.as_object() { + if let Some(msg) = json.get("msg") { + return Err(msg.as_str().unwrap().to_string().into()); + } + if let Some(id) = json.get("task_id") { + let check_res: Value = client + .get("https://apiv3.shanbay.com/wordscollection/words_bulk_upload") + .query(&[("business_id", "6"), ("task_id", id.as_str().unwrap())]) + .header("Cookie", format!("auth_token={}", token)) + .send()? + .json()?; + if let Some(json) = check_res.as_object() { + if let Some(count) = json.get("failed_count") { + if count.as_u64().unwrap() > 0 { + return Err("Failed to add words".into()); + } + } + } + } + } Ok(Value::Bool(true)) } @@ -29,8 +56,8 @@ mod tests { #[test] fn try_request() { let mut needs = HashMap::new(); - needs.insert("port".to_string(), "8765".to_string()); - let result = collection("Hello", "你好", "en", "zh_cn", needs).unwrap(); - println!("{result}"); + needs.insert("auth_token".to_string(), "".to_string()); + let result = collection("pot", "你好", "en", "zh_cn", needs); + println!("{result:?}"); } }