mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-03 10:38:03 +08:00
feat: add common-utils (#2374)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
|
const { toTitleCase } = require('@/utils/common-utils');
|
||||||
const dayjs = require('dayjs');
|
const dayjs = require('dayjs');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
@@ -13,7 +14,7 @@ module.exports = async (ctx) => {
|
|||||||
|
|
||||||
locations.split(',').forEach(function(pair) {
|
locations.split(',').forEach(function(pair) {
|
||||||
const country = pair.split('+')[0];
|
const country = pair.split('+')[0];
|
||||||
const city = titleCase(pair.split('+')[1]);
|
const city = toTitleCase(pair.split('+')[1]);
|
||||||
url += `{"country":"${country}","city":"${city}"},`;
|
url += `{"country":"${country}","city":"${city}"},`;
|
||||||
|
|
||||||
host += `/${country}/${city.replace(' ', '_')}`;
|
host += `/${country}/${city.replace(' ', '_')}`;
|
||||||
@@ -43,12 +44,3 @@ module.exports = async (ctx) => {
|
|||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const titleCase = (str) =>
|
|
||||||
str
|
|
||||||
.toLowerCase()
|
|
||||||
.split(' ')
|
|
||||||
.map(function(word) {
|
|
||||||
return word.replace(word[0], word[0].toUpperCase());
|
|
||||||
})
|
|
||||||
.join(' ');
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
|
const { toTitleCase } = require('@/utils/common-utils');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
@@ -34,19 +35,27 @@ module.exports = async (ctx) => {
|
|||||||
const result = list.map((item) => {
|
const result = list.map((item) => {
|
||||||
const $ = cheerio.load(item);
|
const $ = cheerio.load(item);
|
||||||
return {
|
return {
|
||||||
title: $('.product_title').text(),
|
title: $('.product_title')
|
||||||
|
.text()
|
||||||
|
.trim(),
|
||||||
url: 'https://www.metacritic.com' + $('.product_title > a').attr('href'),
|
url: 'https://www.metacritic.com' + $('.product_title > a').attr('href'),
|
||||||
metascore: $('.brief_metascore').text(),
|
metascore: $('.brief_metascore')
|
||||||
userscore: $('.textscore').text(),
|
.text()
|
||||||
date: $('.release_date > .data').text(),
|
.trim(),
|
||||||
|
userscore: $('.textscore')
|
||||||
|
.text()
|
||||||
|
.trim(),
|
||||||
|
date: $('.release_date > .data')
|
||||||
|
.text()
|
||||||
|
.trim(),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.state.data = {
|
ctx.state.data = {
|
||||||
title: `Metacritic ${ctx.params.platform} ${title}`,
|
title: toTitleCase(`Metacritic ${ctx.params.platform} games ${title}`),
|
||||||
link: url,
|
link: url,
|
||||||
item: result.map((item) => ({
|
item: result.map((item) => ({
|
||||||
title: `${item.metascore === 'tbd' ? '' : [item.metascore]} ${item.title}`,
|
title: `${item.metascore === 'tbd' ? '' : '[' + item.metascore + ']'} ${item.title}`,
|
||||||
description: `Release Date: ${item.date} <br> Metacritic Score: ${item.metascore} <br> User Score: ${item.userscore} <br>`,
|
description: `Release Date: ${item.date} <br> Metacritic Score: ${item.metascore} <br> User Score: ${item.userscore} <br>`,
|
||||||
link: item.url,
|
link: item.url,
|
||||||
})),
|
})),
|
||||||
|
|||||||
11
lib/utils/common-utils.js
Normal file
11
lib/utils/common-utils.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// convert a string into title case
|
||||||
|
const toTitleCase = (str) =>
|
||||||
|
str
|
||||||
|
.toLowerCase()
|
||||||
|
.split(' ')
|
||||||
|
.map((word) => word.replace(word[0], word[0].toUpperCase()))
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
toTitleCase,
|
||||||
|
};
|
||||||
7
test/utils/common-utils.js
Normal file
7
test/utils/common-utils.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const utils = require('../../lib/utils/common-utils');
|
||||||
|
|
||||||
|
describe('common-utils', () => {
|
||||||
|
it('toTitleCase', async () => {
|
||||||
|
expect(utils.toTitleCase('RSSHub IS AS aweSOme aS henry')).toBe('Rsshub Is As Awesome As Henry');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user