-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4ac805
commit 9325565
Showing
5 changed files
with
5,053 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env node | ||
const megaScraper = require('mega-scraper') | ||
const translate = require('.') | ||
|
||
main() | ||
|
||
async function main () { | ||
const browser = await megaScraper.browser.createBrowser({ headless: true }) | ||
const toTranslate = [{ | ||
text: 'ciao, come stai?', from: 'it', to: 'en', browser | ||
}, { | ||
text: 'hey', from: 'en', to: 'es', browser | ||
}, { | ||
text: 'one apple a day keeps the doctor away', from: 'en', to: 'de', browser | ||
}, { | ||
text: 'one apple a day keeps the doctor away', to: 'de', browser | ||
}] | ||
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}"`) | ||
} | ||
process.exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
#!/usr/bin/env node | ||
const { browser: { createBrowser } } = require('mega-scraper') | ||
|
||
if (require.main === module) { | ||
main() | ||
} else { | ||
module.exports = main | ||
} | ||
module.exports = translate | ||
|
||
async function translate ({ text, from = 'auto', to, browser } = {}) { | ||
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') | ||
|
||
const translation = await page.evaluate(() => document.querySelector('.tlid-translation').textContent) | ||
await page.close() | ||
|
||
async function main () { | ||
console.log('main') | ||
return translation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { serial: test } = require('ava') | ||
const { browser: { createBrowser } } = require('mega-scraper') | ||
const translate = require('.') | ||
|
||
let browser | ||
test.before(async () => { | ||
browser = await createBrowser({ headless: true }) | ||
}) | ||
test.after(async () => { | ||
await browser.instance.close() | ||
}) | ||
|
||
test('translates text from italian to english (for free)', async t => { | ||
const translation = await translate({ text: 'ciao, come stai?', from: 'it', to: 'en', browser }) | ||
t.is(translation, 'Hello how are you?') | ||
}) |
Oops, something went wrong.