Add glossary methods #4

Open
KaKi87 wants to merge 15 commits from Androz2091/deepl-scraper:glossary-v2 into master
3 changed files with 170 additions and 3 deletions

View file

@ -30,7 +30,14 @@ sudo sysctl -w kernel.unprivileged_userns_clone=1
### Usage
```js
const { translate, getSupportedLanguages, quit } = require('deepl-scraper');
const {
translate,
getSupportedLanguages,
setGlossary,
clearGlossary,
getSupportedGlossaryLanguages,
quit
} = require('deepl-scraper');
translate(sentence, source, target).then(console.log).catch(console.error);
```
@ -87,6 +94,63 @@ 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
await setGlossary({
'channel': 'salon'
}, 'en', 'fr');
```
Once a word is added, you can update it by calling the function again:
```js
await setGlossary({
'channel': 'test'
}, 'en', 'fr');
```
Glossary is cleared when you quit the browser, although it can be cleared earlier using the following method :
```js
await clearGlossary();
```
As glossary doesn't support all translation-supported languages, you can get the list using the following method :
```js
await getSupportedGlossaryLanguages();
```
```json
[
{
"sourceLanguage": "en",
"targetLanguage": "de"
},
{
"sourceLanguage": "de",
"targetLanguage": "en"
},
{
"sourceLanguage": "en",
"targetLanguage": "fr"
},
{
"sourceLanguage": "fr",
"targetLanguage": "en"
},
{
"sourceLanguage": "en",
"targetLanguage": "es"
},
{
"sourceLanguage": "es",
"targetLanguage": "en"
}
]
```
#### 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 :

View file

@ -11,6 +11,10 @@ const getBrowser = async () => {
const getNewPage = async () => await (await getBrowser()).newPage();
const
glossaryButton = `.lmt__glossary_onOffSwitch_label`,
glossaryLangSelect = `.lmt__glossary_newEntry_langButton`;
module.exports = {
translate: async (sentence, sourceLanguage = 'auto', targetLanguage) => {
if(!/^(auto|[a-z]{2})$/.test(sourceLanguage))
@ -80,9 +84,58 @@ module.exports = {
await page.waitForSelector('.lmt--active_translation_request');
await page.waitForSelector('.lmt--active_translation_request', { hidden: true });
_res.target.translation = await page.$eval(targetSentenceField, el => el.value);
page.close().catch(() => {});
await page.close();
return _res;
},
clearGlossary: async () => {
const page = await getNewPage();
await page.goto(homepage);
await page.waitForSelector(glossaryButton, { visible: true });
await page.click(glossaryButton);
for(const button of await page.$$('button[dl-test=glossary-entry-delete-button]'))
await (await page.$('button[dl-test=glossary-entry-delete-button]')).click();
},
setGlossary: async (glossary, sourceLanguage, targetLanguage) => {
sourceLanguage = sourceLanguage.toUpperCase();
targetLanguage = targetLanguage.toUpperCase();
const
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(glossaryLangSelect, { visible: true });
await page.click(glossaryLangSelect);
{
let isSet = false;
for(const button of await page.$$('button[dl-test=glossary-newentry-lang-dropdown-option]')){
const
_sourceLanguage = await (await (await button.$('.langCode')).getProperty('textContent')).jsonValue(),
_targetLanguage = await (await (await button.$('.langCode:nth-of-type(2)')).getProperty('textContent')).jsonValue();
if(sourceLanguage === _sourceLanguage && targetLanguage === _targetLanguage){
await button.click();
isSet = true;
break;
}
}
if(!isSet)
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);
}
await page.close();
},
quit: async () => {
if(browser)
await browser.close();
@ -118,6 +171,28 @@ module.exports = {
}
}
}
return res;
},
getSupportedGlossaryLanguages: async () => {
const page = await getNewPage();
await page.goto(homepage);
await page.waitForSelector(glossaryButton, { visible: true });
await page.click(glossaryButton);
await page.waitForSelector(glossaryLangSelect, { visible: true });
await page.click(glossaryLangSelect);
const res = [];
for(const button of await page.$$('button[dl-test=glossary-newentry-lang-dropdown-option]')){
res.push({
sourceLanguage: (await (await (await button.$('.langCode')).getProperty('textContent')).jsonValue()).toLowerCase(),
targetLanguage: (await (await (await button.$('.langCode:nth-of-type(2)')).getProperty('textContent')).jsonValue()).toLowerCase()
});
}
await page.close();
return res;
}
};

30
test.js
View file

@ -6,7 +6,7 @@
consola = require('consola'),
axios = require('axios');
const { getSupportedLanguages, translate, quit } = require('.');
const { getSupportedLanguages, translate, quit, setGlossary, clearGlossary } = require('.');
const eventCount = {
total: 0,
@ -55,6 +55,34 @@
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');
let pangrams;