Skip to content

Commit 7a3eada

Browse files
authored
i18n/gl_es (#15)
1 parent 157e84d commit 7a3eada

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The response data format as follow:
4747
- `ar_sa`: Arabic
4848
- `de_de`: German
4949
- `ja_jp`: Japanese
50+
- `gl_es`: Galician
5051

5152
If `LANG` is specified(and is valid), response data will get specified LANG's `title` and `explanation`.
5253
For example, set `LANG` as `Traditional Chinese`:

i18n/gl_es.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const LANG = 'gl_es';
4+
const BASE_URL = 'http://astrogalicia.org/apod';
5+
6+
const handleError = require('../utils/handleError').common;
7+
const notFoundError = require('../utils/handleError').notFound;
8+
const request = require('request');
9+
const cheerio = require('cheerio');
10+
const decoder = require('../utils/utils').decoder;
11+
12+
function buildUrl(date) {
13+
let dateArray = date.split('-');
14+
return `${BASE_URL}/${dateArray[0]}/${dateArray[1]}/${dateArray[2]}`;
15+
}
16+
17+
function craw(baseData, callback) {
18+
let url = buildUrl(baseData.date);
19+
20+
request({
21+
url: url,
22+
encoding: null
23+
}, function(error, response, buf) {
24+
if (handleError(error, response)) {
25+
return callback(handleError(error, response));
26+
}
27+
28+
let decoded = decoder(buf);
29+
30+
let $ = cheerio.load(decoded);
31+
let title = $('article > header > h1 > a').text().trim();
32+
let explanation = $('article > div').text();
33+
let expIdx = explanation.indexOf('Explicaci');
34+
explanation = explanation.slice(expIdx);
35+
36+
if (!title || !explanation) {
37+
return callback(notFoundError(baseData.date, LANG));
38+
}
39+
40+
baseData.title = title;
41+
baseData.explanation = explanation;
42+
baseData.lang = LANG;
43+
return callback(null, baseData);
44+
});
45+
}
46+
47+
module.exports = craw;

i18n/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ module.exports = {
77
fr_fr: require('./fr_fr'),
88
ar_sa: require('./ar_sa'),
99
de_de: require('./de_de'),
10-
ja_jp: require('./ja_jp')
10+
ja_jp: require('./ja_jp'),
11+
gl_es: require('./gl_es')
1112
};

test/i18n.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,15 @@ describe('i18n', function () {
9797
});
9898
});
9999

100+
it('should success with gl_es', function (done) {
101+
apod.get({
102+
DATE: '2016-05-11',
103+
LANG: 'gl_es'
104+
}, function (err, data) {
105+
expect(err).to.be.null;
106+
validateData(data);
107+
done();
108+
});
109+
});
110+
100111
});

0 commit comments

Comments
 (0)