* style: add eslint-unicorn * style: fix unicorn/no-useless-spread * style: fix unicorn/no-useless-promise-resolve-reject * style: fix unicorn/no-for-loop * fix: codeql bad HTML filtering regexp * fix: codeql incomplete replace * fix: unicorn/no-abusive-eslint-disable * style: fix unicorn/no-new-array * style: fix unicorn/no-typeof-undefined * style: fix unicorn/no-zero-fractions * style: fix unicorn/no-empty-file * style: fix unicorn/prefer-date-now * revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js * style: fix unicorn/prefer-array-find * style: fix unicorn/prefer-array-flat * style: fix unicorn/prefer-array-flat-map * style: fix unicorn/prefer-at * style: fix unicorn/prefer-string-starts-ends-with * style: fix unicorn/prefer-includes * fix: codeql URL substring sanitization * style: fix unicorn/prefer-optional-catch-binding * style: fix unicorn/catch-error-name * style: fix unicorn/escape-case * style: fix unicorn/prefer-native-coercion-functions * style: fix unicorn/prefer-regexp-test * style: fix unicorn/require-array-join-separator * style: fix unicorn/prefer-math-trunc * style: fix unicorn/prefer-negative-index * style: fix unicorn/prefer-dom-node-dataset * style: fix unicorn/prefer-dom-node-text-content * style: fix unicorn/prefer-query-selector * style: fix unicorn/no-array-for-each * style: fix unicorn/no-negated-condition * style: fix unicorn/prefer-add-event-listener * style: fix unicorn/import-style * style: fix prefer-regex-literals * style: disable unicorn/no-useless-switch-case * style: disable unicorn/text-encoding-identifier-case * style: fix unicorn/prefer-set-has * style: fix unicorn/prefer-spread * revert: auto fix on lib/routes/universities/ynnu/edu/base64.js * style: fix unicorn/no-useless-undefined * style: fix unicorn/no-array-push-push * style: fix unicorn/no-useless-undefined again * style: fix unicorn/no-lonely-if * style: fix unicorn/prefer-reflect-apply * style: fix unicorn/switch-case-braces * style: fix unicorn/prefer-switch * style: fix unicorn/prefer-array-some * fix: deepscan UNUSED_VAR_ASSIGN * style: fix unicorn/prefer-ternary * fix: follow-up of unicorn/prefer-ternary * revert: auto fix of unicorn/prefer-string-slice for substring() * style: disable unicorn/prefer-string-slice fix: auto fix slice over deprecated substr * style: fix unicorn/throw-new-error * style: fix unicorn/filename-case * test: fix dateParser renaming * style: fix unicorn/better-regex * style: fix unicorn/prefer-string-replace-all * fix(deps): add sanitize-html * style: fix no-prototype-builtins * style: fix unicorn/consistent-destructuring * style: fix unicorn/consistent-function-scoping * style: fix unicorn/prefer-regexp-test * style: fix unicorn/prefer-logical-operator-over-ternary * style: fix unicorn/no-array-callback-reference * style: add prefer-object-has-own * style: warn unicorn/no-empty-file * style: fix unicorn/prefer-number-properties * style: fix no-useless-undefined again * style: fix unicorn/numeric-separators-style * style: disable unicorn/no-array-callback-reference false postive with cheerio
95 lines
3.3 KiB
JavaScript
95 lines
3.3 KiB
JavaScript
const cheerio = require('cheerio');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
const got = require('@/utils/got');
|
|
const timezone = require('@/utils/timezone');
|
|
|
|
const { CookieJar, Cookie } = require('tough-cookie');
|
|
const cookieJar = new CookieJar();
|
|
|
|
const owner = '中央纪委国家监委网站';
|
|
const rootUrl = 'https://www.ccdi.gov.cn';
|
|
const regex = /(?<key>[A-Z_]+)=(?<value>(?:.*?(?=; max-age)|[\dA-Fa-f]+))/gm;
|
|
|
|
const parseCookie = async (body) => {
|
|
let m;
|
|
const cookies = [];
|
|
while ((m = regex.exec(body)) !== null) {
|
|
// This is necessary to avoid infinite loops with zero-width matches
|
|
if (m.index === regex.lastIndex) {
|
|
regex.lastIndex++;
|
|
}
|
|
const { key, value } = m.groups;
|
|
cookies.push(new Cookie({ key, value }));
|
|
}
|
|
await Promise.all(cookies.map((c) => cookieJar.setCookie(c, rootUrl)));
|
|
};
|
|
|
|
const parseNewsList = async (url, selector, ctx) => {
|
|
const response = await got(url, { cookieJar });
|
|
const data = response.data;
|
|
await parseCookie(data);
|
|
|
|
const $ = cheerio.load(data);
|
|
const list = $(selector)
|
|
.slice(0, ctx.query.limit ? Number.parseInt(ctx.query.limit) : 20)
|
|
.toArray()
|
|
.map((item) => {
|
|
item = $(item);
|
|
return {
|
|
title: item.find('a').first().text().trim(),
|
|
link: new URL(item.find('a').first().attr('href'), url).href,
|
|
pubDate: parseDate(item.find('.more').text(), 'YYYY-MM-DD'),
|
|
};
|
|
});
|
|
const title = $('.other_Location')
|
|
.text()
|
|
.replace(/(.+)首页/, owner);
|
|
return { list, title };
|
|
};
|
|
|
|
const changeTrCookie = async () => {
|
|
const cookies = await cookieJar.getCookies(rootUrl);
|
|
const c = cookies.find((c) => c.key === 'HOY_TR');
|
|
if (c) {
|
|
const value = c.value;
|
|
const tr_array = value.split(',');
|
|
const csr = tr_array[0];
|
|
const cnv = [...tr_array[1]];
|
|
const otr = [...tr_array[2]];
|
|
otr[0] = csr.charAt(Number.parseInt(cnv[0], 16));
|
|
const nc = new Cookie({ key: 'HOY_TR', value: csr + ',' + cnv.join('') + ',' + otr.join('') + ',0' });
|
|
await cookieJar.setCookie(nc, rootUrl);
|
|
}
|
|
};
|
|
|
|
const parseArticle = async (item, ctx) => {
|
|
await changeTrCookie();
|
|
return ctx.cache.tryGet(item.link, async () => {
|
|
const response = await got(item.link, { cookieJar });
|
|
const data = response.data;
|
|
await parseCookie(data);
|
|
|
|
const $ = cheerio.load(data);
|
|
const title = $('.daty, .source-box').text().trim();
|
|
item.author = title.match(/来源:(.*)发布时间/s)?.[1].trim() ?? owner;
|
|
item.pubDate = timezone(parseDate(title.match(/发布时间:(.*)分享/s)?.[1].trim() ?? item.pubDate), +8);
|
|
|
|
// Change the img src from relative to absolute for a better compatibility
|
|
$('.content, .bom-box')
|
|
.find('img')
|
|
.each((_, el) => {
|
|
$(el).attr('src', new URL($(el).attr('src'), item.link).href);
|
|
// oldsrc is causing freshrss imageproxy not to work correctly
|
|
$(el).removeAttr('oldsrc').removeAttr('alt');
|
|
});
|
|
item.description = $('.content, .bom-box').html();
|
|
return item;
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
rootUrl,
|
|
parseNewsList,
|
|
parseArticle,
|
|
};
|