Files
RSSHub/lib/routes/hko/weather.js
2019-05-15 15:27:15 +08:00

50 lines
1.3 KiB
JavaScript

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 = await ctx.cache.get(url);
if (cache) {
return JSON.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));
ctx.state.data = result;
};