Files
RSSHub/lib/v2/coolapk/utils.js
T
Tony 6d901cb7dc style(eslint): add eslint-unicorn (#14257)
* 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
2024-01-18 20:43:40 +08:00

150 lines
5.2 KiB
JavaScript

const md5 = require('@/utils/md5');
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const dynamicTpye = { 0: '基本动态', 8: '酷图', 9: '评论', 10: '提问', 11: '回答', 12: '图文', 15: '二手', 17: '观点', 20: '交易动态' };
const getRandomDEVICE_ID = () => {
let id = [10, 6, 6, 6, 14];
id = id.map((i) => Math.random().toString(36).substring(2, i));
return id.join('-');
};
const get_app_token = () => {
const DEVICE_ID = getRandomDEVICE_ID();
const now = Math.round(Date.now() / 1000);
const hex_now = '0x' + now.toString(16);
const md5_now = md5(now.toString());
const s = 'token://com.coolapk.market/c67ef5943784d09750dcfbb31020f0ab?' + md5_now + '$' + DEVICE_ID + '&com.coolapk.market';
const md5_s = md5(Buffer.from(s).toString('base64'));
const token = md5_s + DEVICE_ID + hex_now;
return token;
};
const base_url = 'https://api.coolapk.com';
const getHeaders = () => ({
'X-Requested-With': 'XMLHttpRequest',
'X-App-Id': 'com.coolapk.market',
'X-App-Token': get_app_token(),
'X-Sdk-Int': '29',
'X-Sdk-Locale': 'zh-CN',
'X-App-Version': '11.0',
'X-Api-Version': '11',
'X-App-Code': '2101202',
'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 10; Redmi K30 5G MIUI/V12.0.3.0.QGICMXM) (#Build; Redmi; Redmi K30 5G; QKQ1.191222.002 test-keys; 10) +CoolMarket/11.0-2101202',
});
const parseTuwenFromRaw = (raw) =>
raw.map((i) => {
if (i.type === 'text') {
const output = i.message
.split('\n')
.filter((t) => t !== '')
.map((t) => `<p>${t}</p>`)
.join('');
return output;
} else if (i.type === 'image') {
return `<div class="img-container" style="text-align: center;">
<img src="${i.url}">
<p class="image-caption" style="text-align: center;">${i.description}</p></div>`;
} else {
// console.log(i);
return `Unkown type`;
}
});
const parseDynamic = async (item, ctx) => {
const pubDate = parseDate(item.dateline, 'X');
if (item.entityType === 'sponsorCard' || item.shareUrl === undefined) {
return;
}
const itemUrl = item.shareUrl.split('?')[0];
let description, title;
const type = Number.parseInt(item.type);
switch (type) {
case 0:
case 5:
case 7: // external share
case 8:
case 9:
case 10:
case 11:
case 13: // feedType: video (external)
case 15:
case 17:
case 20: {
// //////////////////////////////////////////// 基本内容 ////////////////////////////////////////////
if (item.issummary) {
// 需要爬内容
description = await ctx.cache.tryGet(itemUrl, async () => {
const result = await got(itemUrl, {
headers: getHeaders(),
});
const message = `<p>` + result.data.data?.message.split('\n').join('<br>') + `</p>`;
const picArr = item.picArr.filter(Boolean).map((i) => `<img src="${i}">`); // 若无图片,item.picArr=[""]
return message + picArr.join('');
});
} else {
const picArr = item.picArr.filter(Boolean).map((i) => `<img src="${i}">`);
description = `<p>` + item.message + `</p>` + picArr.join('');
}
const $ = cheerio.load('<div class="title-filter">' + description + '</div>');
title = $('.title-filter').text().trim(); // no need to perform substring because it's will be handled by RSSHub 'TITLE_LENGTH_LIMIT'
// //////////////////////////////////////////// 基本内容结束 ////////////////////////////////////////////
if (type === 17) {
const keys = item.extra_key.split(',');
description += `<p>` + item.vote.message_title + ` 已选${keys.length}项</p>`;
for (const i of item.vote.options) {
if (keys.includes(String(i.id))) {
description += `<p>${i.title}√</p>`;
}
}
} else if (type === 10 || type === 11) {
title = `${item.message_title} 更多:` + title;
}
break;
}
case 12:
title = item.title;
description = await ctx.cache.tryGet(itemUrl, async () => {
const result = await got(itemUrl, {
headers: getHeaders(),
});
const raw = JSON.parse(result.data.data.message_raw_output);
const tags = parseTuwenFromRaw(raw);
return `<img src="${result.data.data.pic}"><hr>` + tags.join('');
});
break;
default:
// console.log(item.type);
// console.log(item);
break;
}
return {
title,
description,
pubDate,
link: item.shareUrl,
guid: itemUrl,
author: item.username,
};
};
module.exports = {
get_app_token,
base_url,
getHeaders,
parseDynamic,
dynamicTpye,
};