Skip to content

Commit

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

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
3 changes: 2 additions & 1 deletion i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
cs_cz: require('./cs_cz'),
fr_fr: require('./fr_fr'),
ar_sa: require('./ar_sa'),
de_de: require('./de_de')
de_de: require('./de_de'),
ja_jp: require('./ja_jp')
};
45 changes: 45 additions & 0 deletions i18n/ja_jp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const LANG = 'ja_jp';
const BASE_URL = 'http://home.u05.itscom.net/apodjpn/apodj';

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[0]}${dateArray[1]}/jp${dateArray[0].slice(2)}${dateArray[1]}${dateArray[2]}.html`;
}

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 = $('body > table > tr:nth-child(2) > td:nth-child(2) ').text().trim();
let explanation = $('body > center > p:nth-child(1)').text().trim();

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;
11 changes: 11 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ describe('i18n', function () {
});
});

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

});

0 comments on commit 157e84d

Please sign in to comment.