-
Notifications
You must be signed in to change notification settings - Fork 8.9k
feat(route): add Always Control news route #20522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moss-xxh
wants to merge
10
commits into
DIYgod:master
Choose a base branch
from
moss-xxh:feat/add-alwayscontrol-route
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dfd5772
feat(route): add Always Control news route
moss-xxh 76fd08b
feat(route): convert to TypeScript and add namespace
moss-xxh ab2bc9d
refactor: 按照审核意见优化 alwayscontrol 路由
moss-xxh a38f32c
fix: 修复审核意见 - 优化 alwayscontrol 路由代码
moss-xxh b20f5a5
fix: 调整 import 语句顺序以符合 ESLint 规范
moss-xxh b06eb0e
fix: 按 ESLint 规则重新排序 import 语句
moss-xxh ee36acd
fix(route/alwayscontrol): resolve import sorting issue
moss-xxh 084e6cd
fix(route/alwayscontrol): fix invalid radar source configuration
moss-xxh 6cc9a01
fix(route/alwayscontrol): fix import order per ESLint rules
moss-xxh baea981
fix(route/alwayscontrol): fix import alphabetical order
moss-xxh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type { Namespace } from '@/types'; | ||
|
|
||
| export const namespace: Namespace = { | ||
| name: 'Always Control(旭衡电子)', | ||
| url: 'alwayscontrol.com.cn', | ||
| lang: 'zh-CN', | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import type { Route } from '@/types'; | ||
|
|
||
| import cache from '@/utils/cache'; | ||
| import got from '@/utils/got'; | ||
| import { load } from 'cheerio'; | ||
| import { parseDate } from '@/utils/parse-date'; | ||
|
|
||
| const baseUrl = 'https://www.alwayscontrol.com.cn'; | ||
|
|
||
| export const route: Route = { | ||
| path: '/news', | ||
| categories: ['other'], | ||
| example: '/alwayscontrol/news', | ||
| features: { | ||
| requireConfig: false, | ||
| requirePuppeteer: false, | ||
| antiCrawler: false, | ||
| supportBT: false, | ||
| supportPodcast: false, | ||
| supportScihub: false, | ||
| }, | ||
| name: '最新动态', | ||
| maintainers: ['moss-xxh'], | ||
| url: 'alwayscontrol.com.cn', | ||
| handler, | ||
| radar: [ | ||
| { | ||
| source: ['www.alwayscontrol.com.cn/zh-CN/news/list'], | ||
| target: '/news', | ||
| }, | ||
| ], | ||
| description: 'Always Control(旭衡电子)智能能源管理系统解决方案专家的最新动态', | ||
| }; | ||
|
|
||
| async function handler() { | ||
| const listUrl = `${baseUrl}/zh-CN/news/list`; | ||
|
|
||
| // 获取新闻列表页面 | ||
| const response = await got(listUrl); | ||
| const $ = load(response.data); | ||
|
|
||
| // 解析新闻列表 | ||
| const list = $('article') | ||
| .toArray() | ||
| .map((item) => { | ||
| const $item = $(item); | ||
| const title = $item.find('h2').text().trim(); | ||
| const date = $item.find('time').text().trim(); | ||
| const link = $item.find('a').attr('href'); | ||
| const image = $item.find('img').attr('src'); | ||
|
|
||
| return { | ||
| title, | ||
| link: link ? `${baseUrl}${link}` : '', | ||
| pubDate: parseDate(date, 'YYYY-MM-DD'), | ||
| image: image ? (image.startsWith('http') ? image : `${baseUrl}${image}`) : '', | ||
| }; | ||
| }); | ||
|
|
||
| // 获取每篇新闻的详细内容 | ||
| const items = await Promise.all( | ||
| list.map((item) => | ||
| cache.tryGet(item.link, async () => { | ||
| if (!item.link) { | ||
| return item; | ||
| } | ||
|
|
||
| try { | ||
| const detailResponse = await got(item.link); | ||
| const $detail = load(detailResponse.data); | ||
|
|
||
| // 处理图片URL(相对路径转绝对路径) | ||
| $detail('article img').each((_, elem) => { | ||
| const $img = $detail(elem); | ||
| const src = $img.attr('src'); | ||
| if (src && src.startsWith('/')) { | ||
| $img.attr('src', `${baseUrl}${src}`); | ||
| } | ||
| }); | ||
|
|
||
| // 移除所有 class、style 等属性,但保留 src、href、alt | ||
| $detail('article *').each((_, elem) => { | ||
| const $elem = $detail(elem); | ||
| const allowedAttrs = new Set(['src', 'href', 'alt', 'title']); | ||
| const attrs = Object.keys(elem.attribs || {}); | ||
| for (const attr of attrs) { | ||
| if (!allowedAttrs.has(attr)) { | ||
| $elem.removeAttr(attr); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| item.description = $detail('article').html() || ''; | ||
| item.author = '旭衡电子(深圳)有限公司'; | ||
| item.category = ['公司动态', '最新资讯']; | ||
|
|
||
| return item; | ||
| } catch { | ||
| // 如果获取详情失败,返回基本图片信息 | ||
| item.description = item.image ? `<img src="${item.image}">` : ''; | ||
| return item; | ||
| } | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| return { | ||
| title: 'Always Control - 最新动态', | ||
| link: listUrl, | ||
| description: 'Always Control(旭衡电子)- 智能能源管理系统解决方案专家最新动态', | ||
| language: 'zh-CN', | ||
| item: items, | ||
| image: `${baseUrl}/logo.png`, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / ESLint
Automatically sort imports. Error