Skip to content

Commit 9ff06c6

Browse files
committed
fix(route/alwayscontrol): correct import order to pass ESLint check
- Move cheerio import to first line (external dependencies first) - Add blank line after external imports - Keep type imports before other internal imports - Maintain alphabetical order for @/ imports This fixes the ESLint import/order error that was blocking PR #20522
1 parent 28b56e5 commit 9ff06c6

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

lib/routes/alwayscontrol/news.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { load } from 'cheerio';
2+
3+
import type { Route } from '@/types';
4+
import cache from '@/utils/cache';
5+
import got from '@/utils/got';
6+
import { parseDate } from '@/utils/parse-date';
7+
8+
const baseUrl = 'https://www.alwayscontrol.com.cn';
9+
10+
export const route: Route = {
11+
path: '/news',
12+
categories: ['other'],
13+
example: '/alwayscontrol/news',
14+
features: {
15+
requireConfig: false,
16+
requirePuppeteer: false,
17+
antiCrawler: false,
18+
supportBT: false,
19+
supportPodcast: false,
20+
supportScihub: false,
21+
},
22+
name: '最新动态',
23+
maintainers: ['moss-xxh'],
24+
url: 'alwayscontrol.com.cn',
25+
handler,
26+
radar: [
27+
{
28+
source: ['www.alwayscontrol.com.cn/zh-CN/news/list'],
29+
target: '/news',
30+
},
31+
],
32+
description: 'Always Control(旭衡电子)智能能源管理系统解决方案专家的最新动态',
33+
};
34+
35+
async function handler() {
36+
const listUrl = `${baseUrl}/zh-CN/news/list`;
37+
38+
// 获取新闻列表页面
39+
const response = await got(listUrl);
40+
const $ = load(response.data);
41+
42+
// 解析新闻列表
43+
const list = $('article')
44+
.toArray()
45+
.map((item) => {
46+
const $item = $(item);
47+
const title = $item.find('h2').text().trim();
48+
const date = $item.find('time').text().trim();
49+
const link = $item.find('a').attr('href');
50+
const image = $item.find('img').attr('src');
51+
52+
return {
53+
title,
54+
link: link ? `${baseUrl}${link}` : '',
55+
pubDate: parseDate(date, 'YYYY-MM-DD'),
56+
image: image ? (image.startsWith('http') ? image : `${baseUrl}${image}`) : '',
57+
};
58+
});
59+
60+
// 获取每篇新闻的详细内容
61+
const items = await Promise.all(
62+
list.map((item) =>
63+
cache.tryGet(item.link, async () => {
64+
if (!item.link) {
65+
return item;
66+
}
67+
68+
try {
69+
const detailResponse = await got(item.link);
70+
const $detail = load(detailResponse.data);
71+
72+
// 处理图片URL(相对路径转绝对路径)
73+
$detail('article img').each((_, elem) => {
74+
const $img = $detail(elem);
75+
const src = $img.attr('src');
76+
if (src && src.startsWith('/')) {
77+
$img.attr('src', `${baseUrl}${src}`);
78+
}
79+
});
80+
81+
// 移除所有 class、style 等属性,但保留 src、href、alt
82+
$detail('article *').each((_, elem) => {
83+
const $elem = $detail(elem);
84+
const allowedAttrs = new Set(['src', 'href', 'alt', 'title']);
85+
const attrs = Object.keys(elem.attribs || {});
86+
87+
for (const attr of attrs) {
88+
if (!allowedAttrs.has(attr)) {
89+
$elem.removeAttr(attr);
90+
}
91+
}
92+
});
93+
94+
item.description = $detail('article').html() || '';
95+
item.author = '旭衡电子(深圳)有限公司';
96+
item.category = ['公司动态', '最新资讯'];
97+
98+
return item;
99+
} catch {
100+
// 如果获取详情失败,返回基本图片信息
101+
item.description = item.image ? `<img src="${item.image}">` : '';
102+
return item;
103+
}
104+
})
105+
)
106+
);
107+
108+
return {
109+
title: 'Always Control - 最新动态',
110+
link: listUrl,
111+
description: 'Always Control(旭衡电子)- 智能能源管理系统解决方案专家最新动态',
112+
language: 'zh-CN',
113+
item: items,
114+
image: `${baseUrl}/logo.png`,
115+
};
116+
}

0 commit comments

Comments
 (0)