mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-09 06:30:40 +08:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
const got = require('@/utils/got');
|
|
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 got({ 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;
|
|
};
|