diff --git a/lib/v2/lofter/tag.js b/lib/v2/lofter/tag.js index eb04ae74d9..91c9ec300d 100644 --- a/lib/v2/lofter/tag.js +++ b/lib/v2/lofter/tag.js @@ -1,59 +1,94 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); +const { JSDOM } = require('jsdom'); module.exports = async (ctx) => { const name = ctx.params.name ?? '摄影'; const type = ctx.params.type ?? 'new'; + const pageSize = 20; + const startingIndex = 0; const rootUrl = 'https://www.lofter.com'; - const currentUrl = `${rootUrl}/tag/${name}/${type}`; + const linkUrl = `${rootUrl}/tag/${name}/${type}`; + const apiUrl = `${rootUrl}/dwr/call/plaincall/TagBean.getCommonTagExcellentAuthors.dwr`; const response = await got({ - method: 'get', - url: currentUrl, + method: 'post', + url: apiUrl, + form: { + callCount: 1, + scriptSessionId: '${scriptSessionId}187', + httpSessionId: '', + 'c0-scriptName': 'TagBean', + 'c0-methodName': 'search', + 'c0-id': '0', + 'c0-param0': `string:${name}`, + 'c0-param1': 'number:0', + 'c0-param2': 'string:', + 'c0-param3': `string:${type}`, + 'c0-param4': 'boolean:false', + 'c0-param5': 'number:0', + 'c0-param6': `number:${pageSize}`, + 'c0-param7': `number:${startingIndex}`, + 'c0-param8': 'number:0', + batchId: 493053, + }, }); - const $ = cheerio.load(response.data); + const dom = new JSDOM( + ``, + { + runScripts: 'dangerously', + } + ); + const data = dom.window.dwr.engine.data[2]; - $('.js-digest').remove(); + const title = + { + new: '最新', + date: '日榜', + week: '周榜', + month: '月榜', + total: '最热', + }[type] ?? ''; - let title = $('a.j-crt').text(); - title = title === '最热' ? $('.select-recomm-type').text() : title; + const items = data.map((entry) => { + const post = entry.post; - const items = $('div[data-blogid]') - .map((_, item) => { - item = $(item); + let videos = ''; + if (post.embed) { + const embed = JSON.parse(post.embed); + if (embed.h256Url || embed.video_down_url) { + videos = ``; + } + } - const videos = item - .find('.js-video') - .toArray() - .reduce((accumulator, currentValue) => accumulator + ``, ''); + const images = post.photoLinks + ? JSON.parse(post.photoLinks).reduce((accumulator, currentValue) => accumulator + ``, '') // small | middle | orign + : ''; - const images = item - .find('.imgc img') - .toArray() - .reduce((accumulator, currentValue) => accumulator + ``, ''); + const digest = cheerio.load(post.digest); + const description = digest.text(); - const description = item.find('.js-content.ptag'); - - return { - author: item.attr('data-blognickname'), - link: item.find('.isayc').attr('href'), - title: item.find('.tit').text() || `${item.find('.w-who .ptag').text()}${description.text() ? `:${description.text()}` : ''}`, - pubDate: parseDate(item.find('.isayc').attr('data-time') * 1), - description: videos + images + description.html(), - category: item - .find('.opta .opti') - .toArray() - .map((item) => $(item).text()), - }; - }) - .get(); + return { + author: post.blogInfo.blogNickName, + link: post.blogPageUrl, + title: post.title || `${post.blogInfo.blogNickName}${description ? `:${description}` : ''}`, + pubDate: parseDate(post.publishTime), + description: videos + images + digest.html(), + category: post.tagList, + }; + }); ctx.state.data = { title: `${name} - ${title} | LOFTER`, - link: currentUrl, + link: linkUrl, item: items, }; };