Skip to content

Commit

Permalink
first-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fega committed Mar 8, 2017
0 parents commit a1740c6
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["latest"],
"plugins": ["transform-async-to-generator"]
}
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
"extends": "standard",
"plugins": [
"standard",
"promise"
]
};
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
coverage
dist
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- v7
- v6
- v5
- v4
- '0.12'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Fabian Gutierrez <fega.hg@gmail.com> (fabiangutierrez.co)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# wordreference-api [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]
> An api to use the worldreference.com information
## Installation

```sh
$ npm install --save wordreference-api
```

## Usage

```js
var wr = require('wordreference-api');
/**
* wr
* Gets the result for the given word, available languages: 'es', 'en', 'it', 'fr'
* @param {String} word Word to be searched
* @param {String} from from language, default 'en'
* @param {String} to to language, default 'es'
* @return {Object} Object with the word data
*/
wr('Rainbow');
wr('Rainbow','en','fr').then((result)=> console.log);
```
Return:
``` javascript
{
"word": "Rainbow",
"pronWR": "/ˈreɪnˌbəʊ/",
"audio": [
"/audio/en/us/us/en069238.mp3",
"/audio/en/uk/general/en069238.mp3",
"/audio/en/uk/Yorkshire/en069238-55.mp3",
"/audio/en/Irish/en069238.mp3",
"/audio/en/scot/en069238.mp3",
"/audio/en/us/south/en069238.mp3",
"/audio/en/Jamaica/en069238.mp3"
],
"translations": [
{
"title": "Principal Translations",
"translations": [
{
"from": "rainbow",
"fromType": "n",
"toType": "grupo nom",
"to": "arco iris "
}
]
},
{
"title": "Compound Forms:",
"translations": [
{
"from": "rainbow trout",
"fromType": "n",
"toType": "nf",
"to": "trucha arcoiris "
}
]
}
]
}
```
## License

MIT © [Fabian Gutierrez](fabiangutierrez.co)


[npm-image]: https://badge.fury.io/js/wordreference-api.svg
[npm-url]: https://npmjs.org/package/wordreference-api
[travis-image]: https://travis-ci.org/fega/wordreference-api.svg?branch=master
[travis-url]: https://travis-ci.org/fega/wordreference-api
[daviddm-image]: https://david-dm.org/fega/wordreference-api.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/fega/wordreference-api
[coveralls-image]: https://coveralls.io/repos/fega/wordreference-api/badge.svg
[coveralls-url]: https://coveralls.io/r/fega/wordreference-api
83 changes: 83 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var path = require('path');
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var excludeGitignore = require('gulp-exclude-gitignore');
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
var nsp = require('gulp-nsp');
var plumber = require('gulp-plumber');
var coveralls = require('gulp-coveralls');
var babel = require('gulp-babel');
var del = require('del');
var isparta = require('isparta');

// Initialize the babel transpiler so ES2015 files gets compiled
// when they're loaded
require('babel-register');

gulp.task('static', function () {
return gulp.src('**/*.js')
.pipe(excludeGitignore())
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('nsp', function (cb) {
nsp({package: path.resolve('package.json')}, cb);
});

gulp.task('pre-test', function () {
return gulp.src('lib/**/*.js')
.pipe(excludeGitignore())
.pipe(istanbul({
includeUntested: true,
instrumenter: isparta.Instrumenter
}))
.pipe(istanbul.hookRequire());
});

gulp.task('test', ['pre-test'], function (cb) {
var mochaErr;

gulp.src('test/**/*.js')
.pipe(plumber())
.pipe(mocha({reporter: 'spec'}))
.on('error', function (err) {
mochaErr = err;
})
.pipe(istanbul.writeReports())
.on('end', function () {
cb(mochaErr);
});
});

gulp.task('watch', function () {
gulp.watch(['lib/**/*.js', 'test/**'], ['test']);
});

gulp.task('bwatch',['babel'], function () {
gulp.watch(['lib/**/*.js', 'test/**'], ['babel']);
});

gulp.task('coveralls', ['test'], function () {
if (!process.env.CI) {
return;
}

return gulp.src(path.join(__dirname, 'coverage/lcov.info'))
.pipe(coveralls());
});

gulp.task('babel', ['clean'], function () {
return gulp.src('lib/**/*.js')
.pipe(babel())
.pipe(gulp.dest('dist'));
});

gulp.task('clean', function () {
return del('dist');
});

gulp.task('prepublish', ['nsp', 'babel']);
gulp.task('default', ['static', 'test', 'coveralls']);
37 changes: 37 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"word": "Rainbow",
"pronWR": "/ˈreɪnˌbəʊ/",
"audio": [
"/audio/en/us/us/en069238.mp3",
"/audio/en/uk/general/en069238.mp3",
"/audio/en/uk/Yorkshire/en069238-55.mp3",
"/audio/en/Irish/en069238.mp3",
"/audio/en/scot/en069238.mp3",
"/audio/en/us/south/en069238.mp3",
"/audio/en/Jamaica/en069238.mp3"
],
"translations": [
{
"title": "Principal Translations",
"translations": [
{
"from": "rainbow",
"fromType": "n",
"toType": "grupo nom",
"to": "arco iris "
}
]
},
{
"title": "Compound Forms:",
"translations": [
{
"from": "rainbow trout",
"fromType": "n",
"toType": "nf",
"to": "trucha arcoiris "
}
]
}
]
}
29 changes: 29 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const request = require('request-promise-native')
const processHtml = require('./lib/process-html')
const validateLanguage = require('./lib/validate-language')
require('babel-core/register')
require('babel-polyfill')
/**
* Gets the result for the given word
* @param {String} word Word to be searched
* @param {String} from from language, default en
* @param {String} to to language, default es
* @return {Object} Object with the word data
*/

module.exports = async (word, from = 'en', to = 'es') => {
validateLanguage(from)
validateLanguage(to)
// Set the url
var url = `http://www.wordreference.com/${from}${to}/${word}`
// Make the request
var html = await request({
method: 'GET',
uri: url,
headers: {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
})
// Process the HTML
return processHtml(html)
}
64 changes: 64 additions & 0 deletions lib/lib/process-html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const cheerio = require('cheerio')
/**
* Process the html returned from the request and generates the JSON.
* @param {string} html html string to be parsed
* @return {Object} worldreference object
*/
module.exports = (html) => {
var $ = cheerio.load(html)
var result = {}
result.word = $('h3.headerWord').text()
result.pronWR = $('span#pronWR').text()
result.audio = $('div#listen_widget audio source')
.map(function (i, el) {
return $(this).attr('src')
})
.get()

var tables = $('table.WRD')
.map(function (i, el) { // maps each table
return $(this).html()
})
.get()

result.translations = tables.map(WRDtableMap)

// var body = Object.keys($("table.WRD")[0]["attribs"]['class'])
console.log(result)
return result
}
function WRDtableMap (html) {
var $ = cheerio.load(html)
var result = {}
result.title = ''
result.translations = []


$('tr').map(function (i, el) {
var element = $(this)
var id = element.attr('id')
var clss = element.attr('class')

if (clss === 'wrtopsection'){
result.title = element.text()
} else if (id !== undefined && (clss === 'even' || clss === 'even')) {
var V = cheerio.load($(this).html())

var from = V('strong').text()
V('.ToWrd em span').remove()
V('.FrWrd em span').remove()
var fromType = V('.FrWrd em').text()
var toType = V('.ToWrd em').text()
V('.ToWrd em').remove()
var to = V('.ToWrd').text()
var item = {
from,
fromType,
toType,
to
}
result.translations.push(item)
}
})
return result
}
6 changes: 6 additions & 0 deletions lib/lib/validate-language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (lang) => {
if(['es', 'en', 'it', 'fr'].indexOf(lang) != -1)
return lang
else
throw Error('Invalid language')
}
Loading

0 comments on commit a1740c6

Please sign in to comment.