Skip to content

Commit

Permalink
fix(blizzard): 新闻路由报错不能使用 (#17956)
Browse files Browse the repository at this point in the history
* fix(blizzard): 新闻路由报错不能使用

* fix(blizzard): 修改 cache key
  • Loading branch information
ueiu authored Dec 22, 2024
1 parent d525693 commit dc32d68
Showing 1 changed file with 120 additions and 31 deletions.
151 changes: 120 additions & 31 deletions lib/routes/blizzard/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ export const route: Route = {
| Diablo II: Resurrected | diablo2 |
| Diablo III | diablo3 |
| Diablo IV | diablo4 |
| Diablo: Immortal | diablo-immortal |
| Diablo Immortal | diablo-immortal |
| Hearthstone | hearthstone |
| Heroes of the Storm | heroes-of-the-storm |
| Overwatch 2 | overwatch |
| StarCraft: Remastered | starcraft |
| StarCraft II | starcraft2 |
| World of Warcraft | world-of-warcraft |
| Warcraft III: Reforged | warcraft3 |
| Warcraft 3: Reforged | warcraft3 |
| Warcraft Rumble | warcraft-rumble |
| Battle.net | battlenet |
| BlizzCon | blizzcon |
| Inside Blizzard | blizzard |
Expand All @@ -59,49 +60,137 @@ export const route: Route = {
| 繁體中文 | zh-tw |`,
};

const GAME_MAP = {
diablo2: {
key: 'diablo2',
value: 'diablo-2-resurrected',
id: 'blt54fbd3787a705054',
},
diablo3: {
key: 'diablo3',
value: 'diablo-3',
id: 'blt2031aef34200656d',
},
diablo4: {
key: 'diablo4',
value: 'diablo-4',
id: 'blt795c314400d7ded9',
},
'diablo-immortal': {
key: 'diablo-immortal',
value: 'diablo-immortal',
id: 'blt525c436e4a1b0a97',
},
hearthstone: {
key: 'hearthstone',
value: 'hearthstone',
id: 'blt5cfc6affa3ca0638',
},
'heroes-of-the-storm': {
key: 'heroes-of-the-storm',
value: 'heroes-of-the-storm',
id: 'blt2e50e1521bb84dc6',
},
overwatch: {
key: 'overwatch',
value: 'overwatch',
id: 'blt376fb94931906b6f',
},
starcraft: {
key: 'starcraft',
value: 'starcraft',
id: 'blt81d46fcb05ab8811',
},
starcraft2: {
key: 'starcraft2',
value: 'starcraft-2',
id: 'bltede2389c0a8885aa',
},
'world-of-warcraft': {
key: 'world-of-warcraft',
value: 'world-of-warcraft',
id: 'blt2caca37e42f19839',
},
warcraft3: {
key: 'warcraft3',
value: 'warcraft-3',
id: 'blt24859ba8086fb294',
},
'warcraft-rumble': {
key: 'warcraft-rumble',
value: 'warcraft-rumble',
id: 'blte27d02816a8ff3e1',
},
battlenet: {
key: 'battlenet',
value: 'battle-net',
id: 'blt90855744d00cd378',
},
blizzcon: {
key: 'blizzcon',
value: 'blizzcon',
id: 'bltec70ad0ea4fd6d1d',
},
blizzard: {
key: 'blizzard',
value: 'blizzard',
id: 'blt500c1f8b5470bfdb',
},
};

function getSearchParams(category = 'all') {
return category === 'all'
? Object.values(GAME_MAP)
.map((item) => `feedCxpProductIds[]=${item.id}`)
.join('&')
: `feedCxpProductIds[]=${GAME_MAP[category].id}`;
}

async function handler(ctx) {
const category = ctx.req.param('category') || '';
const category = GAME_MAP[ctx.req.param('category')]?.key || 'all';
const language = ctx.req.param('language') || 'en-us';
const rootUrl = `https://news.blizzard.com/${language}`;
const currentUrl = category === 'all' ? rootUrl : `${rootUrl}/?filter=${GAME_MAP[category].value}`;
const apiUrl = `${rootUrl}/api/news/blizzard`;
let rssTitle = '';

const rootUrl = 'https://news.blizzard.com';
const currentUrl = `${rootUrl}/${language}/${category}`;
const apiUrl = `${rootUrl}/${language}/blog/list`;
const response = await got(apiUrl, {
searchParams: {
community: category === '' ? 'all' : category,
const {
data: {
feed: { contentItems: response },
},
});

const $ = load(response.data.html, null, false);
} = await got(`${apiUrl}?${getSearchParams(category)}`);

const list = $('.FeaturedArticle-text > a, .ArticleListItem > article > a')
.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 30)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: `${rootUrl}${item.attr('href')}`,
};
});
const list = response.map((item) => {
const content = item.properties;
rssTitle = category === 'all' ? 'All News' : content.cxpProduct.title; // 这个是用来填充 RSS 订阅源频道级别 title,没别的地方能拿到了(而且会根据语言切换)
return {
title: content.title,
link: content.newsUrl,
author: content.author,
category: content.category,
guid: content.newsId,
description: content.summary,
pubDate: content.lastUpdated,
};
});

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link);
const content = load(detailResponse.data);

item.author = content('.ArticleDetail-bylineAuthor').text();
item.description = content('.ArticleDetail-headingImageBlock').html() + content('.ArticleDetail-content').html();
item.pubDate = content('.ArticleDetail-subHeadingLeft time').attr('timestamp');

return item;
try {
const { data: response } = await got(item.link);
const $ = load(response);
item.description = $('.Content').html();
return item;
} catch {
return item;
}
})
)
);

return {
title: $('title').text(),
title: rssTitle,
link: currentUrl,
item: items,
};
Expand Down

0 comments on commit dc32d68

Please sign in to comment.