feat: add Hong Kong Observatory Current weather api (#1046)

This commit is contained in:
Calpa Liu 2018-11-03 10:52:32 +08:00 committed by DIYgod
parent 933f723660
commit 86ca9ff8b5
3 changed files with 56 additions and 0 deletions

View File

@ -2177,3 +2177,7 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运
### 百度
<route name="百度趣画" author="xyqfer" example="/baidu/doodles" path="/baidu/doodles"/>
### 香港天文台
<route name="Current Weather Report" author="calpa" example="/hko/weather" path="/hko/weather"/>

View File

@ -738,6 +738,9 @@ router.get('/geekpark/breakingnews', require('./routes/geekpark/breakingnews'));
// 百度
router.get('/baidu/doodles', require('./routes/baidu/doodles'));
// 香港天文台
router.get('/hko/weather', require('./routes/hko/weather'));
// sankakucomplex
router.get('/sankakucomplex/post', require('./routes/sankakucomplex/post'));

49
routes/hko/weather.js Normal file
View File

@ -0,0 +1,49 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const url = 'http://rss.weather.gov.hk/rss/CurrentWeather.xml';
const cache = ctx.cache.get(url);
if (cache) {
return Promise.parse(cache);
}
const { data } = await axios({ method: 'get', url });
const $ = cheerio.load(data, {
xmlMode: true,
});
const weather = $('description')
.slice(1, 2)
.text();
const $$ = cheerio.load(weather);
const items = [];
$$('table')
.find('td')
.each(function(index, element) {
if (index % 2) {
return;
}
const area = $$(element).text();
const degree = $$(element)
.next()
.text()
.split(' ')[0];
const item = {
title: area,
description: degree,
};
items.push(item);
});
const result = {
title: 'Current Weather Report',
description: ` provided by the Hong Kong Observatory: ${$('pubDate').text()}`,
link: url,
item: items,
};
// one hour cache
ctx.cache.set(url, JSON.stringify(result), 60 * 60);
ctx.state.data = result;
};