feat: add Hong Kong Observatory Current weather api (#1046)
This commit is contained in:
parent
933f723660
commit
86ca9ff8b5
|
|
@ -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"/>
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
Loading…
Reference in New Issue