Skip to content

Commit

Permalink
refactor: remove openai/youdao/dictionaryapi support (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Sep 11, 2024
1 parent 9f17e1f commit 796282a
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 590 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/chat-cr.yml

This file was deleted.

1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lint-staged
59 changes: 2 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Fanyi

A 🇨🇳 and 🇺🇸🇬🇧 translate tool in your command line, powered by youdao/iciba/ChatGPT.
A 🇨🇳 and 🇺🇸🇬🇧 translate tool in your command line, powered by iciba.

[![NPM version](https://img.shields.io/npm/v/fanyi.svg?style=flat-square)](https://npmjs.org/package/fanyi) [![NPM downloads](http://img.shields.io/npm/dm/fanyi.svg?style=flat-square)](https://npmjs.org/package/fanyi)

Expand Down Expand Up @@ -30,8 +30,6 @@ $ fy word

Translation data is fetched from [iciba.com](https://iciba.com) and [fanyi.youdao.com](https://fanyi.youdao.com), and only support translation between Chinese and English.

In Mac/Linux bash, words will be pronounced by `say` command.

Translate one word.

```bash
Expand Down Expand Up @@ -109,13 +107,6 @@ Use subcommand `fanyi config [options]`
Example:
```bash
# Turn off the pronunciation
$ fanyi config --no-say
# or
$ fanyi config -S

# Disable the dictionaryapi
$ fanyi config --no-dictionaryapi
# or
$ fanyi config -D
```
Expand All @@ -125,52 +116,6 @@ A sample `~/.config/fanyi/.fanyirc` file:
```json
{
"iciba": true,
"youdao": true,
"dictionaryapi": false,
"say": false,
"color": true,
"OPENAI_API_KEY": "YOUR_OPENAI_API_KEY"
}
```
## Enable ChatGPT 🚀
Set an [OpenAI API key](https://platform.openai.com/overview) to enable ChatGPT translation.
```bash
$ fanyi config --openai-api-key sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```
![image](https://user-images.githubusercontent.com/507615/225946548-8d912643-9f81-401e-abdd-ba5b54912ea4.png)
Turn off ChatGPT translation.
```bash
$ fanyi config --openai-api-key false
```
If we get this error:
> Cannot reach OpenAI api, please check network
We need to ensure that the proxy and $http_proxy is correctly set.
For example(for clash X):
```bash
$ export http_proxy=http://127.0.0.1:7890
```
You can also set up other openai api hosts,such as [api.chatanywhere.com.cn](https://github.com/chatanywhere/GPT_API_free):
```bash
$ fanyi config --openai-api-host api.chatanywhere.com.cn
```
## Error: spawn festival ENOENT
Try this workaround from [say.js](https://github.com/Marak/say.js#linux-notes) in Linux.
```
sudo apt-get install festival festvox-kallpc16k
```
```
93 changes: 0 additions & 93 deletions bin/fanyi

This file was deleted.

76 changes: 76 additions & 0 deletions bin/fanyi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node

const program = require("commander");
const chalk = require("chalk");
const updateNotifier = require("update-notifier");
const pkg = require("../package.json");
const config = require("../lib/config");
const { searchList } = require("../lib/searchHistory");

updateNotifier({ pkg }).notify();

program.version(pkg.version);

program
.command("config")
.description("Set the global options")
.option("-c, --color", "Output with color")
.option("-C, --no-color", "Output without color")
.option("-i, --iciba", "Enable the iciba translation engine")
.option("-I, --no-iciba", "Disable the iciba translation engine")
.action((args) => {
// hack
// If the input is "fanyi config", then translate the word config.
if (process.argv.length === 3) {
return runFY();
}
const { color, iciba } = args;
const options = resolveOptions({ color, iciba });
return config.write(options);
});

program
.command("list")
.option("-d, --someDay <char>", "查看指定某天的查询记录")
.option("-r, --recentDays [number]", "查看最近几天内的数据", 0)
.option("-all --show-file [boolean]", "查看全部数据,即单词存放的位置", false)
.action((args) => {
searchList(args);
});

program.on("--help", () => {
console.log("");
console.log(chalk.gray("Examples:"));
console.log(`${chalk.cyan(" $ ")}fanyi word`);
console.log(`${chalk.cyan(" $ ")}fanyi world peace`);
console.log(`${chalk.cyan(" $ ")}fanyi chinglish`);
console.log("");
});

program.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
}

async function runFY(options = {}) {
const defaultOptions = await config.load();
const mergedOptions = { ...defaultOptions, ...options };
const fanyi = require("..");
fanyi(program.args.join(" "), mergedOptions);
}

function resolveOptions(options) {
const opts = {};
const filteredKeys = Object.keys(options).filter(
(key) => isBoolean(options[key]) || typeof options[key] === "string",
);
for (const key of filteredKeys) {
opts[key] = options[key];
}
return opts;
}

function isBoolean(val) {
return typeof val === "boolean";
}
Loading

0 comments on commit 796282a

Please sign in to comment.