From cdb3e9a1d6cae32d40f6fd23827ed4d916a624c9 Mon Sep 17 00:00:00 2001 From: wangwentao Date: Mon, 10 Jun 2024 00:24:22 +0800 Subject: [PATCH] fix: Youdao translator has only one result --- package.json | 12 +++++++-- rollup.config.js | 6 +++++ src/adapters/adapter.ts | 2 +- src/adapters/baidu.ts | 2 +- src/adapters/youdao.ts | 54 +++++++++++++++++++++++++++++++++++++---- src/translator.ts | 2 +- tsconfig.json | 1 + 7 files changed, 69 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 11d26e4..c2f3391 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "youdao.translator", - "version": "3.0.0", + "version": "3.1.1", "description": "Elegant translation tool", "main": "index.js", "scripts": { @@ -12,9 +12,17 @@ "devDependencies": { "rimraf": "^3.0.2", "rollup": "^2.59.0", + "rollup-plugin-commonjs": "^10.1.0", "rollup-plugin-copy": "^3.4.0", + "rollup-plugin-json": "^4.0.0", + "rollup-plugin-node-resolve": "^5.2.0", "rollup-plugin-ts": "^1.4.7", "rollup-plugin-uglify": "^6.0.4", - "typescript": "^4.4.4" + "typescript": "^4.5.5" + }, + "dependencies": { + "@types/boolbase": "^1.0.3", + "@types/cheerio": "^0.22.35", + "cheerio": "^1.0.0-rc.12" } } diff --git a/rollup.config.js b/rollup.config.js index 0131946..99133fc 100755 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,6 +1,9 @@ import ts from 'rollup-plugin-ts' import copy from 'rollup-plugin-copy' import { uglify } from 'rollup-plugin-uglify' +import nodeResolve from 'rollup-plugin-node-resolve' +import commonjs from 'rollup-plugin-commonjs'; +import json from 'rollup-plugin-json' export default { input: 'src/index.ts', @@ -16,6 +19,9 @@ export default { { src: 'runtime/*', dest: 'dist/runtime' }, { src: 'assets/*', dest: 'dist/assets' } ]}), + nodeResolve(), + commonjs(), + json(), uglify() ] } \ No newline at end of file diff --git a/src/adapters/adapter.ts b/src/adapters/adapter.ts index ce1d92f..fdf64a7 100755 --- a/src/adapters/adapter.ts +++ b/src/adapters/adapter.ts @@ -17,6 +17,6 @@ export interface Adapter { url :(word: string) => string; - parse: (response: any) => Result[] + parse: (response: any) => Promise } diff --git a/src/adapters/baidu.ts b/src/adapters/baidu.ts index f114898..fa180b9 100755 --- a/src/adapters/baidu.ts +++ b/src/adapters/baidu.ts @@ -42,7 +42,7 @@ class Baidu implements Adapter { return "https://fanyi-api.baidu.com/api/trans/vip/translate?" + params.toString(); } - parse(data: any): Result[] { + async parse(data: any): Promise { if (data.error_code) { return this.parseError(data.error_code); } diff --git a/src/adapters/youdao.ts b/src/adapters/youdao.ts index 60c246d..d95d42d 100755 --- a/src/adapters/youdao.ts +++ b/src/adapters/youdao.ts @@ -1,5 +1,6 @@ import { Adapter, Result } from "./adapter"; import md5 from "../libs/md5"; +import cheerio from "cheerio"; class Youdao implements Adapter { key: string; @@ -40,16 +41,14 @@ class Youdao implements Adapter { return "https://openapi.youdao.com/api?" + params.toString(); } - parse(data: any): Result[] { + async parse(data: any): Promise { if (data.errorCode !== "0") { return this.parseError(data.errorCode); } - const { translation, basic, web } = data; - + const { translation, webdict, web } = data; this.parseTranslation(translation); - this.parseBasic(basic); - this.parseWeb(web); + await this.parseWebdict(webdict); return this.results; } @@ -77,6 +76,51 @@ class Youdao implements Adapter { } } + // Thanks for @VWagen1989's solution + // https://github.com/whyliam/whyliam.workflows.youdao/issues/125#issuecomment-2119263993 + private async parseWebdict(t: any) { + var url = t && t["url"] + await fetch(url) + .then(response => { + if (response.ok) { + return response.text(); + } + throw new Error('Network response was not ok.'); + }) + .then(html => { + const $ = cheerio.load(html); + $('div.content-wrp.dict-container.opened').each((i, el) => { + this.parseResultItems($(el), $); + }) + }) + .catch(error => { + console.error('There has been a problem with your fetch operation:', error); + }); + } + + private parseResultItems(item, $) { + let e = this.word + // Tricky: It will just get the first trans-container. + // Because the others will not be available until you click the button to expand. + const tc = item.find('div[class^="trans-container"]'); + + let phonetics: string[] = []; + tc.find('span.phonetic').each((i, el) => { + const label = $(el).parent('span').contents().first().text().trim(); + const phoneticText = $(el).text().trim(); + phonetics.push(`${label} ${phoneticText}`); + }); + const phoneticsCombined = phonetics.join('; '); + if (phoneticsCombined != '') { + this.addResult(phoneticsCombined, "回车可听发音", e, e); + } + + // Extract translation results + item.find('ul li, ul a.clickable').each((i, el) => { + this.addResult($(el).text().trim(), this.word, e, e); + }); + } + private parseWeb(web: any) { if (web) { web.forEach((item, index) => { diff --git a/src/translator.ts b/src/translator.ts index b4de996..fe322b5 100755 --- a/src/translator.ts +++ b/src/translator.ts @@ -24,7 +24,7 @@ class Translator implements ITranslator{ // fetch const response = await redaxios.create().get(url); // parse - const result = this.adapter.parse(response.data); + const result = await this.adapter.parse(response.data); // compose return new Workflow().compose(result).output(); } diff --git a/tsconfig.json b/tsconfig.json index cda74b5..bf6bed5 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,7 @@ "strict": true, "esModuleInterop": false, "outDir": "out-tsc", + "allowSyntheticDefaultImports": true, "rootDir": "./" }, "include": [