Skip to content

Commit

Permalink
feat(route): add 蓝点网资讯 (#18216)
Browse files Browse the repository at this point in the history
  • Loading branch information
nczitzk authored Jan 25, 2025
1 parent bffd40c commit 478bf25
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
162 changes: 162 additions & 0 deletions lib/routes/landiannews/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { type Data, type DataItem, type Route, ViewType } from '@/types';

import { art } from '@/utils/render';
import cache from '@/utils/cache';
import { getCurrentPath } from '@/utils/helpers';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio';
import { type Context } from 'hono';
import path from 'node:path';

const __dirname = getCurrentPath(import.meta.url);

export const handler = async (ctx: Context): Promise<Data> => {
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10);

const baseUrl: string = 'https://www.landiannews.com';
const targetUrl: string = baseUrl;

const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language = $('html').attr('lang') ?? 'zh-CN';

let items: DataItem[] = [];

items = $('section.article-list a.title')
.slice(0, limit)
.toArray()
.map((el): Element => {
const $el: Cheerio<Element> = $(el);

const processedItem: DataItem = {
title: $el.attr('title') ?? $el.text(),
link: $el.attr('href'),
language,
};

return processedItem;
});

items = (
await Promise.all(
items.map((item) => {
if (!item.link) {
return item;
}

return cache.tryGet(item.link, async (): Promise<DataItem> => {
const detailResponse = await ofetch(item.link);
const $$: CheerioAPI = load(detailResponse);

const title: string = $$('header.post-header h1').text();
const intro: string = $$('div.excerpt').text();

$$('div.excerpt').remove();

const description: string = art(path.join(__dirname, 'templates/description.art'), {
intro,
description: $$('div.content-post').html(),
});
const pubDateStr: string | undefined = $$('span.date').text();
const categoryEls: Element[] = $$('span.category a, div.article-tags a').toArray();
const categories: string[] = [...new Set(categoryEls.map((el) => $$(el).attr('title') ?? $$(el).text()).filter(Boolean))];
const authorEls: Element[] = $$('span.author a[title]').toArray();
const authors: DataItem['author'] = authorEls.map((authorEl) => {
const $$authorEl: Cheerio<Element> = $$(authorEl);

return {
name: $$authorEl.attr('title') ?? '',
url: $$authorEl.attr('href') ?? '',
avatar: $$authorEl.find('img.avatar').attr('src'),
};
});
const upDatedStr: string | undefined = pubDateStr;

let processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? timezone(parseDate(pubDateStr, 'YYYY年M月D日 HH:mm'), +8) : item.pubDate,
category: categories,
author: authors,
content: {
html: description,
text: description,
},
updated: upDatedStr ? timezone(parseDate(upDatedStr, 'YYYY年M月D日 HH:mm'), +8) : item.updated,
language,
};

const extraLinkEls: Element[] = $$('section.article-list a[title]').toArray();
const extraLinks = extraLinkEls
.map((extraLinkEl) => {
const $$extraLinkEl: Cheerio<Element> = $$(extraLinkEl);

return {
url: $$extraLinkEl.attr('href'),
type: 'related',
content_html: $$extraLinkEl.text(),
};
})
.filter((_): _ is { url: string; type: string; content_html: string } => true);

if (extraLinks) {
processedItem = {
...processedItem,
_extra: {
links: extraLinks,
},
};
}

return {
...item,
...processedItem,
};
});
})
)
).filter((_): _ is DataItem => true);

return {
title: $('title').text(),
description: $('meta[property="og:description"]').attr('content'),
link: targetUrl,
item: items,
allowEmpty: true,
image: $('img.logo').attr('src'),
author: $('meta[property="og:site_name"]').attr('content'),
language,
id: $('meta[property="og:url"]').attr('content'),
};
};

export const route: Route = {
path: '/',
name: '资讯',
url: 'www.landiannews.com',
maintainers: ['nczitzk'],
handler,
example: '/landiannews',
parameters: undefined,
description: undefined,
categories: ['new-media'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.landiannews.com'],
target: '/',
},
],
view: ViewType.Articles,
};
9 changes: 9 additions & 0 deletions lib/routes/landiannews/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '蓝点网',
url: 'landiannews.com',
categories: ['new-media'],
description: '',
lang: 'zh-CN',
};
7 changes: 7 additions & 0 deletions lib/routes/landiannews/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ if intro }}
<blockquote>{{ intro }}</blockquote>
{{ /if }}

{{ if description }}
{{@ description }}
{{ /if }}

0 comments on commit 478bf25

Please sign in to comment.