fix(route): use api to obtain /iwara/users (#12655)

This commit is contained in:
miles 2023-06-13 19:21:37 +08:00 committed by GitHub
parent 75f545a9d7
commit cdad5977db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 24 deletions

View File

@ -1,43 +1,55 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const config = require('@/config').value;
const rootUrl = 'https://ecchi.iwara.tv';
const rootUrl = 'https://www.iwara.tv';
const apiRootUrl = 'https://api.iwara.tv';
const imageRootUrl = 'https://i.iwara.tv';
const typeMap = {
video: 'Videos',
image: 'Images',
};
const selectorMap = {
video: 'div.node.node-video.node-teaser.node-teaser.clearfix',
image: 'div.node.node-image.node-teaser.node-teaser.clearfix',
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 = /http(?:s?):\/\/(?: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 '';
};
module.exports = async (ctx) => {
const username = ctx.params.username;
const id = await ctx.cache.tryGet(`${apiRootUrl}/profile/${username}`, async () => (await got(`${apiRootUrl}/profile/${username}`)).data.user.id);
const type = ctx.params.type ?? 'video';
const userUrl = `${rootUrl}/users/${username}`;
const response = await got.get(userUrl);
const $ = cheerio.load(response.data);
const list = $(selectorMap[type]);
const items = list
.map((_, item) => {
const imageUrl = 'https:' + $(item).find('img').attr('src');
return {
title: $(item).find('h3.title').text(),
author: username,
link: rootUrl + $(item).find('h3.title > a').attr('href'),
description: `<img src="${imageUrl}">`,
guid: (rootUrl + $(item).find('h3.title > a').attr('href')).split('?')[0],
};
})
.get();
const items = (await ctx.cache.tryGet(`${apiUrlMap[type]}?user=${id}`, async () => (await got(`${apiUrlMap[type]}?user=${id}`)).data.results, config.cache.routeExpire, false)).map((item) => ({
title: item.title,
author: username,
link: `${rootUrl}/${type}/${item.id}/${item.slug}`,
category: item.tags.map((i) => i.id),
description: parseThumbnail(type, item),
pubDate: parseDate(item.createdAt),
}));
ctx.state.data = {
title: `${username}'s iwara - ${typeMap[type]}`,
link: userUrl,
link: `${rootUrl}/users/${username}`,
item: items,
};
};