* 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
171 lines
6.4 KiB
JavaScript
171 lines
6.4 KiB
JavaScript
const got = require('@/utils/got');
|
|
const cheerio = require('cheerio');
|
|
const { JSDOM } = require('jsdom');
|
|
const { parseDate } = require('@/utils/parse-date');
|
|
const { art } = require('@/utils/render');
|
|
const path = require('path');
|
|
const dayjs = require('dayjs');
|
|
const localizedFormat = require('dayjs/plugin/localizedFormat');
|
|
require('dayjs/locale/zh-cn');
|
|
dayjs.extend(localizedFormat);
|
|
|
|
function nuxtReader(data) {
|
|
let nuxt = {};
|
|
try {
|
|
const dom = new JSDOM(data, {
|
|
runScripts: 'dangerously',
|
|
});
|
|
nuxt = dom.window.__NUXT__.data[0];
|
|
} catch {
|
|
throw new Error('Nuxt 框架信息提取失败,请报告这个问题');
|
|
}
|
|
|
|
return nuxt;
|
|
}
|
|
|
|
function generateImageLink(link) {
|
|
return `<img src="${link}"><br/>`;
|
|
}
|
|
|
|
async function load(link) {
|
|
const response = await got(link);
|
|
|
|
const data = response.data;
|
|
|
|
const $ = cheerio.load(data); // 使用 cheerio 加载返回的 HTML
|
|
const description = $('.description').html();
|
|
|
|
return {
|
|
content: description,
|
|
};
|
|
}
|
|
|
|
async function loadNews(link) {
|
|
const response = await got(link);
|
|
|
|
const data = response.data;
|
|
|
|
const $ = cheerio.load(data);
|
|
let description = $('.detail-body-container').html();
|
|
const date = $('.topics-articleHead__date').text();
|
|
description = description.replaceAll('src="/topics/', 'src="https://www.nintendo.com.hk/topics/');
|
|
return {
|
|
content: description,
|
|
pubDate: parseDate(date, 'YYYY.M.D'),
|
|
};
|
|
}
|
|
|
|
const ProcessItem = (list, caches) =>
|
|
Promise.all(
|
|
list.map(async (item) => {
|
|
const other = await caches.tryGet(item.link, () => load(item.link));
|
|
return { ...item, ...other };
|
|
})
|
|
);
|
|
|
|
const ProcessNews = (list, caches) =>
|
|
Promise.all(
|
|
list.map(async (item) => {
|
|
const other = await caches.tryGet(item.url, () => loadNews('https://www.nintendo.com.hk' + item.url));
|
|
return { ...item, ...other };
|
|
})
|
|
);
|
|
|
|
/*
|
|
Software Item Example
|
|
{
|
|
"nsUid": "70010000027088",
|
|
"gameProductCode": "HAC-P-ADALB",
|
|
"name": "新 超级马力欧兄弟U 豪华版",
|
|
"price": "299",
|
|
"currency": "CNY",
|
|
"images": ["//switch-cn.gtgres.com/global-images/c247f0e0-1654-11ea-9b40-236e671bca9e.png", "//switch-cn.gtgres.com/global-images/c62e52d0-1654-11ea-9b40-236e671bca9e.jpg", "//switch-cn.gtgres.com/global-images/c8ec6160-1654-11ea-9b40-236e671bca9e.jpg", "//switch-cn.gtgres.com/global-images/cb84e690-1654-11ea-9b40-236e671bca9e.jpg", "//switch-cn.gtgres.com/global-images/cdc1b730-1654-11ea-9b40-236e671bca9e.jpg", "//switch-cn.gtgres.com/global-images/cfd57c00-1654-11ea-9b40-236e671bca9e.jpg", "//switch-cn.gtgres.com/global-images/d1fe4f70-1654-11ea-9b40-236e671bca9e.jpg"],
|
|
"movies": [],
|
|
"purchaseUrls": [{
|
|
"key": "JD",
|
|
"url": "https://item.jd.com/100010400326.html"
|
|
}, {
|
|
"key": "TM",
|
|
"url": "https://detail.tmall.com/item.htm?id=608916438547"
|
|
}],
|
|
"descriptionTitle": "2D超级马力欧全新体验,同享欢乐时光",
|
|
"descriptionContent": "《新 超级马力欧兄弟U 豪华版》是2D超级马力欧横版卷轴动作游戏。\\n\\n游戏包括《新 超级路易吉U》和《新 超级马力欧兄弟U》共164个关卡,马力欧一行需要前往被酷霸王占领的桃花公主城堡,利用多种新奇的能力强化,与可靠有趣的伙伴,在丰富多彩的世界中展开冒险。在《新 超级路易吉U》中,关卡难度更高,限时更短,角色跳跃能力更强但也更易打滑,非常适合喜欢挑战的玩家。\\n\\n游戏中有“马力欧”、“路易吉”、“奇诺比奥”、“偷天兔”和“奇诺比珂”5个角色供你选择,其中“奇诺比珂”与“偷天兔”拥有非常适合新手的特殊能力,即使不擅长动作游戏,也可以轻松游玩。\\n\\n除了故事模式外,游戏还提供课题模式、加倍模式和金币对战多种丰富的玩法。游戏支持最多4人一起游玩,快与伙伴分享Joy-Con™,一起趣玩吧!",
|
|
"sizeNum": "2.1",
|
|
"sizeUnit": "GB",
|
|
"playMode": ["tv", "tabletop", "handheld"],
|
|
"normalPlayersNum": {
|
|
"min": 1,
|
|
"max": 4
|
|
},
|
|
"localPlayersNum": {
|
|
"min": 0,
|
|
"max": 0
|
|
},
|
|
"internatPlayersNum": {
|
|
"min": 0,
|
|
"max": 0
|
|
},
|
|
"handleSupport": "Nintendo Switch专业手柄",
|
|
"platform": "Nintendo Switch",
|
|
"developer": "Nintendo",
|
|
"publisher": "任天堂",
|
|
"genre": ["动作", "冒险", "家庭", "多人"],
|
|
"releaseDatetime": 1575907200000,
|
|
"supportLanguages": ["简体中文"],
|
|
"colors": {
|
|
"words": "3c3c3c",
|
|
"entrance": "e60012",
|
|
"background": "e8ebef"
|
|
},
|
|
"copyright": {
|
|
"publishCompany": "上海电子出版有限公司",
|
|
"operateCompany": "深圳市腾讯计算机系统有限公司",
|
|
"copyrightNotice": "© 2012-2019 Nintendo",
|
|
"isbn": "978-7-498-06914-6",
|
|
"gapp": "国新出审[2019]2990号",
|
|
"copyrightNumber": "电出字09-2019-031"
|
|
}
|
|
}
|
|
*/
|
|
const ProcessItemChina = (list, cache) =>
|
|
Promise.all(
|
|
list.map(async (item) => {
|
|
if (!item.link.startsWith('https://www.nintendoswitch.com.cn/software/')) {
|
|
return item;
|
|
}
|
|
const n = await cache.tryGet(item.link, async () => nuxtReader((await got(item.link)).data));
|
|
const software = n.data;
|
|
return {
|
|
...item,
|
|
category: [...software.supportLanguages, ...software.genre, ...software.playMode],
|
|
description: art(path.join(__dirname, 'templates/eshop_cn.art'), {
|
|
item,
|
|
software,
|
|
releaseDatetime: dayjs(software.releaseDatetime).locale('zh-cn').format('lll'),
|
|
}),
|
|
};
|
|
})
|
|
);
|
|
|
|
const ProcessNewsChina = (list, cache) =>
|
|
Promise.all(
|
|
list.map(async (item) => {
|
|
const n = await cache.tryGet(item.link, async () => nuxtReader((await got(item.link)).data));
|
|
return {
|
|
...item,
|
|
description: item.description + n.newsData.content,
|
|
category: n.newsData.category,
|
|
pubDate: parseDate(n.newsData.releaseTime, 'x'),
|
|
};
|
|
})
|
|
);
|
|
|
|
module.exports = {
|
|
ProcessItem,
|
|
ProcessNews,
|
|
ProcessNewsChina,
|
|
ProcessItemChina,
|
|
nuxtReader,
|
|
generateImageLink,
|
|
};
|