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 '';
+};