diff --git a/lib/v2/iwara/index.js b/lib/v2/iwara/index.js index f8b41bd44..9bbd80c02 100644 --- a/lib/v2/iwara/index.js +++ b/lib/v2/iwara/index.js @@ -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 ``; + } + + if (item.embedUrl === null) { + return ``; + } + + // 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 ``; + } + + 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: ``, - 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, }; };