From 608aa675cf7d840cf0d174e35a9ca1cb01fb0c57 Mon Sep 17 00:00:00 2001 From: Yunfi Date: Tue, 3 Sep 2024 21:35:55 +0800 Subject: [PATCH] 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 --------- --- lib/routes-deprecated/gamersky/ent.js | 89 ------------------------ lib/routes-deprecated/gamersky/news.js | 34 ---------- lib/routes/gamersky/ent.ts | 57 ++++++++++++++++ lib/routes/gamersky/namespace.ts | 10 +++ lib/routes/gamersky/news.ts | 91 +++++++++++++++++++++++++ lib/routes/gamersky/review.ts | 80 ++++++++++++++++++++++ lib/routes/gamersky/utils.ts | 94 ++++++++++++++++++++++++++ 7 files changed, 332 insertions(+), 123 deletions(-) delete mode 100644 lib/routes-deprecated/gamersky/ent.js delete mode 100644 lib/routes-deprecated/gamersky/news.js create mode 100644 lib/routes/gamersky/ent.ts create mode 100644 lib/routes/gamersky/namespace.ts create mode 100644 lib/routes/gamersky/news.ts create mode 100644 lib/routes/gamersky/review.ts create mode 100644 lib/routes/gamersky/utils.ts diff --git a/lib/routes-deprecated/gamersky/ent.js b/lib/routes-deprecated/gamersky/ent.js deleted file mode 100644 index 566423aa2..000000000 --- a/lib/routes-deprecated/gamersky/ent.js +++ /dev/null @@ -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, - }; -}; diff --git a/lib/routes-deprecated/gamersky/news.js b/lib/routes-deprecated/gamersky/news.js deleted file mode 100644 index b0d17c330..000000000 --- a/lib/routes-deprecated/gamersky/news.js +++ /dev/null @@ -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, - }; -}; diff --git a/lib/routes/gamersky/ent.ts b/lib/routes/gamersky/ent.ts new file mode 100644 index 000000000..9f376c8ec --- /dev/null +++ b/lib/routes/gamersky/ent.ts @@ -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, + }; +} diff --git a/lib/routes/gamersky/namespace.ts b/lib/routes/gamersky/namespace.ts new file mode 100644 index 000000000..ac8034d74 --- /dev/null +++ b/lib/routes/gamersky/namespace.ts @@ -0,0 +1,10 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'GamerSky', + url: 'gamersky.com', + + zh: { + name: '游民星空', + }, +}; diff --git a/lib/routes/gamersky/news.ts b/lib/routes/gamersky/news.ts new file mode 100644 index 000000000..57b6c5d26 --- /dev/null +++ b/lib/routes/gamersky/news.ts @@ -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, + }; +} diff --git a/lib/routes/gamersky/review.ts b/lib/routes/gamersky/review.ts new file mode 100644 index 000000000..eb2fd8a64 --- /dev/null +++ b/lib/routes/gamersky/review.ts @@ -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, + }; +} diff --git a/lib/routes/gamersky/utils.ts b/lib/routes/gamersky/utils.ts new file mode 100644 index 000000000..b8968fb35 --- /dev/null +++ b/lib/routes/gamersky/utils.ts @@ -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( + `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; + +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; +}