From ea2fefd5e68166371ec1a7af0d98357c27fc3791 Mon Sep 17 00:00:00 2001
From: CaoMeiYouRen233 <87434371+CaoMeiYouRen233@users.noreply.github.com>
Date: Sun, 1 Mar 2026 18:08:40 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20Iwara=20=E6=8E=92?=
=?UTF-8?q?=E8=A1=8C=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=94=AF=E6=8C=81=E8=A7=86?=
=?UTF-8?q?=E9=A2=91=E5=92=8C=E5=9B=BE=E7=89=87=E7=9A=84=E6=8E=92=E5=BA=8F?=
=?UTF-8?q?=E4=B8=8E=E7=AD=9B=E9=80=89=20(#21251)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat: 添加 Iwara 排行功能,支持视频和图片的排序与筛选
* fix: 更新维护者信息
* feat: 使用 Puppeteer 优化 Iwara 排行数据获取
* fix: 修复链接生成逻辑以正确处理 slug
* refactor: 提取公共常量和函数到 utils.ts 文件以简化代码结构
* fix: 修复 Iwara URL 以确保正确访问网站
---
lib/routes/iwara/index.ts | 27 +---------
lib/routes/iwara/namespace.ts | 2 +-
lib/routes/iwara/ranking.ts | 98 +++++++++++++++++++++++++++++++++++
lib/routes/iwara/utils.ts | 28 ++++++++++
4 files changed, 128 insertions(+), 27 deletions(-)
create mode 100644 lib/routes/iwara/ranking.ts
create mode 100644 lib/routes/iwara/utils.ts
diff --git a/lib/routes/iwara/index.ts b/lib/routes/iwara/index.ts
index d54718be8..13ed58f66 100644
--- a/lib/routes/iwara/index.ts
+++ b/lib/routes/iwara/index.ts
@@ -4,38 +4,13 @@ import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
-const rootUrl = 'https://www.iwara.tv';
-const apiRootUrl = 'https://api.iwara.tv';
-const imageRootUrl = 'https://i.iwara.tv';
-
-const typeMap = {
- video: 'Videos',
- image: 'Images',
-};
+import { apiRootUrl, parseThumbnail, rootUrl, typeMap } from './utils';
const apiUrlMap = {
video: `${apiRootUrl}/videos`,
image: `${apiRootUrl}/images`,
};
-const parseThumbnail = (type, item) => {
- if (type === 'image') {
- return `
`;
- }
-
- if (item.embedUrl === null) {
- return `
`;
- }
-
- // regex borrowed from https://stackoverflow.com/a/3726073
- const match = /https?:\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w-]*)(&(amp;)?[\w=?]*)?/.exec(item.embedUrl);
- if (match) {
- return `
`;
- }
-
- return '';
-};
-
export const route: Route = {
path: '/users/:username/:type?',
example: '/iwara/users/kelpie/video',
diff --git a/lib/routes/iwara/namespace.ts b/lib/routes/iwara/namespace.ts
index 01aa2f0c3..71792cb95 100644
--- a/lib/routes/iwara/namespace.ts
+++ b/lib/routes/iwara/namespace.ts
@@ -2,6 +2,6 @@ import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'iwara',
- url: 'ecchi.iwara.tv',
+ url: 'www.iwara.tv',
lang: 'en',
};
diff --git a/lib/routes/iwara/ranking.ts b/lib/routes/iwara/ranking.ts
new file mode 100644
index 000000000..b2969e31b
--- /dev/null
+++ b/lib/routes/iwara/ranking.ts
@@ -0,0 +1,98 @@
+import { config } from '@/config';
+import type { Route } from '@/types';
+import cache from '@/utils/cache';
+import { parseDate } from '@/utils/parse-date';
+import { getPuppeteerPage } from '@/utils/puppeteer';
+
+import { apiqRootUrl, parseThumbnail, rootUrl, typeMap } from './utils';
+
+const sortMap = {
+ date: 'Latest',
+ trending: 'Trending',
+ popularity: 'Popularity',
+ views: 'Views',
+ likes: 'Likes',
+};
+
+const ratingMap = {
+ all: 'All',
+ general: 'General',
+ ecchi: 'Ecchi',
+};
+
+export const route: Route = {
+ path: '/ranking/:type?/:sort?/:rating?',
+ example: '/iwara/ranking/video/date/ecchi',
+ parameters: {
+ type: 'Content type, can be video or image, default is video',
+ sort: 'Sort type, can be date, trending, popularity, views, likes, default is date',
+ rating: 'Rating, can be all, general, ecchi, default is ecchi',
+ },
+ name: 'Ranking',
+ maintainers: ['CaoMeiYouRen233'],
+ handler,
+ features: {
+ requirePuppeteer: true,
+ nsfw: true,
+ },
+ radar: [
+ {
+ source: ['www.iwara.tv/videos', 'www.iwara.tv/images'],
+ target: (params, url) => {
+ const searchParams = new URL(url).searchParams;
+ const type = url.includes('/videos') ? 'video' : 'image';
+ const sort = searchParams.get('sort') || 'date';
+ const rating = searchParams.get('rating') || 'ecchi';
+ return `/iwara/ranking/${type}/${sort}/${rating}`;
+ },
+ },
+ ],
+};
+
+async function handler(ctx) {
+ const { type = 'video', sort = 'date', rating = 'ecchi' } = ctx.req.param();
+
+ const limit = ctx.req.query('limit') || 32;
+ const url = `${apiqRootUrl}/${type === 'video' ? 'videos' : 'images'}?sort=${sort}&rating=${rating}&limit=${limit}`;
+
+ const items = await cache.tryGet(
+ `iwara:ranking:${type}:${sort}:${rating}`,
+ async () => {
+ const { page, destory } = await getPuppeteerPage(url, {
+ onBeforeLoad: async (page) => {
+ await page.setRequestInterception(true);
+ page.on('request', (request) => {
+ request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'xhr' || request.resourceType() === 'fetch' ? request.continue() : request.abort();
+ });
+ },
+ gotoConfig: {
+ waitUntil: 'networkidle0',
+ },
+ });
+
+ try {
+ const content = await page.evaluate(() => document.querySelector('pre')?.textContent || document.body.textContent);
+ const response = JSON.parse(content || '{}');
+
+ return response.results.map((item) => ({
+ title: item.title,
+ author: item.user.name,
+ link: `${rootUrl}/${type === 'video' ? 'video' : 'image'}/${item.id}${item.slug ? `/${item.slug}` : ''}`,
+ category: item.tags?.map((i) => i.id) || [],
+ description: parseThumbnail(type, item),
+ pubDate: parseDate(item.createdAt),
+ }));
+ } finally {
+ await destory();
+ }
+ },
+ config.cache.routeExpire,
+ false
+ );
+
+ return {
+ title: `Iwara Ranking - ${typeMap[type]} - ${sortMap[sort]} - ${ratingMap[rating]}`,
+ link: `${rootUrl}/${type === 'video' ? 'videos' : 'images'}?sort=${sort}&rating=${rating}`,
+ item: items,
+ };
+}
diff --git a/lib/routes/iwara/utils.ts b/lib/routes/iwara/utils.ts
new file mode 100644
index 000000000..573e3a1ef
--- /dev/null
+++ b/lib/routes/iwara/utils.ts
@@ -0,0 +1,28 @@
+export const rootUrl = 'https://www.iwara.tv';
+export const apiRootUrl = 'https://api.iwara.tv';
+export const apiqRootUrl = 'https://apiq.iwara.tv';
+export const imageRootUrl = 'https://i.iwara.tv';
+
+export const typeMap = {
+ video: 'Videos',
+ image: 'Images',
+};
+
+export const parseThumbnail = (type: 'video' | 'image', item: any) => {
+ if (type === 'image') {
+ const thumbnail = item.thumbnail || item.file;
+ return thumbnail ? `
` : '';
+ }
+
+ if (item.embedUrl === null) {
+ return item.file ? `
` : '';
+ }
+
+ // regex borrowed from https://stackoverflow.com/a/3726073
+ const match = /https?:\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w-]*)(&(amp;)?[\w=?]*)?/.exec(item.embedUrl);
+ if (match) {
+ return `
`;
+ }
+
+ return '';
+};