Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions lib/routes/cloudflare/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { load } from 'cheerio';
import pMap from 'p-map';
import Parser from 'rss-parser';

import type { Data, Route } from '@/types';
import { ViewType } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

const parser = new Parser<Pick<Data, 'language'>, { creators?: string[] }>({
customFields: {
item: [['dc:creator', 'creators', { keepArray: true }]],
},
});

export const route: Route = {
path: '/blog',
name: 'Blog',
url: 'blog.cloudflare.com',
example: '/cloudflare/blog',
categories: ['programming'],
maintainers: ['ljh12138164'],
description:
'Uses the [official RSS feed](https://blog.cloudflare.com/rss/) for article metadata and fetches full articles from [Cloudflare Blog](https://blog.cloudflare.com/), including inline images, code blocks, and copyable prompts.',
zh: {
description: '来源:[Cloudflare Blog](https://blog.cloudflare.com/)。使用[官方 RSS](https://blog.cloudflare.com/rss/)获取文章列表、作者、日期和标签,再抓取并缓存网页正文,补齐图片、代码块和可复制的提示词。',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['blog.cloudflare.com'],
target: '/blog',
},
],
view: ViewType.Articles,
handler,
};

async function handler(): Promise<Data> {
const feedXml = await ofetch('https://blog.cloudflare.com/rss/', { responseType: 'text' });
const feed = await parser.parseString(feedXml);

return {
title: feed.title!,
description: feed.description,
link: feed.link,
image: feed.image?.url,
language: feed.language,
item: await pMap(
feed.items,
async (item) => ({
title: item.title!,
link: item.link,
description: await cache.tryGet(item.link!, () => fetchArticle(item.link!)),
pubDate: item.pubDate ? parseDate(item.pubDate) : undefined,
author: item.creators?.join(', '),
category: item.categories,
guid: item.guid,
enclosure_url: item.enclosure?.url,
enclosure_type: item.enclosure?.type,
// rss-parser returns XML strings despite declaring enclosure lengths as numbers.
// oxlint-disable-next-line typescript/no-unnecessary-type-conversion
enclosure_length: item.enclosure ? Number(item.enclosure.length) : undefined,
}),
{ concurrency: 3 }
),
};
}

async function fetchArticle(link: string): Promise<string> {
const response = await ofetch(link);
const $ = load(response);
const article = $('.article-content').first();
if (!article.length) {
throw new Error(`Cloudflare Blog article content not found: ${link}`);
}

article.find('[data-copy-prompt]').each((_, element) => {
// The copy button stores the full prompt as a JSON string.
const prompt = JSON.parse($(element).find('[data-prompt]').attr('data-prompt')!);
$(element).replaceWith($('<pre>').append($('<code>').text(prompt)));
});

article.find('script, style, button, img[data-image-placeholder]').remove();
article.find('[srcset]').removeAttr('srcset');
article.find('[src], [href], [poster]').each((_, element) => {
const $element = $(element);
for (const attribute of ['src', 'href', 'poster']) {
const value = $element.attr(attribute);
if (value) {
$element.attr(attribute, new URL(value, link).href);
}
}
});

return article.html()!;
}
8 changes: 8 additions & 0 deletions lib/routes/cloudflare/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Cloudflare',
url: 'cloudflare.com',
categories: ['programming'],
lang: 'en',
};
130 changes: 130 additions & 0 deletions lib/routes/cloudflare/status.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { load } from 'cheerio';
import type { Context } from 'hono';
import { raw } from 'hono/html';
import { renderToString } from 'hono/jsx/dom/server';
import MarkdownIt from 'markdown-it';

import { config } from '@/config';
import type { Data, DataItem, Route } from '@/types';
import { ViewType } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

interface IncidentUpdate {
status: string;
body: string;
display_at: string;
}

interface Incident {
id: string;
name: string;
incident_updates: IncidentUpdate[];
}

interface StatusResponse {
page: { name: string };
incidents: Incident[];
scheduled_maintenances: Incident[];
}

const baseUrl = 'https://www.cloudflarestatus.com';
const md = MarkdownIt({ html: true, linkify: true });

function createItem(incident: Incident, update: IncidentUpdate, index: number) {
const previousUpdate = incident.incident_updates[index + 1];
const type = update.status === 'scheduled' && incident.incident_updates.length === 1 ? '' : update.status === previousUpdate?.status ? 'Update' : update.status.replaceAll('_', ' ').replace(/^./, (char) => char.toUpperCase());
const body = md.renderInline(update.body.replaceAll(/\r?\n/g, '<br>'));
const text = load(body, null, false).text();
// Statuspage's HTML timestamps use second precision; preserve existing GUIDs.
const timestamp = Math.floor(parseDate(update.display_at).getTime() / 1000) * 1000;
const pubDate = parseDate(timestamp, 'x');
const link = `${baseUrl}/incidents/${incident.id}`;
const guid = `${link}#${timestamp}`;
const description = renderToString(
<>
<h2>{incident.name}</h2>
{type ? (
<>
<strong>{type}</strong>
{' - '}
</>
) : null}
<span class="whitespace-pre-wrap">{raw(body)}</span>
<br />
<small>
<span class="ago" data-datetime-unix={timestamp} />
{pubDate.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' })}
{' - '}
{pubDate.toISOString().slice(11, 16)} UTC
</small>
</>
);

return {
title: `${type ? `${type} - ` : ''}${text}`,
description,
pubDate,
link,
category: type ? [type] : [],
guid,
id: guid,
content: { html: description, text: description },
updated: pubDate,
language: 'en',
} satisfies DataItem;
}

export const handler = async (ctx: Context): Promise<Data> => {
const limit = Number(ctx.req.query('limit') ?? '100');
const response = await ofetch<StatusResponse>(`${baseUrl}/api/v2/summary.json`, {
headers: { 'User-Agent': config.trueUA },
});
const items = [...response.incidents, ...response.scheduled_maintenances]
.flatMap((incident) => incident.incident_updates.map((update, index) => createItem(incident, update, index)))
.toSorted((a, b) => b.pubDate.getTime() - a.pubDate.getTime())
.slice(0, limit);

return {
title: response.page.name,
description: 'Real-time status and incident history for Cloudflare services, network locations, and scheduled maintenance.',
link: baseUrl,
item: items,
allowEmpty: true,
language: 'en',
id: baseUrl,
};
};

export const route: Route = {
path: '/status',
name: 'Status',
url: 'www.cloudflarestatus.com',
maintainers: ['nczitzk', 'ljh12138164'],
handler,
example: '/cloudflare/status',
parameters: undefined,
description:
'Uses the [official API](https://www.cloudflarestatus.com/api) to provide each update for ongoing incidents and upcoming or active maintenance from [Cloudflare Status](https://www.cloudflarestatus.com/) as a separate item. The legacy route `/cloudflarestatus` redirects here.',
zh: {
description:
'来源:[Cloudflare Status](https://www.cloudflarestatus.com/)。使用[官方 API](https://www.cloudflarestatus.com/api)获取未解决故障及计划中、进行中的维护通知,每次更新单独生成一条订阅内容。旧路由 `/cloudflarestatus` 会重定向到此路由。',
},
categories: ['programming'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.cloudflarestatus.com'],
target: '/status',
},
],
view: ViewType.Notifications,
};
105 changes: 7 additions & 98 deletions lib/routes/cloudflarestatus/index.tsx
Original file line number Diff line number Diff line change
@@ -1,105 +1,14 @@
import type { Cheerio, CheerioAPI } from 'cheerio';
import { load } from 'cheerio';
import type { Element } from 'domhandler';
import type { Context } from 'hono';
import { raw } from 'hono/html';
import { renderToString } from 'hono/jsx/dom/server';

import type { Data, DataItem, Language, Route } from '@/types';
import { ViewType } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const handler = async (ctx: Context): Promise<Data> => {
const limit = Number(ctx.req.query('limit') ?? '100');

const baseUrl = 'https://www.cloudflarestatus.com';

const response = await ofetch(baseUrl);
const $: CheerioAPI = load(response);
const language = ($('html').attr('lang') ?? 'en') as Language;

$('div.incidents-list').remove();

const items: DataItem[] = $('div.update')
.slice(0, limit)
.toArray()
.map((el) => {
const $el: Cheerio<Element> = $(el);

const $actualTitleEl: Cheerio<Element> = $el.parent().parent().find('a');
const actualTitle: string = $actualTitleEl.text();
const type: string = $el.find('strong').first().text();
const text: string = $el.find('span.whitespace-pre-wrap').first().text();

const title = `${type ? `${type} - ` : ''}${text}`;
const description: string | undefined = renderToString(
<>
{actualTitle ? <h2>{actualTitle}</h2> : null}
{$el.html() ? raw($el.html()) : null}
</>
);
const pubDateStr: string | undefined = $el.find('span.ago').attr('data-datetime-unix');
const href: string | undefined = $actualTitleEl.attr('href');
const linkUrl: string | undefined = href ? new URL(href, baseUrl).href : undefined;
const categories: string[] = [type].filter(Boolean);
const guid: string = linkUrl ? `${linkUrl}#${pubDateStr}` : '';
const upDatedStr: string | undefined = pubDateStr;

const processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? parseDate(pubDateStr, 'x') : undefined,
link: linkUrl,
category: categories,
guid,
id: guid,
content: {
html: description,
text: description,
},
updated: upDatedStr ? parseDate(upDatedStr, 'x') : undefined,
language,
};

return processedItem;
});

return {
title: $('title').text(),
description: $('meta[name="description"]').attr('content'),
link: baseUrl,
item: items,
allowEmpty: true,
language,
id: baseUrl,
};
};
import type { Route } from '@/types';

export const route: Route = {
path: '/',
name: 'Status',
name: 'Status (legacy)',
url: 'www.cloudflarestatus.com',
maintainers: ['nczitzk'],
handler,
maintainers: ['nczitzk', 'ljh12138164'],
example: '/cloudflarestatus',
parameters: undefined,
description: undefined,
categories: ['programming'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
description: 'This route redirects to `/cloudflare/status`.',
zh: {
description: '此旧路由会重定向到 `/cloudflare/status`。',
},
radar: [
{
source: ['www.cloudflarestatus.com'],
target: '/',
},
],
view: ViewType.Notifications,
handler: (ctx) => ctx.set('redirect', `/cloudflare/status${new URL(ctx.req.url).search}`),
};
Loading