diff --git a/lib/routes/4khd/article.ts b/lib/routes/4khd/article.ts new file mode 100644 index 0000000000..c600a581ae --- /dev/null +++ b/lib/routes/4khd/article.ts @@ -0,0 +1,28 @@ +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import { WPPost } from './types'; + +const processImages = ($) => { + $('a').each((_, elem) => { + const $elem = $(elem); + const largePhotoUrl = $elem.attr('href').replace('i0.wp.com/pic', 'img'); + if (largePhotoUrl) { + $elem.attr('href', largePhotoUrl); + $elem.find('img').attr('src', largePhotoUrl); + } + }); +}; + +function loadArticle(item: WPPost) { + const article = load(item.content.rendered); + processImages(article); + + return { + title: item.title.rendered, + description: article.html() ?? '', + pubDate: parseDate(item.date_gmt), + link: item.link, + }; +} + +export default loadArticle; diff --git a/lib/routes/4khd/category.ts b/lib/routes/4khd/category.ts new file mode 100644 index 0000000000..539baad8be --- /dev/null +++ b/lib/routes/4khd/category.ts @@ -0,0 +1,48 @@ +import { Route } from '@/types'; +import got from '@/utils/got'; +import { SUB_NAME_PREFIX, SUB_URL } from './const'; +import loadArticle from './article'; +import { WPPost } from './types'; + +export const route: Route = { + path: '/category/:category', + categories: ['picture'], + example: '/4khd/category/cosplay', + parameters: { category: 'Category' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.4khd.com/pages/:category'], + target: '/category/:category', + }, + ], + name: 'Category', + maintainers: ['AiraNadih'], + handler, + url: 'www.4khd.com/', +}; + +async function handler(ctx) { + const limit = Number.parseInt(ctx.req.query('limit')) || 20; + const category = ctx.req.param('category'); + const categoryUrl = `${SUB_URL}pages/${category}/`; + const slug = category === 'album' ? 'photo' : category; + + const { + data: [{ id: categoryId }], + } = await got(`${SUB_URL}wp-json/wp/v2/categories?slug=${slug}`); + const { data: posts } = await got(`${SUB_URL}wp-json/wp/v2/posts?categories=${categoryId}&per_page=${limit}`); + + return { + title: `${SUB_NAME_PREFIX} - Category: ${category}`, + link: categoryUrl, + item: posts.map((post) => loadArticle(post as WPPost)), + }; +} diff --git a/lib/routes/4khd/const.ts b/lib/routes/4khd/const.ts new file mode 100644 index 0000000000..130430f17b --- /dev/null +++ b/lib/routes/4khd/const.ts @@ -0,0 +1,4 @@ +const SUB_NAME_PREFIX = '4KHD'; +const SUB_URL = 'https://www.4khd.com/'; + +export { SUB_NAME_PREFIX, SUB_URL }; diff --git a/lib/routes/4khd/latest.ts b/lib/routes/4khd/latest.ts new file mode 100644 index 0000000000..e718baf8bf --- /dev/null +++ b/lib/routes/4khd/latest.ts @@ -0,0 +1,41 @@ +import { Route } from '@/types'; +import got from '@/utils/got'; +import { SUB_NAME_PREFIX, SUB_URL } from './const'; +import loadArticle from './article'; +import { WPPost } from './types'; + +export const route: Route = { + path: '/', + categories: ['picture'], + example: '/4khd', + parameters: {}, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.4khd.com/'], + target: '', + }, + ], + name: 'Latest', + maintainers: ['AiraNadih'], + handler, + url: 'www.4khd.com/', +}; + +async function handler(ctx) { + const limit = Number.parseInt(ctx.req.query('limit')) || 20; + const { data: posts } = await got(`${SUB_URL}wp-json/wp/v2/posts?per_page=${limit}`); + + return { + title: `${SUB_NAME_PREFIX} - Latest`, + link: SUB_URL, + item: posts.map((post) => loadArticle(post as WPPost)), + }; +} diff --git a/lib/routes/4khd/namespace.ts b/lib/routes/4khd/namespace.ts new file mode 100644 index 0000000000..c5bb29a8c2 --- /dev/null +++ b/lib/routes/4khd/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '4KHD', + url: 'www.4khd.net', + description: '4KHD - HD Beautiful Girls', + lang: 'en', +}; diff --git a/lib/routes/4khd/types.ts b/lib/routes/4khd/types.ts new file mode 100644 index 0000000000..d3ea3ac2a8 --- /dev/null +++ b/lib/routes/4khd/types.ts @@ -0,0 +1,12 @@ +interface WPPost { + title: { + rendered: string; + }; + content: { + rendered: string; + }; + date_gmt: string; + link: string; +} + +export type { WPPost };