feat(route): add gamersky (#16594)

* feat(route): add gamersky

* doc: add default value for 'type' parameter in gamersky news route

* fix: type

* chore: remove deprecated route

* fix: use jsonp api & remove junk

* fix: combine duplicated code

* fix: restore ent

---------
This commit is contained in:
Yunfi
2024-09-03 21:35:55 +08:00
committed by GitHub
parent a532f7ee71
commit 608aa675cf
7 changed files with 332 additions and 123 deletions
-89
View File
@@ -1,89 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const map = new Map([
['qysj', { title: '趣囧时间', suffix: 'ent/qw' }],
['ymyy', { title: '游民影院', suffix: 'wenku/movie' }],
['ygtx', { title: '游观天下', suffix: 'ent/discovery' }],
['bztk', { title: '壁纸图库', suffix: 'ent/wp' }],
['ympd', { title: '游民盘点', suffix: 'wenku' }],
['ymfl', { title: '游民福利', suffix: 'ent/xz/' }],
]);
module.exports = async (ctx) => {
const category = ctx.params.category;
const suffix = map.get(category).suffix;
const title = map.get(category).title;
const url = `https://www.gamersky.com/${suffix}`;
const response = await got({
method: 'get',
url,
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('ul.pictxt.contentpaging li')
.slice(0, 10)
.map(function () {
const info = {
title: $(this).find('div.tit a').text(),
link: $(this).find('div.tit a').attr('href'),
pubDate: new Date($(this).find('.time').text()).toUTCString(),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const itemUrl = info.link.startsWith('https://') ? info.link : `https://www.gamersky.com/${info.link}`;
const pubDate = info.pubDate;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return JSON.parse(cache);
}
const response = await got.get(itemUrl);
const $ = cheerio.load(response.data);
let next_pages = $('div.page_css a')
.map(function () {
return $(this).attr('href');
})
.get();
next_pages = next_pages.slice(0, -1);
const des = await Promise.all(
next_pages.map(async (next_page) => {
const response = await got.get(next_page);
const $ = cheerio.load(response.data);
$('div.page_css').remove();
return $('.Mid2L_con').html().trim();
})
);
const description = des.join('');
const single = {
title,
link: itemUrl,
description,
pubDate,
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return single;
})
);
ctx.state.data = {
title: `游民娱乐-${title}`,
link: url,
item: out,
};
};
-34
View File
@@ -1,34 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const response = await got({
method: 'get',
url: 'https://www.gamersky.com/news/',
headers: {
Referer: 'https://www.gamersky.com/news/',
},
});
const data = response.data;
const $ = cheerio.load(data);
const out = $('.Mid2L_con li')
.slice(0, 10)
.map(function () {
const info = {
title: $(this).find('.tt').text(),
link: $(this).find('.tt').attr('href'),
pubDate: new Date($(this).find('.time').text()).toUTCString(),
description: $(this).find('.txt').text(),
};
return info;
})
.get();
ctx.state.data = {
title: '游民星空-今日推荐',
link: 'https://www.gamersky.com/news/',
item: out,
};
};
+57
View File
@@ -0,0 +1,57 @@
import type { Route } from '@/types';
import type { Context } from 'hono';
import { getArticleList, parseArticleList, getArticle, mdTableBuilder } from './utils';
const idNameMap = new Map([
['all', { title: '热点图文', suffix: 'ent', nodeId: '20107' }],
['qw', { title: '趣囧时间', suffix: 'ent/qw', nodeId: '20113' }],
['movie', { title: '游民影院', suffix: 'wenku/movie', nodeId: '20111' }],
['discovery', { title: '游观天下', suffix: 'ent/discovery', nodeId: '20114' }],
['wp', { title: '壁纸图库', suffix: 'ent/wp', nodeId: '20117' }],
['wenku', { title: '游民盘点', suffix: 'wenku', nodeId: '20106' }],
['xz', { title: '游民福利', suffix: 'ent/xz', nodeId: '20119' }],
]);
export const route: Route = {
path: '/ent/:category?',
categories: ['game'],
example: '/gamersky/ent/xz',
parameters: {
type: '分类类型,留空为 `all`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: Object.entries(idNameMap).map(([type, { title, suffix }]) => ({
title,
source: [`www.gamersky.com/${suffix}`],
target: `/ent/${type}`,
})),
name: '娱乐',
maintainers: ['LogicJake'],
description: mdTableBuilder(Object.entries(idNameMap).map(([type, { title, nodeId }]) => ({ type, name: title, nodeId }))),
handler,
};
async function handler(ctx: Context) {
const category = ctx.req.param('category') ?? 'all';
const idName = idNameMap.get(category);
if (!idName) {
throw new Error(`Invalid type: ${category}`);
}
const response = await getArticleList(idName.nodeId);
const list = parseArticleList(response);
const fullTextList = await Promise.all(list.map((item) => getArticle(item)));
return {
title: `${idName.title} - 游民娱乐`,
link: `https://www.gamersky.com/${idName.suffix}`,
item: fullTextList,
};
}
+10
View File
@@ -0,0 +1,10 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: 'GamerSky',
url: 'gamersky.com',
zh: {
name: '游民星空',
},
};
+91
View File
@@ -0,0 +1,91 @@
import type { Route } from '@/types';
import type { Context } from 'hono';
import { getArticleList, parseArticleList, getArticle, mdTableBuilder } from './utils';
const idNameMap = [
{
type: 'today',
name: '今日推荐',
nodeId: '11007',
},
{
name: '单机电玩',
type: 'pc',
nodeId: '129',
},
{
name: 'NS',
type: 'ns',
nodeId: '21160',
},
{
name: '手游',
type: 'mobile',
nodeId: '20260',
},
{
name: '网游',
type: 'web',
nodeId: '20225',
},
{
name: '业界',
type: 'industry',
nodeId: '21163',
},
{
name: '硬件',
type: 'hardware',
nodeId: '20070',
},
{
name: '科技',
type: 'tech',
nodeId: '20547',
},
];
export const route: Route = {
path: '/news/:type?',
categories: ['game'],
example: '/gamersky/news/pc',
parameters: {
type: '资讯类型,见表,默认为 `pc`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.gamersky.com/news'],
target: '/news',
},
],
name: '资讯',
maintainers: ['yy4382'],
description: mdTableBuilder(idNameMap),
handler,
};
async function handler(ctx: Context) {
const type = ctx.req.param('type') ?? 'pc';
const idName = idNameMap.find((item) => item.type === type);
if (!idName) {
throw new Error(`Invalid type: ${type}`);
}
const response = await getArticleList(idName.nodeId);
const list = parseArticleList(response);
const fullTextList = await Promise.all(list.map((item) => getArticle(item)));
return {
title: `${idName.name} - 游民星空`,
link: 'https://www.gamersky.com/news',
item: fullTextList,
};
}
+80
View File
@@ -0,0 +1,80 @@
import type { Route } from '@/types';
import type { Context } from 'hono';
import { getArticleList, parseArticleList, getArticle, mdTableBuilder } from './utils';
const idNameMap = [
{
type: 'pc',
name: '单机',
nodeId: '20465',
},
{
type: 'tv',
name: '电视',
nodeId: '20466',
},
{
type: 'indie',
name: '独立游戏',
nodeId: '20922',
},
{
type: 'web',
name: '网游',
nodeId: '20916',
},
{
type: 'mobile',
name: '手游',
nodeId: '20917',
},
{
type: 'all',
name: '全部评测',
nodeId: '20915',
},
];
export const route: Route = {
path: '/review/:type?',
categories: ['game'],
example: '/gamersky/review/pc',
parameters: {
type: '评测类型,可选值为 `pc`、`tv`、`indie`、`web`、`mobile`、`all`,默认为 `pc`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.gamersky.com/review'],
target: '/review',
},
],
name: '评测',
maintainers: ['yy4382'],
description: mdTableBuilder(idNameMap),
handler,
};
async function handler(ctx: Context) {
const type = ctx.req.param('type') ?? 'pc';
const idName = idNameMap.find((item) => item.type === type);
if (!idName) {
throw new Error(`Invalid type: ${type}`);
}
const response = await getArticleList(idName.nodeId);
const list = parseArticleList(response);
const fullTextList = await Promise.all(list.map((item) => getArticle(item)));
return {
title: `${idName.name} - 游民星空评测`,
link: 'https://www.gamersky.com/review',
item: fullTextList,
};
}
+94
View File
@@ -0,0 +1,94 @@
import type { DataItem } from '@/types';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
interface idNameMap {
type: string;
name: string;
nodeId: string;
suffix?: string;
}
interface ArticleList {
status: string;
totalPages: number;
body: string;
}
export const getArticleList = async (nodeId) => {
const response = await ofetch<ArticleList>(
`https://db2.gamersky.com/LabelJsonpAjax.aspx?${new URLSearchParams({
jsondata: JSON.stringify({
type: 'updatenodelabel',
isCache: true,
cacheTime: 60,
nodeId,
isNodeId: 'true',
page: 1,
}),
})}`,
{
parseResponse: (txt) => JSON.parse(txt.match(/\((.+)\);/)?.[1] ?? '{}'),
}
);
return response.body;
};
export const parseArticleList = (response: string) => {
const $ = load(response);
return $('li')
.toArray()
.map((item) => {
const ele = $(item);
const a = ele.find('.tt').length ? ele.find('.tt') : ele.find('a');
const title = a.text();
const link = a.attr('href');
const pubDate = timezone(parseDate(ele.find('.time').text()), 8);
const description = ele.find('.txt').text();
if (!link) {
return;
}
return {
title,
link,
pubDate,
description,
};
})
.filter((item) => item !== undefined) satisfies DataItem[];
};
export const getArticle = (item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = load(response);
const content = $('.Mid2L_con, .MidLcon');
content.find('.appGameBuyCardIframe, .GSAppButton, .Mid2L_down').remove();
content.find('a').each((_, item) => {
if (item.attribs.href?.startsWith('https://www.gamersky.com/showimage/id_gamersky.shtml?')) {
item.attribs.href = item.attribs.href.replace('https://www.gamersky.com/showimage/id_gamersky.shtml?', '');
}
});
content.find('img').each((_, item) => {
if (item.attribs.src === 'http://image.gamersky.com/webimg13/zhuanti/common/blank.png') {
item.attribs.src = item.attribs['data-origin'];
} else if (item.attribs.src.endsWith('_S.jpg')) {
item.attribs.src = item.attribs.src.replace('_S.jpg', '.jpg');
}
});
content.find('.Slides li').each((_, item) => {
if (item.attribs.style === 'display: none;') {
item.attribs.style = 'display: list-item;';
}
});
item.description = content.html() || item.description;
return item satisfies DataItem;
}) as Promise<DataItem>;
export function mdTableBuilder(data: idNameMap[]) {
const table = '|' + data.map((item) => `${item.type}|`).join('') + '\n|' + Array(data.length).fill('---|').join('') + '\n|' + data.map((item) => `${item.name}|`).join('') + '\n';
return table;
}