feat: 添加 Iwara 排行功能,支持视频和图片的排序与筛选 (#21251)

* feat: 添加 Iwara 排行功能,支持视频和图片的排序与筛选

* fix: 更新维护者信息

* feat: 使用 Puppeteer 优化 Iwara 排行数据获取

* fix: 修复链接生成逻辑以正确处理 slug

* refactor: 提取公共常量和函数到 utils.ts 文件以简化代码结构

* fix: 修复 Iwara URL 以确保正确访问网站
This commit is contained in:
CaoMeiYouRen233
2026-03-01 18:08:40 +08:00
committed by GitHub
parent 144d60b80f
commit ea2fefd5e6
4 changed files with 128 additions and 27 deletions
+1 -26
View File
@@ -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 `<img src="${imageRootUrl}/image/original/${item.thumbnail.id}/${item.thumbnail.name}">`;
}
if (item.embedUrl === null) {
return `<img src="${imageRootUrl}/image/original/${item.file.id}/thumbnail-${String(item.thumbnail).padStart(2, '0')}.jpg">`;
}
// 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 `<img src="${imageRootUrl}/image/embed/original/youtube/${match[1]}">`;
}
return '';
};
export const route: Route = {
path: '/users/:username/:type?',
example: '/iwara/users/kelpie/video',
+1 -1
View File
@@ -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',
};
+98
View File
@@ -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,
};
}
+28
View File
@@ -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 ? `<img src="${imageRootUrl}/image/original/${thumbnail.id}/${thumbnail.name}">` : '';
}
if (item.embedUrl === null) {
return item.file ? `<img src="${imageRootUrl}/image/original/${item.file.id}/thumbnail-${String(item.thumbnail).padStart(2, '0')}.jpg">` : '';
}
// 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 `<img src="${imageRootUrl}/image/embed/original/youtube/${match[1]}">`;
}
return '';
};