Add glossary methods #4

Open
KaKi87 wants to merge 15 commits from Androz2091/deepl-scraper:glossary-v2 into master
3 changed files with 111 additions and 2 deletions
Showing only changes of commit 8b058b2448 - Show all commits

View file

@ -30,7 +30,7 @@ sudo sysctl -w kernel.unprivileged_userns_clone=1
### Usage ### Usage
```js ```js
const { translate, getSupportedLanguages, quit } = require('deepl-scraper'); const { translate, getSupportedLanguages, setGlossary, quit } = require('deepl-scraper');
translate(sentence, source, target).then(console.log).catch(console.error); translate(sentence, source, target).then(console.log).catch(console.error);
``` ```
@ -87,6 +87,26 @@ getSupportedLanguages().then(console.log);
``` ```
### Glossary
DeepL allows you to define a glossary, so you can add your own words in the translation or influence the words DeepL is choosing.
You can add new words in the glossary using:
```js
setGlossary({
channel: 'salon'
}, 'EN', 'FR');
```
Once a word is added, you can update it by calling the function again:
```js
setGlossary({
channel: 'test'
}, 'EN', 'FR');
```
Note: at this time, the only way to reset the glossary is to quit the browser.
#### Quitting #### Quitting
Since this module uses a headless browser, it won't quit as long as your main script is ronning or until you quit it using the following code : Since this module uses a headless browser, it won't quit as long as your main script is ronning or until you quit it using the following code :

View file

@ -83,6 +83,67 @@ module.exports = {
page.close().catch(() => {}); page.close().catch(() => {});
return _res; return _res;
}, },
clearGlossary: async () => {
const glossaryButton = `.lmt__glossary_onOffSwitch_label`;
const page = await getNewPage();
await page.goto(homepage);
await page.waitForSelector(glossaryButton, { visible: true });
await page.click(glossaryButton);
await page.evaluate(() => {
Array.from(document.querySelectorAll('button[dl-test=glossary-entry-delete-button]')).forEach((element) => {
element.click();
});
});
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await sleep(1000);
},
setGlossary: async (glossary, sourceLanguage, targetLanguage) => {
const
glossaryButton = `.lmt__glossary_onOffSwitch_label`,
langSelect = `.lmt__glossary_newEntry_langButton`,
sourceGlossaryInput = `.lmt__glossary_newEntry_inputSource`,
targetGlossaryInput = `.lmt__glossary_newEntry_inputTarget`,
glossaryAddButton = `.lmt__glossary_acceptButton`;
const page = await getNewPage();
await page.goto(homepage);
await page.waitForSelector(glossaryButton, { visible: true });
await page.click(glossaryButton);
await page.waitForSelector(langSelect, { visible: true });
await page.click(langSelect);
try {
await page.evaluate(({
sourceLanguage,
targetLanguage
}) => {
const langButtons = Array.from(document.querySelectorAll('button[dl-test=glossary-newentry-lang-dropdown-option]'));
const button = langButtons.find((btn) => {
const childrens = Array.from(Array.from(btn.children)[1].children);
const buttonSourceLanguage = childrens[0].textContent;
const buttonTargetLanguage = childrens[2].textContent;
return buttonSourceLanguage === sourceLanguage && buttonTargetLanguage === targetLanguage;
});
button.click();
}, {
sourceLanguage,
targetLanguage
});
}
catch(_){
console.error(_)
throw new Error('UNSUPPORTED_GLOSSARY_LANGUAGE');
}
await page.waitForSelector(sourceGlossaryInput, { visible: true });
const words = Object.keys(glossary);
for (let word of words) {
await page.type(sourceGlossaryInput, word);
await page.type(targetGlossaryInput, glossary[word]);
await page.click(glossaryAddButton);
}
page.close().catch(() => {});
},
quit: async () => { quit: async () => {
if(browser) if(browser)
await browser.close(); await browser.close();

30
test.js
View file

@ -6,7 +6,7 @@
consola = require('consola'), consola = require('consola'),
axios = require('axios'); axios = require('axios');
const { getSupportedLanguages, translate, quit } = require('.'); const { getSupportedLanguages, translate, quit, setGlossary, clearGlossary } = require('.');
const eventCount = { const eventCount = {
total: 0, total: 0,
@ -55,6 +55,34 @@
return error('Target languages are invalid'); return error('Target languages are invalid');
consola.info('Testing glossary');
await setGlossary({
cat: 'poney'
}, 'EN', 'FR');
const result = await translate('I love my cat!', 'en', 'fr-FR');
if(result.target.translation.includes('poney'))
success('Glossary is working');
else
return error('Glossary is not working');
consola.info('Testing clear glossary');
await clearGlossary();
if(!result.target.translation.includes('poney'))
success('Clear glossary is working');
else
return error('Clear glossary is not working');
consola.info('Downloading pangrams'); consola.info('Downloading pangrams');
let pangrams; let pangrams;