fix: lofter tag 绕过登录需求 (#14453)

* Fix lofter tag

* Apply suggestions from code review

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* fix: import jsdom, correct URL of key 'link'

* fix: post encoded formdata

---------
This commit is contained in:
LucunJi
2024-02-18 23:12:42 +08:00
committed by GitHub
co-authored by Tony
parent 8c077f966d
commit 04b7d73a6c
+69 -34
View File
@@ -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(
`<script>if (dwr == null) var dwr = {};
if (dwr.engine == null) dwr.engine = {};
dwr.engine._remoteHandleCallback = function () {
this.data = arguments;
};
${response.data}</script>`,
{
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 = `<video src="${embed.h256Url ?? embed.video_down_url}" poster="${embed.video_img_url ?? ''}" controls="controls"></video>`;
}
}
const videos = item
.find('.js-video')
.toArray()
.reduce((accumulator, currentValue) => accumulator + `<video src="${$(currentValue).attr('data-videourl')}" poster="${$(currentValue).attr('data-videoimg')}" controls="controls"></video>`, '');
const images = post.photoLinks
? JSON.parse(post.photoLinks).reduce((accumulator, currentValue) => accumulator + `<img src="${currentValue.orign}"/>`, '') // small | middle | orign
: '';
const images = item
.find('.imgc img')
.toArray()
.reduce((accumulator, currentValue) => accumulator + `<img src="${$(currentValue).attr('data-origin') || $(currentValue).attr('src')}"/>`, '');
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,
};
};