Skip to content

Commit

Permalink
Merge pull request #12 from SSARCandy/i18n/de_de
Browse files Browse the repository at this point in the history
I18n/de_de
  • Loading branch information
SSARCandy authored Jun 26, 2016
2 parents fd17a01 + 601c714 commit 09dbf77
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 10 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
6 changes: 4 additions & 2 deletions i18n/ar_sa.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const LANG = 'ar_sa';
const BASE_URL = 'http://www.apodar.com/apod';

const handleError = require('../utils/handleError').common;
const notFoundError = require('../utils/handleError').notFound;
const request = require('request');
Expand All @@ -25,12 +27,12 @@ function craw(baseData, callback) {
let explanation = $('body > div.region4 > div:nth-child(1) > div > div').text().trim();

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

baseData.title = title;
baseData.explanation = explanation;
baseData.lang = 'ar_sa';
baseData.lang = LANG;
callback(null, baseData);
});
}
Expand Down
6 changes: 4 additions & 2 deletions i18n/cs_cz.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const LANG = 'cs_cz';
const BASE_URL = 'http://www.astro.cz/apod';

const handleError = require('../utils/handleError').common;
const notFoundError = require('../utils/handleError').notFound;
const request = require('request');
Expand All @@ -25,12 +27,12 @@ function craw(baseData, callback) {
let explanation = $('article.apod > p').eq(1).text().trim().replace(/\r?\n/g, ' ');

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

baseData.title = title;
baseData.explanation = explanation;
baseData.lang = 'cs_cz';
baseData.lang = LANG;
callback(null, baseData);
});
}
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;
6 changes: 4 additions & 2 deletions i18n/fr_fr.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const LANG = 'fr_fr';
const BASE_URL = 'http://www.cidehom.com/apod.php';

const handleError = require('../utils/handleError').common;
const notFoundError = require('../utils/handleError').notFound;
const request = require('request');
Expand All @@ -24,12 +26,12 @@ function craw(baseData, callback) {
let explanation = $('body > div > div:nth-child(6) > div.article_colonne_gauche > div > p').text().trim().replace(/\r?\n/g, ' ');

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

baseData.title = title;
baseData.explanation = explanation;
baseData.lang = 'fr_fr';
baseData.lang = LANG;
return callback(null, baseData);
});
}
Expand Down
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')
};
6 changes: 4 additions & 2 deletions i18n/zh_tw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const LANG = 'zh_tw';
const BASE_URL = 'http://www.phys.ncku.edu.tw/~astrolab/mirrors/apod';

const handleError = require('../utils/handleError').common;
const notFoundError = require('../utils/handleError').notFound;
const request = require('request');
Expand All @@ -24,12 +26,12 @@ function craw(baseData, callback) {
let explanation = $('body').children('p').eq(0).text().trim();

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

baseData.title = title;
baseData.explanation = explanation;
baseData.lang = 'zh_tw';
baseData.lang = LANG;
return callback(null, baseData);
});
}
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('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 09dbf77

Please sign in to comment.