-
Notifications
You must be signed in to change notification settings - Fork 9.4k
feat(apple): add full-text Apple Newsroom route #21384
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
LinxHex
wants to merge
2
commits into
DIYgod:master
Choose a base branch
from
LinxHex:apple-newsroom
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 1 commit
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,310 @@ | ||
| import { load } from 'cheerio'; | ||
| import type { Item } from 'rss-parser'; | ||
|
|
||
| import { config } from '@/config'; | ||
| import type { Route } from '@/types'; | ||
| import { ViewType } from '@/types'; | ||
| import cache from '@/utils/cache'; | ||
| import ofetch from '@/utils/ofetch'; | ||
| import { parseDate } from '@/utils/parse-date'; | ||
| import parser from '@/utils/rss-parser'; | ||
|
|
||
| const baseUrl = 'https://www.apple.com.cn'; | ||
| const rootUrl = `${baseUrl}/newsroom/`; | ||
| const feedUrl = `${rootUrl}rss-feed.rss`; | ||
| const defaultLimit = 20; | ||
| const articleEndSelector = '.nr-article-share, .presscontacts-headline, .articles, .section-headline-container'; | ||
| const galleryControlSelector = '.paddlenav, .paddlenav-arrow, .paddlenav-arrow-next, .paddlenav-arrow-previous, .dotnav, .dotnav-item'; | ||
| const videoCaptionContainerSelector = '.video-description'; | ||
| const mediaCaptionContainerSelector = '.image-description, .gallery-caption, .video-description'; | ||
| const videoControlSelector = '.nr-av-control'; | ||
|
|
||
| const normalizeText = (text: string) => text.replaceAll(/\s+/g, ' ').trim(); | ||
|
|
||
| const getMediaCaptionPrefix = ($, element) => ($(element).is(videoCaptionContainerSelector) ? '视频说明:' : '图片说明:'); | ||
|
|
||
| const resolveUrl = (url: string, pageUrl: string) => { | ||
| if ( | ||
| url.startsWith('#') || | ||
| url.startsWith('data:') || | ||
| url.startsWith('mailto:') || | ||
| url.startsWith('tel:') || | ||
| url.startsWith('javascript:') | ||
| ) { | ||
| return url; | ||
| } | ||
|
|
||
| try { | ||
| return new URL(url, pageUrl).href; | ||
| } catch { | ||
| return url; | ||
| } | ||
| }; | ||
|
|
||
| const resolveSrcset = (srcset: string, pageUrl: string) => | ||
| srcset | ||
| .split(',') | ||
| .map((entry) => { | ||
| const [url, ...descriptors] = entry.trim().split(/\s+/); | ||
|
|
||
| if (!url) { | ||
| return ''; | ||
| } | ||
|
|
||
| return [resolveUrl(url, pageUrl), ...descriptors].join(' '); | ||
| }) | ||
| .filter(Boolean) | ||
| .join(', '); | ||
|
|
||
| const removeDuplicateMediaDescriptions = ($, node, bodyTexts: Set<string>) => { | ||
| const $node = $(node); | ||
|
|
||
| $(mediaCaptionContainerSelector, $node).each((_, element) => { | ||
| const description = $(element); | ||
| const descriptionClone = description.clone(); | ||
|
|
||
| descriptionClone.find('a, button').remove(); | ||
|
|
||
| const descriptionText = normalizeText(descriptionClone.text()); | ||
|
|
||
| if (!descriptionText || bodyTexts.has(descriptionText)) { | ||
| description.remove(); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const formatMediaDescriptions = ($, node) => { | ||
| const $node = $(node); | ||
|
|
||
| $(mediaCaptionContainerSelector, $node).each((_, element) => { | ||
| const description = $(element); | ||
| const captions = description | ||
| .find('.image-caption') | ||
| .toArray() | ||
| .map((caption) => normalizeText($(caption).text())) | ||
| .filter(Boolean); | ||
|
|
||
| if (captions.length === 0) { | ||
| description.remove(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const figcaption = $('<figcaption></figcaption>').html( | ||
| captions.map((caption) => `<small><em>${getMediaCaptionPrefix($, element)}${caption}</em></small>`).join('<br>') | ||
| ); | ||
|
|
||
| description.replaceWith(figcaption); | ||
| }); | ||
| }; | ||
|
|
||
| const removeVideoControls = ($, node) => { | ||
| const $node = $(node); | ||
|
|
||
| $(videoControlSelector, $node).remove(); | ||
| $node.find('.video-container a').each((_, element) => { | ||
| const link = $(element); | ||
| const hasMediaContent = link.find('img, picture, video, source, figcaption').length > 0; | ||
|
|
||
| if (!normalizeText(link.text()) && !hasMediaContent) { | ||
| link.remove(); | ||
| } | ||
| }); | ||
| $node.find('.autoplay-controls-container').each((_, element) => { | ||
| const container = $(element); | ||
|
|
||
| container.replaceWith(container.html() ?? ''); | ||
| }); | ||
| }; | ||
|
|
||
| const absolutizeArticleNode = ($, node, pageUrl: string, bodyTexts: Set<string>) => { | ||
| const $node = $(node); | ||
|
|
||
| if ($node.is(galleryControlSelector)) { | ||
| $node.remove(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $node.find('script, style').addBack('script, style').remove(); | ||
| $(galleryControlSelector, $node).remove(); | ||
| removeVideoControls($, node); | ||
| $node.find('[download]').remove(); | ||
| $node.find('.image-description > div:empty, .gallery-caption > div:empty, .video-description > div:empty').remove(); | ||
| removeDuplicateMediaDescriptions($, node, bodyTexts); | ||
| formatMediaDescriptions($, node); | ||
| $(mediaCaptionContainerSelector, $node).each((_, element) => { | ||
| if (normalizeText($(element).text()) === '' && $(element).children().length === 0) { | ||
| $(element).remove(); | ||
| } | ||
| }); | ||
|
|
||
| $node | ||
| .find('a[href]') | ||
| .addBack('a[href]') | ||
| .each((_, element) => { | ||
| const href = $(element).attr('href'); | ||
|
|
||
| if (href) { | ||
| $(element).attr('href', resolveUrl(href, pageUrl)); | ||
| } | ||
| }); | ||
|
|
||
| $node | ||
| .find('img[src], source[src], video[src]') | ||
| .addBack('img[src], source[src], video[src]') | ||
| .each((_, element) => { | ||
| const src = $(element).attr('src'); | ||
|
|
||
| if (src) { | ||
| $(element).attr('src', resolveUrl(src, pageUrl)); | ||
| } | ||
| }); | ||
|
|
||
| $node | ||
| .find('img[srcset], source[srcset]') | ||
| .addBack('img[srcset], source[srcset]') | ||
| .each((_, element) => { | ||
| const srcset = $(element).attr('srcset'); | ||
|
|
||
| if (srcset) { | ||
| $(element).attr('srcset', resolveSrcset(srcset, pageUrl)); | ||
| } | ||
| }); | ||
|
|
||
| $node | ||
| .find('video[poster]') | ||
| .addBack('video[poster]') | ||
| .each((_, element) => { | ||
| const poster = $(element).attr('poster'); | ||
|
|
||
| if (poster) { | ||
| $(element).attr('poster', resolveUrl(poster, pageUrl)); | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const extractArticleDescription = ($, pageUrl: string) => { | ||
| const article = $('.article').first(); | ||
|
|
||
| if (!article.length) { | ||
| return; | ||
| } | ||
|
|
||
| const articleChildren = article.children().toArray(); | ||
| const headerIndex = articleChildren.findIndex((node) => $(node).is('.article-header')); | ||
| const startIndex = headerIndex === -1 ? 0 : headerIndex + 1; | ||
| const endIndex = articleChildren.findIndex((node, index) => index >= startIndex && $(node).is(articleEndSelector)); | ||
| const contentNodes = articleChildren.slice(startIndex, endIndex === -1 ? undefined : endIndex); | ||
|
|
||
| if (contentNodes.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const bodyTexts = new Set( | ||
| contentNodes | ||
| .flatMap((node) => | ||
| $(node) | ||
| .find('.pagebody-copy') | ||
| .addBack('.pagebody-copy') | ||
| .toArray() | ||
| .map((element) => normalizeText($(element).text())) | ||
| ) | ||
| .filter(Boolean) | ||
| ); | ||
|
|
||
| for (const node of contentNodes) { | ||
| absolutizeArticleNode($, node, pageUrl, bodyTexts); | ||
| } | ||
|
|
||
| return contentNodes.map((node) => $.html(node) ?? '').join(''); | ||
| }; | ||
|
|
||
| const fetchArticle = (item: Item & { link: string }) => | ||
| cache.tryGet(item.link, async () => { | ||
| const response = await ofetch(item.link, { | ||
| headers: { | ||
| 'User-Agent': config.trueUA, | ||
| }, | ||
| parseResponse: (text) => text, | ||
| }); | ||
| const $ = load(response); | ||
| const category = $('.category-eyebrow__category').first().text(); | ||
| const fallbackDate = $('.category-eyebrow__date').first().text(); | ||
| const description = extractArticleDescription($, item.link); | ||
| const ogTitle = $('meta[property="og:title"]').attr('content'); | ||
| const ogImage = $('meta[property="og:image"]').attr('content'); | ||
| const pubDate = item.isoDate ?? item.pubDate; | ||
|
|
||
| return { | ||
| title: ogTitle ?? item.title ?? item.link, | ||
| link: item.link, | ||
| description: description ?? item.content ?? item.contentSnippet, | ||
| pubDate: pubDate ? parseDate(pubDate) : fallbackDate ? parseDate(fallbackDate, 'YYYY 年 M 月 D 日') : undefined, | ||
| author: item.author ?? 'Apple Newsroom', | ||
| category: category ? [category] : item.categories, | ||
| image: ogImage ?? item.enclosure?.url, | ||
| }; | ||
| }); | ||
|
|
||
| async function handler(ctx) { | ||
| const limit = Math.max(Number.parseInt(ctx.req.query('limit') ?? '', 10) || defaultLimit, 1); | ||
| const feedResponse = await ofetch(feedUrl, { | ||
| headers: { | ||
| 'User-Agent': config.trueUA, | ||
| }, | ||
| parseResponse: (text) => text, | ||
| }); | ||
| const feed = await parser.parseString(feedResponse); | ||
| const items = await Promise.all( | ||
| (feed.items as Item[]) | ||
| .slice(0, limit) | ||
| .filter((item): item is Item & { link: string } => Boolean(item.link)) | ||
| .map((item) => fetchArticle(item)) | ||
| ); | ||
|
|
||
| return { | ||
| title: 'Apple Newsroom (中国大陆)', | ||
| link: rootUrl, | ||
| feedLink: feedUrl, | ||
| description: 'Apple 新闻中心是 Apple 新闻的来源。阅读新闻稿、获取最新消息、观看视频和下载图片。', | ||
| item: items, | ||
| language: 'zh-CN', | ||
| }; | ||
| } | ||
|
|
||
| export const route: Route = { | ||
| path: '/newsroom', | ||
| name: 'Apple Newsroom (中国大陆)', | ||
| url: 'www.apple.com.cn/newsroom', | ||
| maintainers: ['LinxHex'], | ||
| example: '/apple/newsroom', | ||
| description: `The official source for news about Apple, from Apple. Read press releases, get updates, watch video and download images.`, | ||
| categories: ['new-media'], | ||
| features: { | ||
| requireConfig: false, | ||
| requirePuppeteer: false, | ||
| antiCrawler: false, | ||
| supportRadar: true, | ||
| supportBT: false, | ||
| supportPodcast: false, | ||
| supportScihub: false, | ||
| }, | ||
| radar: [ | ||
| { | ||
| source: ['www.apple.com.cn/newsroom', 'www.apple.com.cn/newsroom/:year/:month/:slug'], | ||
| target: '/newsroom', | ||
| }, | ||
| ], | ||
| view: ViewType.Articles, | ||
| zh: { | ||
| path: '/newsroom', | ||
| name: 'Apple Newsroom (中国大陆)', | ||
| url: 'www.apple.com.cn/newsroom', | ||
| maintainers: ['LinxHex'], | ||
| example: '/apple/newsroom', | ||
| description: `Apple 新闻中心是 Apple 新闻的来源。阅读新闻稿、获取最新消息、观看视频和下载图片。`, | ||
| handler, | ||
| }, | ||
| handler, | ||
| }; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.