Skip to content

Commit

Permalink
feat(route): add route "4KHD" (DIYgod#18308)
Browse files Browse the repository at this point in the history
  • Loading branch information
AiraNadih authored Feb 10, 2025
1 parent 46368b2 commit 46877a1
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/routes/4khd/article.ts
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 48 additions & 0 deletions lib/routes/4khd/category.ts
Original file line number Diff line number Diff line change
@@ -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)),
};
}
4 changes: 4 additions & 0 deletions lib/routes/4khd/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const SUB_NAME_PREFIX = '4KHD';
const SUB_URL = 'https://www.4khd.com/';

export { SUB_NAME_PREFIX, SUB_URL };
41 changes: 41 additions & 0 deletions lib/routes/4khd/latest.ts
Original file line number Diff line number Diff line change
@@ -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)),
};
}
8 changes: 8 additions & 0 deletions lib/routes/4khd/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: '4KHD',
url: 'www.4khd.net',
description: '4KHD - HD Beautiful Girls',
lang: 'en',
};
12 changes: 12 additions & 0 deletions lib/routes/4khd/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
interface WPPost {
title: {
rendered: string;
};
content: {
rendered: string;
};
date_gmt: string;
link: string;
}

export type { WPPost };

0 comments on commit 46877a1

Please sign in to comment.