From 1ac8bd6136bc78cc6e11f84f79612c2ae33782c0 Mon Sep 17 00:00:00 2001 From: Urabartin <162798205+Urabartin@users.noreply.github.com> Date: Sat, 16 Mar 2024 11:56:38 +0800 Subject: [PATCH] feat(route): jpxgmn (#14734) * feat(route): jpxgmn * docs: revert unnecessary build * Apply suggestions from code review - parsing pubDate - radar rules --- lib/routes/jpxgmn/namespace.ts | 6 +++ lib/routes/jpxgmn/search.ts | 45 +++++++++++++++++ lib/routes/jpxgmn/tab.ts | 55 +++++++++++++++++++++ lib/routes/jpxgmn/templates/description.art | 3 ++ lib/routes/jpxgmn/utils.ts | 36 ++++++++++++++ lib/routes/jpxgmn/weekly.ts | 52 +++++++++++++++++++ 6 files changed, 197 insertions(+) create mode 100644 lib/routes/jpxgmn/namespace.ts create mode 100644 lib/routes/jpxgmn/search.ts create mode 100644 lib/routes/jpxgmn/tab.ts create mode 100644 lib/routes/jpxgmn/templates/description.art create mode 100644 lib/routes/jpxgmn/utils.ts create mode 100644 lib/routes/jpxgmn/weekly.ts diff --git a/lib/routes/jpxgmn/namespace.ts b/lib/routes/jpxgmn/namespace.ts new file mode 100644 index 000000000..a0b9011ec --- /dev/null +++ b/lib/routes/jpxgmn/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '极品性感美女', + url: 'www.jpxgmn.com', +}; diff --git a/lib/routes/jpxgmn/search.ts b/lib/routes/jpxgmn/search.ts new file mode 100644 index 000000000..666739dd0 --- /dev/null +++ b/lib/routes/jpxgmn/search.ts @@ -0,0 +1,45 @@ +import { Route } from '@/types'; +import { originUrl, getArticleDesc } from './utils'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/search/:kw', + categories: ['picture'], + example: '/jpxgmn/search/candy', + parameters: { kw: '搜索关键词' }, + name: '搜索', + maintainers: ['Urabartin'], + handler, +}; + +async function handler(ctx) { + const { kw } = ctx.req.param(); + const searchUrl = originUrl + `/plus/search/index.asp?keyword=${kw}`; + const response = await got(searchUrl); + const baseUrl = new URL(response.url).origin; + const $ = load(response.data); + const items = $('div.list div.list div.node p') + .toArray() + .map((item) => ({ + title: $(item).find('b').text(), + link: new URL($(item).find('a').attr('href'), baseUrl).href, + pubDate: parseDate($(item).next().next().next().find('span').first().text()), + })) + .filter((item) => item.title.length !== 0); + + return { + title: `极品性感美女搜索 - ${kw}`, + link: response.url, + item: await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + item.description = await getArticleDesc(item.link); + return item; + }) + ) + ), + }; +} diff --git a/lib/routes/jpxgmn/tab.ts b/lib/routes/jpxgmn/tab.ts new file mode 100644 index 000000000..67661143c --- /dev/null +++ b/lib/routes/jpxgmn/tab.ts @@ -0,0 +1,55 @@ +import { Route } from '@/types'; +import { originUrl, getArticleDesc } from './utils'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/tab/:tab?', + categories: ['picture'], + example: '/jpxgmn/tab', + parameters: { tab: '分类,默认为`top`,包括`top`、`new`、`hot`,以及[源网站](http://www.jpxgmn.com/)所包含的其他相对路径,比如`Xiuren`、`XiaoYu`等' }, + radar: [{ + source: ['www.12356782.xyz/:tab'], + target: '/:tab', + }], + name: '分类', + maintainers: ['Urabartin'], + handler, +}; + +async function handler(ctx) { + const { tab = 'top' } = ctx.req.param(); + const isSpecial = ['new', 'top', 'hot'].includes(tab); + const tabUrl = `${originUrl}/${tab}` + (isSpecial ? '.html' : '/'); + const response = await got(tabUrl); + const baseUrl = new URL(response.url).origin; + const $ = load(response.data); + const topTitle = $('div.toptip > a').get(1); + let feedTitle = $('title').text(); + if (isSpecial) { + feedTitle = feedTitle.split('_')[1]; + } else if (topTitle) { + feedTitle = $(topTitle).text(); + } + const items = $('div.related_posts ul > li') + .toArray() + .map((item) => ({ + title: $(item).find('a span').text(), + link: new URL($(item).find('a').attr('href'), baseUrl).href, + pubDate: parseDate($(item).find('footer span').first().text()), + })); + return { + title: `极品性感美女 - ${feedTitle}`, + link: response.url, + item: await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + item.description = await getArticleDesc(item.link); + return item; + }) + ) + ), + }; +} diff --git a/lib/routes/jpxgmn/templates/description.art b/lib/routes/jpxgmn/templates/description.art new file mode 100644 index 000000000..f60deb835 --- /dev/null +++ b/lib/routes/jpxgmn/templates/description.art @@ -0,0 +1,3 @@ +{{ each images }} + +{{ /each }} diff --git a/lib/routes/jpxgmn/utils.ts b/lib/routes/jpxgmn/utils.ts new file mode 100644 index 000000000..d03d52f44 --- /dev/null +++ b/lib/routes/jpxgmn/utils.ts @@ -0,0 +1,36 @@ +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); + +import got from '@/utils/got'; +import { art } from '@/utils/render'; +import { load } from 'cheerio'; +import * as path from 'node:path'; + +const originUrl = 'http://www.jpxgmn.com'; + +const getImages = ($articleContent) => + $articleContent('article > p img') + .toArray() + .map((img) => $articleContent(img).attr('src')); + +const getArticleDesc = async (articleUrl) => { + const response = await got(articleUrl); + const $content = load(response.data); + let pageCnt = $content('div.pagination:first ul a').length - 1; + if (pageCnt === -1) { + pageCnt = 1; + } + const images = getImages($content); + const otherImages = await Promise.all( + [...Array(pageCnt - 1).keys()].map(async (pageIndex) => { + const pageUrl = articleUrl.replace('.html', `_${pageIndex + 1}.html`); + const pageResponse = await got(pageUrl); + return getImages(load(pageResponse.data)); + }) + ); + return art(path.join(__dirname, 'templates/description.art'), { + images: [...images, ...otherImages.flat()], + }); +}; + +export { originUrl, getArticleDesc }; diff --git a/lib/routes/jpxgmn/weekly.ts b/lib/routes/jpxgmn/weekly.ts new file mode 100644 index 000000000..1cdea6ee5 --- /dev/null +++ b/lib/routes/jpxgmn/weekly.ts @@ -0,0 +1,52 @@ +import { Route } from '@/types'; +import { originUrl, getArticleDesc } from './utils'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +export const route: Route = { + path: '/weekly', + categories: ['picture'], + example: '/jpxgmn/weekly', + radar: [{ + source: ['www.12356782.xyz/'], + target: '/weekly', + }], + name: '本周热门', + maintainers: ['Urabartin'], + handler, +}; + +async function handler() { + const response = await got(originUrl); + const baseUrl = new URL(response.url).origin; + const $ = load(response.data); + const items = $('aside div:nth-child(2) li') + .toArray() + .map((item) => { + const fullTitle = $(item).find('a').attr('title') || ''; + const result = fullTitle.match(/([^.]+)\.\D+([\d-]+)/); + const ret = { + title: fullTitle, + link: new URL($(item).find('a').attr('href'), baseUrl).href, + }; + if (result !== null) { + ret.title = result[1]; + ret.pubDate = parseDate(result[2]); + } + return ret; + }); + return { + title: `极品性感美女 - 本周热门推荐`, + link: response.url, + item: await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + item.description = await getArticleDesc(item.link); + return item; + }) + ) + ), + }; +}