const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const id = ctx.params.id;
const response = await got({
method: 'get',
url: `https://m.okjike.com/users/${id}`,
headers: {
Referer: `https://m.okjike.com/users/${id}`,
},
});
const html = response.data;
const $ = cheerio.load(html);
const raw = $('[type = "application/json"]').html();
const data = JSON.parse(raw).props.pageProps;
const getLink = (id, type) => {
switch (type) {
case 'REPOST':
return `https://m.okjike.com/reposts/${id}`;
case 'MEDIUM':
return `https://www.okjike.com/medium/${id}`;
default:
return `https://m.okjike.com/originalPosts/${id}`;
}
};
const items = await Promise.all(
data.posts.map(async (item) => {
const typeMap = {
ORIGINAL_POST: '发布',
REPOST: '转发',
ANSWER: '回答',
QUESTION: '提问',
PERSONAL_UPDATE: '创建新主题',
};
let linkTemplate = '';
if (item.linkInfo && item.linkInfo.linkUrl) {
linkTemplate = `${item.linkInfo.title}
`;
}
let imgTemplate = '';
item.pictures &&
item.pictures.forEach((item) => {
imgTemplate += `
`;
});
let content = item.content || (item.linkInfo && item.linkInfo.title) || (item.question && item.question.title) || item.title || '';
content = content.replace(/\r\n|\n|\r/g, '
');
let shortenTitle = '一条动态';
if (content) {
shortenTitle = content.replace(/(
)+/g, ' ');
content = `${content}
`;
}
let repostContent;
if (item.type === 'REPOST') {
const screenNameTemplate = item.target.user ? `@${item.target.user.screenName}` : '';
let repostImgTemplate = '';
item.target.pictures &&
item.target.pictures.forEach((item) => {
repostImgTemplate += ``;
});
repostContent = `