Files
RSSHub/lib/routes/owspace/read.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

142 lines
4.0 KiB
JavaScript

const got = require('@/utils/got');
const md5 = require('@/utils/md5');
const qs = require('querystring');
const cheerio = require('cheerio');
const secret = 'fk4iy@98(*Y98fh-^o)re+wg=';
const typeMap = {
1: '文字',
2: '影像',
3: '声音',
4: '单向历',
6: '谈论',
};
const signQuery = (query) => {
query.sign = md5(['apiname=', query.a, 'device_id=', query.device_id, 'time=', query.time, secret].join(''));
};
const generateFullText = async (item) => {
const model = Number.parseInt(item.model);
const fullTextResponse = await got.get(item.html5);
const $ = cheerio.load(fullTextResponse.data);
const content = $('#singleArticle');
const describe = $('.articleTit>.describe');
let intro = '';
if (item.excerpt) {
intro += '<p>' + item.excerpt + '</p>';
}
if (item.thumbnail) {
intro += `<p><img src="${item.thumbnail}"></p>`;
}
if (describe.length > 0) {
intro += '<blockquote>' + describe.html() + '</blockquote>';
}
if (intro) {
intro += '<hr>';
}
// hide redundant meta
content.find('.articleTit').remove();
if (model === 2) {
// make video visible
content.find('.vBox').removeAttr('style');
content.find('.vBox video').attr('preload', 'metadata');
}
if (model === 3) {
// make audio visible
const audioContainer = content.find('.fmCon').removeAttr('style');
// generate audio player
$('<audio controls="controls"></audio>').attr('src', item.fm).attr('preload', 'metadata').prependTo(audioContainer);
content.find('.fmCon>.graphic').remove();
content.find('.fmCon>.fmDetail').remove();
}
return intro + content.html();
};
const handleItem = (ctx) => async (item) => {
if (Number.parseInt(item.model) === 4) {
const result = {
title: item.title.substring(0, 4) + '年' + Number.parseInt(item.title.substring(4, 6)) + '月' + Number.parseInt(item.title.substring(6, 8)) + '日',
link: item.html5,
description: `<img src="${item.thumbnail}">`,
pubDate: new Date(Number(item.create_time) * 1000).toUTCString(),
author: item.author,
};
return result;
} else {
const url = item.html5;
const cache = await ctx.cache.get(url);
if (cache) {
return JSON.parse(cache);
}
const result = {
title: item.title.replace('\r\n', ''),
link: item.html5,
description: await generateFullText(item),
pubDate: new Date(Number(item.create_time) * 1000).toUTCString(),
author: item.author,
};
ctx.cache.set(url, JSON.stringify(result));
return result;
}
};
module.exports = async (ctx) => {
const selectedType = ctx.params.type === undefined ? 0 : Number.parseInt(ctx.params.type);
const queryData = {
c: 'api2',
a: 'getList',
p: '1',
model: selectedType,
client: 'android',
version: '1.6.3',
time: Math.round(Date.now() / 1000),
device_id: 'null',
show_sdv: '0',
};
signQuery(queryData);
const response = await got({
method: 'get',
url: 'http://static.owspace.com/',
searchParams: qs.stringify(queryData),
headers: {
'User-Agent': 'com.onewaystreet.weread/14',
},
});
if (!response.data.status || response.data.status !== 'ok') {
throw response.data.msg ? 'api error: ' + response.data.msg : 'api error';
}
const responseItemArray = response.data.datas.filter((item) => Number.parseInt(item.model) !== 5);
const feedItemArray = await Promise.all(responseItemArray.map(handleItem(ctx)));
const selectedTypeName = typeMap[selectedType];
ctx.state.data = {
title: selectedTypeName ? `${selectedTypeName} - 单读` : '单读',
link: 'http://www.owspace.com/read.html',
item: feedItemArray,
};
};