|
| 1 | +const cheerio = require('cheerio'); |
| 2 | +const { parseDate } = require('@/utils/parse-date'); |
| 3 | +const got = require('@/utils/got'); |
| 4 | +const timezone = require('@/utils/timezone'); |
| 5 | + |
| 6 | +const { CookieJar } = require('tough-cookie'); |
| 7 | +const cookieJar = new CookieJar(); |
| 8 | + |
| 9 | +const owner = '中央纪委国家监委网站'; |
| 10 | +const rootUrl = 'https://www.ccdi.gov.cn'; |
| 11 | + |
| 12 | +const parseNewsList = async (url, selector, ctx) => { |
| 13 | + const response = await got(url, { cookieJar }); |
| 14 | + const $ = cheerio.load(response.data); |
| 15 | + const list = $(selector) |
| 16 | + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 8) |
| 17 | + .toArray() |
| 18 | + .map((item) => { |
| 19 | + item = $(item); |
| 20 | + return { |
| 21 | + title: item.find('a').first().text().trim(), |
| 22 | + link: new URL(item.find('a').first().attr('href'), url).href, |
| 23 | + pubDate: parseDate(item.find('span').text(), 'YYYY-MM-DD'), |
| 24 | + }; |
| 25 | + }); |
| 26 | + const title = $('.other_Location') |
| 27 | + .text() |
| 28 | + .replace(/(.+)首页/, owner); |
| 29 | + return { list, title }; |
| 30 | +}; |
| 31 | + |
| 32 | +const parseArticle = async (item, ctx) => |
| 33 | + await ctx.cache.tryGet(item.link, async () => { |
| 34 | + const response = await got(item.link, { cookieJar }); |
| 35 | + const $ = cheerio.load(response.data); |
| 36 | + |
| 37 | + const title = $('.daty').text().trim(); |
| 38 | + item.author = title.match(/来源:(.*)发布时间/s)?.[1].trim() ?? owner; |
| 39 | + item.pubDate = timezone(parseDate(title.match(/发布时间:(.*)分享/s)?.[1].trim() ?? item.pubDate), +8); |
| 40 | + |
| 41 | + // Change the img src from relative to absolute for a better compatibility |
| 42 | + $('.content') |
| 43 | + .find('img') |
| 44 | + .each((_, el) => { |
| 45 | + $(el).attr('src', new URL($(el).attr('src'), item.link).href); |
| 46 | + // oldsrc is causing freshrss imageproxy not to work correctly |
| 47 | + $(el).removeAttr('oldsrc').removeAttr('alt'); |
| 48 | + }); |
| 49 | + item.description = $('.content').html(); |
| 50 | + return item; |
| 51 | + }); |
| 52 | + |
| 53 | +module.exports = { |
| 54 | + rootUrl, |
| 55 | + parseNewsList, |
| 56 | + parseArticle, |
| 57 | +}; |
0 commit comments