From a45d2b49b69870eff7bfee79a6bcf32be2b377bd Mon Sep 17 00:00:00 2001 From: FeCCC <72006654+FeCCC@users.noreply.github.com> Date: Tue, 2 May 2023 04:40:41 +0800 Subject: [PATCH] fix(route): iwara subscriptions (#12417) * fix(route): iwara subscriptions * style: auto format --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- lib/v2/iwara/subscriptions.js | 90 ++++++++++++++++++------ lib/v2/iwara/templates/subscriptions.art | 2 +- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/lib/v2/iwara/subscriptions.js b/lib/v2/iwara/subscriptions.js index f2466cf4e..497036093 100644 --- a/lib/v2/iwara/subscriptions.js +++ b/lib/v2/iwara/subscriptions.js @@ -1,48 +1,92 @@ const got = require('@/utils/got'); const config = require('@/config').value; -const cheerio = require('cheerio'); const { art } = require('@/utils/render'); +const { parseDate } = require('@/utils/parse-date'); const path = require('path'); +const md = require('markdown-it')({ + html: true, +}); module.exports = async (ctx) => { - const rootUrl = `https://ecchi.iwara.tv`; - const subUrl = `https://ecchi.iwara.tv/subscriptions`; + const rootUrl = `https://www.iwara.tv`; + const videoSubUrl = 'https://api.iwara.tv/videos?page=0&limit=30&subscribed=true'; + const imageSubUrl = 'https://api.iwara.tv/images?page=0&limit=30&subscribed=true'; const cookie = config.iwara.cookie; if (cookie === undefined) { throw Error('Iwara subscription RSS is disabled due to the lack of relevant config'); } - const response = await got({ + const videoResponse = await got({ method: 'get', - url: subUrl, + url: videoSubUrl, headers: { Cookie: cookie, }, }); - const $ = cheerio.load(response.data); - const username = $('a.btn.btn-info.btn-sm.user').text().trim(); - const list = $('div.views-column'); - const items = list - .map((_, item) => { - const imageUrl = 'https:' + $(item).find('img').attr('src'); - const type = $(item).find('div.field').hasClass('field-type-video') ? 'Video' : 'Image'; - return { - title: $(item).find('h3.title').text(), - author: $(item).find('a.username').text(), - link: rootUrl + $(item).find('h3.title > a').attr('href'), - category: type, - description: art(path.join(__dirname, 'templates/subscriptions.art'), { + const imageResponse = await got({ + method: 'get', + url: imageSubUrl, + headers: { + Cookie: cookie, + }, + }); + + const videoList = videoResponse.data.results.map((item) => { + const imageUrl = item.file ? 'https://i.iwara.tv/image/original/' + item.file.id.toString().padStart(2, '0') + '/thumbnail-' + item.thumbnail.toString().padStart(2, '0') + '.jpg' : ''; + return { + title: item.title, + author: item.user.name, + link: rootUrl + '/video/' + item.id, + category: 'Video', + imageUrl, + pubDate: parseDate(item.createdAt), + }; + }); + + const imageList = imageResponse.data.results.map((item) => { + const imageUrl = item.thumbnail ? 'https://i.iwara.tv/image/thumbnail/' + item.thumbnail.id + '/' + item.thumbnail.id + '.jpg' : ''; + return { + title: item.title, + author: item.user.name, + link: rootUrl + '/image/' + item.id, + category: 'Image', + imageUrl, + pubDate: parseDate(item.createdAt), + }; + }); + + const list = videoList.concat(imageList); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const link = item.link.replace('www.iwara.tv', 'api.iwara.tv'); + const response = await got({ + method: 'get', + url: link, + headers: { + Cookie: cookie, + }, + }); + const type = item.category; + const imageUrl = item.imageUrl; + const body = response.data.body ? md.render(response.data.body) : ''; + + item.description = art(path.join(__dirname, 'templates/subscriptions.art'), { type, imageUrl, - }), - }; - }) - .get(); + }); + item.description += body; + + return item; + }) + ) + ); ctx.state.data = { - title: `${username}'s iwara Subscription`, + title: `Iwara Subscription`, link: rootUrl, item: items, }; diff --git a/lib/v2/iwara/templates/subscriptions.art b/lib/v2/iwara/templates/subscriptions.art index 61eef898a..80af17471 100644 --- a/lib/v2/iwara/templates/subscriptions.art +++ b/lib/v2/iwara/templates/subscriptions.art @@ -1 +1 @@ -