* style: add eslint-unicorn * style: fix unicorn/no-useless-spread * style: fix unicorn/no-useless-promise-resolve-reject * style: fix unicorn/no-for-loop * fix: codeql bad HTML filtering regexp * fix: codeql incomplete replace * fix: unicorn/no-abusive-eslint-disable * style: fix unicorn/no-new-array * style: fix unicorn/no-typeof-undefined * style: fix unicorn/no-zero-fractions * style: fix unicorn/no-empty-file * style: fix unicorn/prefer-date-now * revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js * style: fix unicorn/prefer-array-find * style: fix unicorn/prefer-array-flat * style: fix unicorn/prefer-array-flat-map * style: fix unicorn/prefer-at * style: fix unicorn/prefer-string-starts-ends-with * style: fix unicorn/prefer-includes * fix: codeql URL substring sanitization * style: fix unicorn/prefer-optional-catch-binding * style: fix unicorn/catch-error-name * style: fix unicorn/escape-case * style: fix unicorn/prefer-native-coercion-functions * style: fix unicorn/prefer-regexp-test * style: fix unicorn/require-array-join-separator * style: fix unicorn/prefer-math-trunc * style: fix unicorn/prefer-negative-index * style: fix unicorn/prefer-dom-node-dataset * style: fix unicorn/prefer-dom-node-text-content * style: fix unicorn/prefer-query-selector * style: fix unicorn/no-array-for-each * style: fix unicorn/no-negated-condition * style: fix unicorn/prefer-add-event-listener * style: fix unicorn/import-style * style: fix prefer-regex-literals * style: disable unicorn/no-useless-switch-case * style: disable unicorn/text-encoding-identifier-case * style: fix unicorn/prefer-set-has * style: fix unicorn/prefer-spread * revert: auto fix on lib/routes/universities/ynnu/edu/base64.js * style: fix unicorn/no-useless-undefined * style: fix unicorn/no-array-push-push * style: fix unicorn/no-useless-undefined again * style: fix unicorn/no-lonely-if * style: fix unicorn/prefer-reflect-apply * style: fix unicorn/switch-case-braces * style: fix unicorn/prefer-switch * style: fix unicorn/prefer-array-some * fix: deepscan UNUSED_VAR_ASSIGN * style: fix unicorn/prefer-ternary * fix: follow-up of unicorn/prefer-ternary * revert: auto fix of unicorn/prefer-string-slice for substring() * style: disable unicorn/prefer-string-slice fix: auto fix slice over deprecated substr * style: fix unicorn/throw-new-error * style: fix unicorn/filename-case * test: fix dateParser renaming * style: fix unicorn/better-regex * style: fix unicorn/prefer-string-replace-all * fix(deps): add sanitize-html * style: fix no-prototype-builtins * style: fix unicorn/consistent-destructuring * style: fix unicorn/consistent-function-scoping * style: fix unicorn/prefer-regexp-test * style: fix unicorn/prefer-logical-operator-over-ternary * style: fix unicorn/no-array-callback-reference * style: add prefer-object-has-own * style: warn unicorn/no-empty-file * style: fix unicorn/prefer-number-properties * style: fix no-useless-undefined again * style: fix unicorn/numeric-separators-style * style: disable unicorn/no-array-callback-reference false postive with cheerio
116 lines
3.9 KiB
JavaScript
116 lines
3.9 KiB
JavaScript
const got = require('@/utils/got');
|
|
const config = require('@/config').value;
|
|
const utils = require('./utils');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
|
|
module.exports = async (ctx) => {
|
|
const cookie = config.zhihu.cookies;
|
|
if (cookie === undefined) {
|
|
throw new Error('缺少知乎用户登录后的 Cookie 值');
|
|
}
|
|
|
|
const response = await got({
|
|
method: 'get',
|
|
url: `https://www.zhihu.com/api/v3/moments?desktop=true`,
|
|
headers: {
|
|
Cookie: cookie,
|
|
},
|
|
});
|
|
const feeds = response.data.data;
|
|
|
|
const urlBase = 'https://zhihu.com';
|
|
const buildLink = (e) => {
|
|
if (!e || !e.target || !e.target.type) {
|
|
return '';
|
|
}
|
|
const id = e.target.id;
|
|
switch (e.target.type) {
|
|
case 'answer': {
|
|
const questionId = e.target.question.id;
|
|
return `${urlBase}/question/${questionId}/answer/${id}`;
|
|
}
|
|
case 'article':
|
|
return e.target.url;
|
|
case 'question':
|
|
return `${urlBase}/question/${id}`;
|
|
}
|
|
return '';
|
|
};
|
|
|
|
/**
|
|
* Returns one non-undefined/null element from a list of items
|
|
* make sure there exists at least one element that is non-undefined
|
|
*
|
|
* @param {Array} list a list of items to be filtered
|
|
*/
|
|
const getOne = (list) => list.find((e) => e !== undefined && e !== null);
|
|
|
|
const buildActors = (e) => {
|
|
const actors = e.actors;
|
|
if (!actors) {
|
|
return '';
|
|
}
|
|
return actors.map((e) => e.name).join(', ');
|
|
};
|
|
|
|
const getContent = (content) => {
|
|
if (!content || !Array.isArray(content)) {
|
|
return content;
|
|
}
|
|
// content can be a string or an array of objects
|
|
return (
|
|
content
|
|
.map((e) => e.content)
|
|
.filter((e) => e instanceof String && !!e)
|
|
// some content may not be wrapped in tag, it will cause error when parsing
|
|
.map((e) => `<div>${e}</div>`)
|
|
.join('')
|
|
);
|
|
};
|
|
|
|
const buildItem = (e) => {
|
|
if (!e || !e.target) {
|
|
return {};
|
|
}
|
|
return {
|
|
title: `${e.action_text_tpl.replace('{}', buildActors(e))}: ${getOne([e.target.title, e.target.question ? e.target.question.title : ''])}`,
|
|
description: utils.ProcessImage(`<div>${getOne([e.target.content_html, getContent(e.target.content), e.target.detail, e.target.excerpt, ''])}</div>`),
|
|
pubDate: parseDate(e.updated_time * 1000),
|
|
link: buildLink(e),
|
|
author: e.target.author ? e.target.author.name : '',
|
|
guid: this.link,
|
|
};
|
|
};
|
|
|
|
const out = feeds
|
|
.filter((e) => e && e.type && e.type !== 'feed_advert')
|
|
.map((e) => {
|
|
if (e && e.type && e.type === 'feed_group') {
|
|
// A feed group contains a list of feeds whose structure is the same as a single feed
|
|
const title = e.group_text.replace('{LIST_COUNT}', e.list.length);
|
|
const description =
|
|
e.list && Array.isArray(e.list)
|
|
? e.list
|
|
.map((e) => buildItem(e))
|
|
.map((e) => `<a href="${e.link}"><b>${e.title}</b></a><br><p>${e.description}</p><br>`)
|
|
.join('')
|
|
: '';
|
|
const pubDate = e.list && Array.isArray(e.list) && e.list.length > 0 ? parseDate(e.list[0].updated_time * 1000) : new Date();
|
|
const guid = `${title} ${pubDate}`;
|
|
return {
|
|
title,
|
|
description,
|
|
pubDate,
|
|
guid,
|
|
};
|
|
}
|
|
return buildItem(e);
|
|
});
|
|
|
|
ctx.state.data = {
|
|
title: `知乎关注动态`,
|
|
link: `https://www.zhihu.com/follow`,
|
|
item: out,
|
|
};
|
|
};
|