Files
RSSHub/lib/routes/pingwest/utils.ts
T
dependabot[bot]andTony 07834413f5 chore(deps-dev): bump eslint-plugin-unicorn from 64.0.0 to 65.0.0 (#22219)
* chore(deps-dev): bump eslint-plugin-unicorn from 64.0.0 to 65.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 64.0.0 to 65.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v64.0.0...v65.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 65.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: no-array-from-fill

* fix: unicorn-js/no-duplicate-set-values

* fix: no-this-outside-of-class

* fix: no-unnecessary-nested-ternary

* fix: prefer-array-last-methods

* fix: prefer-queue-microtask

* fix: prefer-split-limit

* fix: no-array-from-fill

* fix: no-unused-array-method-return

* fix: prefer-single-call

* fix: prefer-includes-over-repeated-comparisons

* fix: consistent-compound-words

* fix: prefer-includes-over-repeated-comparisons

* chore: add eslint-plugin-unicorn v65 recommended preset

* fix: correct duration parsing logic in parseDuration function

* fix: update oxlint-disable

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-09 00:08:15 +08:00

91 lines
3.4 KiB
TypeScript

import { load } from 'cheerio';
import got from '@/utils/got';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
const modifiedLink = (link) => (link.startsWith('http') ? link : `https:${link}`);
const statusListParser = ($) => {
const items = $('.item')
.toArray()
.map((ele) => {
const timestamp = ele.attribs.pt;
const $item = load(ele);
const mainNode = $item('.news-detail');
const imgsStr = mainNode
.find('img')
.toArray()
.map((ele) => `<img src="${ele.attribs.src.split('?x-', 1)[0]}">`)
.join('<br>');
const link = mainNode.find('.content a').first().attr('href');
const content = mainNode
.text()
.trim()
.replace(/展开全文$/, '');
return {
title: content,
link: link.startsWith('http') ? link : `https:${link}`,
description: [content, imgsStr].filter((s) => !!s).join('<br>'),
pubDate: new Date(timestamp * 1000).toUTCString(),
};
});
return items;
};
const articleListParser = async ($, needFullText, cache) => {
const items = await Promise.all(
$('.item')
.toArray()
.map(async (ele) => {
const $item = load(ele);
let titleNode, authorNode;
if ($item('.news-detail').children().length <= 2) {
// newsflash
titleNode = $item('.news-detail .content .text');
authorNode = $item('.news-detail .content .op');
} else {
// article
titleNode = $item('.news-detail .title');
authorNode = $item('.news-detail .author');
}
const title = titleNode.find('a').text();
const prefixUrl = titleNode.find('a').attr('href');
const link = modifiedLink(prefixUrl);
const imgUrl = $item('.news-img img')?.attr('src')?.split('?x-', 1)[0];
const author = authorNode.children().first().text();
const timestamp = authorNode.find('.time').text();
const date = /小时前/.test(timestamp) ? parseRelativeDate(timestamp) : parseDate(timestamp, ['YYYY M D', 'M D']);
let description = $item('.desc').text();
const shortText = imgUrl ? `<br><img src="${imgUrl}">` : '';
description += needFullText ? await getFullArticle(link, cache) : shortText;
return {
title,
link,
description,
author,
pubDate: timezone(date, 8),
};
})
);
return items;
};
const getFullArticle = (link, cache) =>
cache.tryGet(link, async () => {
const { data } = await got(link);
const $ = load(data);
$('img').each((_, ele) => {
if (ele.attribs.src.includes('?x-')) {
ele.attribs.src = ele.attribs.src.split('?x-', 1)[0];
}
});
const description = $('section .article-style').html();
return description;
});
export default { articleListParser, statusListParser };