diff --git a/docs/README.md b/docs/README.md index d9c776d09..8af524a8d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2177,3 +2177,7 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运 ### 百度 + +### 香港天文台 + + diff --git a/router.js b/router.js index 116448848..254660110 100644 --- a/router.js +++ b/router.js @@ -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')); diff --git a/routes/hko/weather.js b/routes/hko/weather.js new file mode 100644 index 000000000..0a1fad7ce --- /dev/null +++ b/routes/hko/weather.js @@ -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; +};