Skip to content

Commit

Permalink
i18n/gl_es (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
SSARCandy committed Jun 26, 2016
1 parent 157e84d commit 7a3eada
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The response data format as follow:
- `ar_sa`: Arabic
- `de_de`: German
- `ja_jp`: Japanese
- `gl_es`: Galician

If `LANG` is specified(and is valid), response data will get specified LANG's `title` and `explanation`.
For example, set `LANG` as `Traditional Chinese`:
Expand Down
47 changes: 47 additions & 0 deletions i18n/gl_es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const LANG = 'gl_es';
const BASE_URL = 'http://astrogalicia.org/apod';

const handleError = require('../utils/handleError').common;
const notFoundError = require('../utils/handleError').notFound;
const request = require('request');
const cheerio = require('cheerio');
const decoder = require('../utils/utils').decoder;

function buildUrl(date) {
let dateArray = date.split('-');
return `${BASE_URL}/${dateArray[0]}/${dateArray[1]}/${dateArray[2]}`;
}

function craw(baseData, callback) {
let url = buildUrl(baseData.date);

request({
url: url,
encoding: null
}, function(error, response, buf) {
if (handleError(error, response)) {
return callback(handleError(error, response));
}

let decoded = decoder(buf);

let $ = cheerio.load(decoded);
let title = $('article > header > h1 > a').text().trim();
let explanation = $('article > div').text();
let expIdx = explanation.indexOf('Explicaci');
explanation = explanation.slice(expIdx);

if (!title || !explanation) {
return callback(notFoundError(baseData.date, LANG));
}

baseData.title = title;
baseData.explanation = explanation;
baseData.lang = LANG;
return callback(null, baseData);
});
}

module.exports = craw;
3 changes: 2 additions & 1 deletion i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = {
fr_fr: require('./fr_fr'),
ar_sa: require('./ar_sa'),
de_de: require('./de_de'),
ja_jp: require('./ja_jp')
ja_jp: require('./ja_jp'),
gl_es: require('./gl_es')
};
11 changes: 11 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@ describe('i18n', function () {
});
});

it('should success with gl_es', function (done) {
apod.get({
DATE: '2016-05-11',
LANG: 'gl_es'
}, function (err, data) {
expect(err).to.be.null;
validateData(data);
done();
});
});

});

0 comments on commit 7a3eada

Please sign in to comment.