Files
RSSHub/lib/v2/embassy/index.js
Mengxin Cao a428c4c5cd fix(route): broken link embassy/us (#9673)
* Fix broken link embassy/us

* refactor: migrate to v2

* fix: embassy/us selector
2022-05-01 21:27:20 +08:00

61 lines
1.7 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const supportedList = require('./supportedList');
module.exports = async (ctx) => {
const country = ctx.params.country;
const city = ctx.params.city ?? undefined;
let config = supportedList[country.toLowerCase()];
let desc;
if (city === undefined) {
desc = `中国驻${config.countryCN}大使馆 -- 重要通知`;
} else {
config = config.consulates[city.toLowerCase()];
desc = `中国驻${config.cityCN}领事馆 -- 重要通知`;
}
const link = config.link;
const hostname = new URL(link).hostname;
const res = await got(link);
const $ = cheerio.load(res.data);
const list = [];
$(config.list)
.slice(0, 10)
.each((i, e) => {
const temp = new URL($(e).attr('href'), link);
if (temp.hostname === hostname) {
list.push(temp);
}
});
const out = await Promise.all(
list.map((link) =>
ctx.cache.tryGet(link.href, async () => {
const response = await got(link);
const $ = cheerio.load(response.data);
const single = {
title: $(config.title).text(),
link,
description: $(config.description).html(),
pubDate: parseDate($(config.pubDate).text().replace('(', '').replace(')', '')),
};
return single;
})
)
);
ctx.state.data = {
title: desc,
description: desc,
link,
item: out,
};
};