Skip to content

Commit

Permalink
de_de
Browse files Browse the repository at this point in the history
  • Loading branch information
SSARCandy committed Jun 26, 2016
1 parent fd17a01 commit c5e8743
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ The response data format as follow:
}
```

## Support language:
## Supported language:
- `en_us`: English(default)
- `zh_tw`: Traditional Chinese
- `cs_cz`: Czech
- `fr_fr`: French
- `ar_sa`: Arabic
- `de_de`: German

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
39 changes: 39 additions & 0 deletions i18n/de_de.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const LANG = 'de_de';
const BASE_URL = 'http://www.starobserver.org';

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 craw(baseData, callback) {
let date = baseData.date.replace(/-/g, '').slice(2);
request({
url: `${BASE_URL}/ap${date}.html`,
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 > center > h3').text().trim();
let explanation = $('body > center > p:nth-child(6)').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;
3 changes: 2 additions & 1 deletion i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
en_us: require('./en_us'),
cs_cz: require('./cs_cz'),
fr_fr: require('./fr_fr'),
ar_sa: require('./ar_sa')
ar_sa: require('./ar_sa'),
de_de: require('./de_de')
};
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ APOD.prototype.get = function(options, callback) {
json: true
};

console.time('i');
request(opt, function(error, response, body) {
console.timeEnd('i');
if (handleError(error, response, body)) {
return callback(handleError(error, response, body));
}
Expand Down
11 changes: 11 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ describe('i18n', function () {
});
});

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

});

0 comments on commit c5e8743

Please sign in to comment.