diff --git a/lib/routes-deprecated/ltaaa/index.js b/lib/routes-deprecated/ltaaa/index.js deleted file mode 100644 index eec7915f5..000000000 --- a/lib/routes-deprecated/ltaaa/index.js +++ /dev/null @@ -1,69 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const timezone = require('@/utils/timezone'); -const { parseDate } = require('@/utils/parse-date'); - -module.exports = async (ctx) => { - const category = ctx.params.category || 'latest'; - - const rootUrl = 'http://www.ltaaa.cn'; - const currentUrl = `${rootUrl}/${category === 'picture' ? category : `article${category === 'latest' ? '' : `/${category}`}`}`; - - const response = await got({ - method: 'get', - url: currentUrl, - }); - - const $ = cheerio.load(response.data); - - const list = $(category === 'picture' || category === 'curiosities' ? 'dd .title' : '.li-title a') - .slice(0, 10) - .map((_, item) => { - item = $(item); - - return { - title: item.text(), - link: `${rootUrl}${item.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - if (category === 'picture') { - item.description = ''; - content('.show li').each(function () { - item.description += content(this).find('a').html() + (content(this).find('.pic-p').html() || ''); - }); - item.pubDate = parseDate( - content('.view a img') - .attr('src') - .match(/http:\/\/img\.ltaaa\.cn\/uploadfile\/(.*)\/\d+\.jpg/)[1], - 'YYYY/MM/DD' - ); - } else { - content('.post-param').find('a, span').remove(); - item.pubDate = timezone(new Date(content('.post-param').text().trim()), +8); - - content('.post-title, .post-param, .post-keywords, .like-post, .clear, .hook').remove(); - item.description = content('.post-body').html(); - } - - return item; - }) - ) - ); - - ctx.state.data = { - title: $('title').text(), - link: currentUrl, - item: items, - }; -}; diff --git a/lib/routes/ltaaa/article.ts b/lib/routes/ltaaa/article.ts new file mode 100644 index 000000000..510bd4c0d --- /dev/null +++ b/lib/routes/ltaaa/article.ts @@ -0,0 +1,180 @@ +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 { 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 => { + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10); + + const baseUrl: string = 'https://www.ltaaa.cn'; + const targetUrl: string = new URL('article', baseUrl).href; + + const response = await ofetch(targetUrl); + const $: CheerioAPI = load(response); + const language = $('html').attr('lang') ?? 'zh-CN'; + + let items: DataItem[] = []; + + items = $('ul.wlist li') + .slice(0, limit) + .toArray() + .map((el): Element => { + const $el: Cheerio = $(el); + + const $aEl: Cheerio = $el.find('div.li-title a'); + + const title: string = $aEl.text(); + const description: string = art(path.join(__dirname, 'templates/description.art'), { + intro: $el.find('div.dbody p').first().text(), + }); + const pubDateStr: string | undefined = $el.find('i.icon-time').next().text().trim(); + const linkUrl: string | undefined = $aEl.attr('href'); + const authorEls: Element[] = $el.find('i.icon-user').parent().find('a').toArray(); + const authors: DataItem['author'] = authorEls.map((authorEl) => { + const $authorEl: Cheerio = $(authorEl); + + return { + name: $authorEl.text(), + url: new URL($authorEl.attr('href') ?? '', baseUrl).href, + avatar: undefined, + }; + }); + const image: string | undefined = $el.find('div.li-thumb img').attr('src'); + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr) : undefined, + link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined, + author: authors, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr) : undefined, + language, + }; + + return processedItem; + }); + + items = ( + await Promise.all( + items.map((item) => { + if (!item.link) { + return item; + } + + return cache.tryGet(item.link, async (): Promise => { + const detailResponse = await ofetch(item.link); + const $$: CheerioAPI = load(detailResponse); + + const title: string = $$('div.post-title').text(); + + const pubDateStr: string | undefined = $$('i.icon-time').next().text().trim(); + const categoryEls: Element[] = $$('span.keywords a').toArray(); + const categories: string[] = [...new Set(categoryEls.map((el) => $$(el).text()).filter(Boolean))]; + const authorEls: Element[] = $$('i.icon-user').first().nextAll('a').toArray(); + const authors: DataItem['author'] = authorEls.map((authorEl) => { + const $$authorEl: Cheerio = $$(authorEl); + + return { + name: $$authorEl.text(), + url: new URL($$authorEl.attr('href') ?? '', baseUrl).href, + avatar: undefined, + }; + }); + const upDatedStr: string | undefined = pubDateStr; + + $$('div.post-tip').each((_, el) => { + const $$el: Cheerio = $$(el); + const content: string = $$el.html() ?? ''; + + if (content) { + $$el.replaceWith(`

${content}

`); + } + }); + $$('div.post-param, div.post-title, div.post-keywords').remove(); + $$('div.attitude, div.clear').remove(); + + const description: string = art(path.join(__dirname, 'templates/description.art'), { + description: $$('div.post-body').html(), + }); + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate, + category: categories, + author: authors, + content: { + html: description, + text: description, + }, + updated: upDatedStr ? parseDate(upDatedStr) : item.updated, + language, + }; + + return { + ...item, + ...processedItem, + }; + }); + }) + ) + ).filter((_): _ is DataItem => true); + + const title: string = $('title').text(); + + return { + title, + description: $('meta[name="description"]').attr('content'), + link: targetUrl, + item: items, + allowEmpty: true, + image: new URL('static/home/images/logo.png', baseUrl).href, + author: title.split(/-/).pop(), + language, + id: targetUrl, + }; +}; + +export const route: Route = { + path: '/article', + name: '网站翻译', + url: 'www.ltaaa.cn', + maintainers: ['nczitzk'], + handler, + example: '/ltaaa/article', + 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.ltaaa.cn/article'], + target: '/article', + }, + ], + view: ViewType.Articles, +}; diff --git a/lib/routes/ltaaa/namespace.ts b/lib/routes/ltaaa/namespace.ts new file mode 100644 index 000000000..e019aeab3 --- /dev/null +++ b/lib/routes/ltaaa/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '龙腾网', + url: 'ltaaa.cn', + categories: ['new-media'], + description: '', + lang: 'zh-CN', +}; diff --git a/lib/routes/ltaaa/templates/description.art b/lib/routes/ltaaa/templates/description.art new file mode 100644 index 000000000..57498ab45 --- /dev/null +++ b/lib/routes/ltaaa/templates/description.art @@ -0,0 +1,7 @@ +{{ if intro }} +
{{ intro }}
+{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file