Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-fei committed Mar 26, 2020
1 parent a8cc8a5 commit 61da785
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
41 changes: 33 additions & 8 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
#!/usr/bin/env node
const { browser: { createBrowser } } = require('mega-scraper')
const pLimit = require('p-limit')
const limit = pLimit(3)
const translate = require('.')

main()

async function main () {
const browser = await createBrowser({ headless: true })
const toTranslate = [{
text: 'ciao, come stai?', from: 'it', to: 'en', browser
text: 'ciao, come stai?', from: 'it', to: 'en'
}, {
text: 'hey', from: 'en', to: 'es', browser
text: 'hey', from: 'en', to: 'es'
}, {
text: 'one apple a day keeps the doctor away', from: 'en', to: 'de', browser
text: 'You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use', from: 'en', to: 'es'
}, {
text: 'one apple a day keeps the doctor away', to: 'de', browser
text: 'Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation', from: 'en', to: 'de'
}, {
text: 'If the repair center was able to receive the repair order, it will remain there until repair services resume', from: 'en', to: 'fr'
}, {
text: 'one apple a day keeps the doctor away', from: 'en', to: 'de'
}, {
text: 'This file contains additional information, probably added from the digital camera or scanner used to create or digitize it.', from: 'en', to: 'it'
}, {
text: 'one apple a day keeps the doctor away', to: 'de'
}, {
text: 'You can tap on the cards to expand them and see additional information. Expanding the weather widget, for instance, will display a five-day forecast, with additional prompts to save you the trouble of asking Assistant a question with your voice. Expanding a calendar reminder, meanwhile, allows you to see the description of an upcoming event and a list of attendees.', to: 'it'
}, {
text: 'But you can still find some decent trade in values across the web. It wouldn’t do much good for us to share and compare all of the exact dollar amounts in this post', to: 'fr'
}, {
text: 'Spider-Man garnered awards and nominations in a variety of categories with particular praise for its story, characters, performances, and music', to: 'de'
}, {
text: 'Spider-Man appeared on several lists of the top video games of 2018, being ranked in first place by Wired;[19] second place by Apple Daily and Time.', to: 'pl'
}, {
text: 'Ubinas is a steep cone with a prominent notch on the southern side. The most active volcano in Peru, it has a history of small- to moderate-sized explosive eruptions and persistent degassing and ash emissions', to: 'it'
}]
for (const params of toTranslate) {
// for (const params of toTranslate) {
// console.log(`Translating "${params.text}" from "${params.from || 'auto'}" to "${params.to}"`)
// const translation = await translate(params)
// console.log(`-> "${translation}"`)
// }

await Promise.all(toTranslate.map(params => limit(async () => {
console.log(`Translating "${params.text}" from "${params.from || 'auto'}" to "${params.to}"`)
const translation = await translate(params)
console.log(`-> "${translation}"`)
}
})))

process.exit(0)
}
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ if (require.main === module) {
module.exports = translate
}

async function translate ({ text, from = 'auto', to = 'en', browser } = {}) {
async function translate ({ text, from = 'auto', to = 'en', browser, page } = {}) {
if (!text) throw new Error('missing text')
if (!from) throw new Error('missing from')
if (!to) throw new Error('missing to')
if (!browser) browser = await createBrowser({ headless: true })

const page = await browser.newPage(`https://translate.google.com/#view=home&op=translate&sl=${from}&tl=${to}&text=${encodeURIComponent(text)}`)
await page.waitForSelector('.tlid-translation')
page = page || await browser.newPage()
await page.goto(`https://translate.google.com/#view=home&op=translate&sl=${from}&tl=${to}&text=${encodeURIComponent(text)}`)
await page.waitForSelector('.tlid-translation', { timeout: 10000 })

const translation = await page.evaluate(() => document.querySelector('.tlid-translation').textContent)
await page.close()
await browser.instance.close()

return translation
}
6 changes: 6 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ test('translates text to english (auto detect language) (for free)', async t =>
const translation = await translate({ text: 'ein apfel am tag hält den arzt fern', to: 'en', browser })
t.is(translation, 'one apple a day keeps the doctor away')
})

test('translates given a page instance', async t => {
const page = await browser.newPage()
const translation = await translate({ text: 'ciao, come stai?', from: 'it', to: 'en', page })
t.is(translation, 'Hello how are you?')
})
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"yargs": "^15.3.1"
},
"devDependencies": {
"ava": "^2.4.0"
"ava": "^2.4.0",
"p-limit": "^2.2.2"
},
"scripts": {
"test": "ava"
Expand Down

0 comments on commit 61da785

Please sign in to comment.