-
Notifications
You must be signed in to change notification settings - Fork 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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,116 @@ | ||
| import { load } from 'cheerio'; | ||
|
|
||
| import type { Route } from '@/types'; | ||
| import cache from '@/utils/cache'; | ||
| import got from '@/utils/got'; | ||
| 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 = ['公司动态', '最新资讯']; | ||
|
|
||
TonyRL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return item; | ||
| } catch { | ||
| // 如果获取详情失败,返回基本图片信息 | ||
TonyRL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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`, | ||
| }; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.