Skip to content

Commit

Permalink
Merge pull request #17 from tolerious/fix/translation-content
Browse files Browse the repository at this point in the history
🚧 Add try catch.
  • Loading branch information
tolerious authored Sep 22, 2024
2 parents 3216f2b + 60022e4 commit f3bf800
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,29 @@ router.post("/translation/content/", async function (req, res, next) {
console.log(`Original content: ${textToBeTranslated}`);
const url = `https://dict.youdao.com/result?word=${textToBeTranslated}&lang=en`;
let dicList = [];
const a = await axios(url);
try {
const a = await axios(url);

const $ = cheerio.load(a.data);
const dictBook = $(".basic .word-exp");
let phonetic = "";
const yinBiao = $(".phone_con .phonetic");
yinBiao.each((index, element) => {
phonetic += $(element).text() + " ";
});
dictBook.each((index, element) => {
const ciXing = $(element).find(".pos");
const zh = $(element).find(".trans");
dicList.push({ pos: $(ciXing).text(), zh: $(zh).text() });
});
const dictBookSecond = $(".dict-module .trans-container .trans-content");
dicList.push({ pos: "", zh: $(dictBookSecond).text() });
console.log(dicList);
res.json(generateResponse({ dicList, phonetic }));
const $ = cheerio.load(a.data);
const dictBook = $(".basic .word-exp");
let phonetic = "";
const yinBiao = $(".phone_con .phonetic");
yinBiao.each((index, element) => {
phonetic += $(element).text() + " ";
});
dictBook.each((index, element) => {
const ciXing = $(element).find(".pos");
const zh = $(element).find(".trans");
dicList.push({ pos: $(ciXing).text(), zh: $(zh).text() });
});
const dictBookSecond = $(".dict-module .trans-container .trans-content");
dicList.push({ pos: "", zh: $(dictBookSecond).text() });
console.log(dicList);
res.json(generateResponse({ dicList, phonetic }));
} catch (error) {
console.log(error);
res.json(generateResponse({ dicList: [], phonetic: "" }));
}
});

router.post("/grab", async function (req, res, next) {
Expand Down

0 comments on commit f3bf800

Please sign in to comment.