Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add blizzard/news-cn route #18058

Merged
merged 14 commits into from
Jan 11, 2025
Merged
133 changes: 133 additions & 0 deletions lib/routes/blizzard/news-cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: 'news-cn/:category',
categories: ['game'],
example: 'blizzard/news-cn/ow',
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved
parameters: { category: '游戏类别, 默认为 ow' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['ow.blizzard.cn', 'wow.blizzard.cn', 'hs.blizzard.cn'],
target: '/news-cn/',
},
],
name: '暴雪游戏国服新闻',
maintainers: ['zhangpeng2k'],
description: `
| 守望先锋 | 炉石传说 | 魔兽世界 |
|----------|----------|---------|
| ow | hs | wow |
`,
handler,
};

const categoryNames = {
ow: '守望先锋',
hs: '炉石传说',
wow: '魔兽世界',
};

/* 列表解析逻辑 */
const parsers = {
ow: ($) =>
$('.list-data-container .list-item-container').toArray().map((item) => {
item = $(item);
return {
title: item.find('.content-title').text(),
link: item.find('.fill-link').attr('href'),
description: item.find('.content-intro').text(),
pubDate: parseDate(item.find('.content-date').text()),
image: item.find('.item-pic').attr('src'),
};
}),
hs: ($) =>
$('.article-container>a').toArray().map((item) => {
item = $(item);
return {
title: item.find('.title').text(),
link: item.attr('href'),
description: item.find('.desc').text(),
pubDate: parseDate(item.find('.date').attr('data-time')),
image: item.find('.article-img img').attr('src'),
};
}),
wow: ($) => $('.Pane-list>a').toArray().map((item) => {
item = $(item);
return {
title: item.find('.list-title').text(),
link: item.attr('href'),
description: item.find('.list-desc').text(),
pubDate: parseDate(item.find('.list-time').attr('data-time')),
image: item.find('.img-box img').attr('src'),
};
}),
};

// 详情页解析逻辑
const detailParsers = {
ow: ($) => $('.detail-content-box').first().html(),
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved
hs: ($) => $('.article').first().html(),
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved
wow: ($) => $('.detail').first().html(),
};

function getList(category, $) {
return parsers[category] ? parsers[category]($) : [];
}

async function fetchDetail(item, category) {
Fixed Show fixed Hide fixed
return await cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = load(response);

const parseDetail = detailParsers[category];
item.description = parseDetail($);
return item;
});
}

async function handler(ctx) {
const category = ctx.req.param('category') || 'ow';
if (!categoryNames[category]) {
return {
title: '错误的类别',
description: '您请求的类别不存在。',
item: [],
};
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved
}

const rootUrl = `https://${category}.blizzard.cn/news`;

const response = await ofetch(rootUrl);
const $ = load(response);

const list = getList(category, $);
if (!list.length) {
return {
title: `${categoryNames[category]}新闻`,
description: '未找到相关新闻。',
item: [],
};
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved
}

const items = await Promise.all(
list.map((item) => fetchDetail(item, category))
);

return {
title: `${categoryNames[category]}新闻`,
link: rootUrl,
item: items,
};
}
7 changes: 7 additions & 0 deletions lib/routes/overwatch-cn/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved
zhangpeng2k marked this conversation as resolved.
Show resolved Hide resolved

export const namespace: Namespace = {
name: '守望先锋国服官方网站新闻',
url: 'ow.blizzard.cn/news',
lang: 'zh-CN',
};
71 changes: 71 additions & 0 deletions lib/routes/overwatch-cn/news.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/news',
categories: ['game'],
example: '/overwatch-cn/news',
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['ow.blizzard.cn', 'ow.blizzard.cn/news'],
target: '/news',
},
],
name: 'News',
maintainers: ['zhangpeng2k'],
handler,
};

async function handler() {
const rootUrl = `https://ow.blizzard.cn/news`;

const response = await ofetch(rootUrl);
const $ = load(response);

const list = $('.list-data-container .list-item-container')
.toArray()
.map((item) => {
item = $(item);
const title = item.find('.content-title').text();
const link = item.find('.fill-link').attr('href');
const description = item.find('.content-intro').text();
const pubDate = parseDate(item.find('.content-date').text());
const image = item.find('.item-pic').attr('src');

return {
title,
link,
description,
image,
pubDate,
};
});

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = load(response);
item.description = $('.detail-content-box').first().html();
return item;
})
)
);

return {
title: `守望先锋官方网站新闻`,
link: `https://ow.blizzard.cn/news`,
item: items,
};
};
Loading