|
| 1 | +import { Route } from '@/types'; |
| 2 | +import { load } from 'cheerio'; |
| 3 | +import cache from '@/utils/cache'; |
| 4 | +import ofetch from '@/utils/ofetch'; |
| 5 | +import { parseDate } from '@/utils/parse-date'; |
| 6 | + |
| 7 | +export const route: Route = { |
| 8 | + path: '/news-cn/:category?', |
| 9 | + categories: ['game'], |
| 10 | + example: '/blizzard/news-cn/ow', |
| 11 | + parameters: { category: '游戏类别, 默认为 ow' }, |
| 12 | + features: { |
| 13 | + requireConfig: false, |
| 14 | + requirePuppeteer: false, |
| 15 | + antiCrawler: false, |
| 16 | + supportBT: false, |
| 17 | + supportPodcast: false, |
| 18 | + supportScihub: false, |
| 19 | + }, |
| 20 | + radar: [ |
| 21 | + { |
| 22 | + source: ['ow.blizzard.cn', 'wow.blizzard.cn', 'hs.blizzard.cn'], |
| 23 | + target: '/news-cn/', |
| 24 | + }, |
| 25 | + ], |
| 26 | + name: '暴雪游戏国服新闻', |
| 27 | + maintainers: ['zhangpeng2k'], |
| 28 | + description: ` |
| 29 | +| 守望先锋 | 炉石传说 | 魔兽世界 | |
| 30 | +|----------|----------|---------| |
| 31 | +| ow | hs | wow | |
| 32 | +`, |
| 33 | + handler, |
| 34 | +}; |
| 35 | + |
| 36 | +const categoryNames = { |
| 37 | + ow: '守望先锋', |
| 38 | + hs: '炉石传说', |
| 39 | + wow: '魔兽世界', |
| 40 | +}; |
| 41 | + |
| 42 | +/* 列表解析逻辑 */ |
| 43 | +const parsers = { |
| 44 | + ow: ($) => |
| 45 | + $('.list-data-container .list-item-container').toArray().map((item) => { |
| 46 | + item = $(item); |
| 47 | + return { |
| 48 | + title: item.find('.content-title').text(), |
| 49 | + link: item.find('.fill-link').attr('href'), |
| 50 | + description: item.find('.content-intro').text(), |
| 51 | + pubDate: parseDate(item.find('.content-date').text()), |
| 52 | + image: item.find('.item-pic').attr('src'), |
| 53 | + }; |
| 54 | + }), |
| 55 | + hs: ($) => |
| 56 | + $('.article-container>a').toArray().map((item) => { |
| 57 | + item = $(item); |
| 58 | + return { |
| 59 | + title: item.find('.title').text(), |
| 60 | + link: item.attr('href'), |
| 61 | + description: item.find('.desc').text(), |
| 62 | + pubDate: parseDate(item.find('.date').attr('data-time')), |
| 63 | + image: item.find('.article-img img').attr('src'), |
| 64 | + }; |
| 65 | + }), |
| 66 | + wow: ($) => $('.Pane-list>a').toArray().map((item) => { |
| 67 | + item = $(item); |
| 68 | + return { |
| 69 | + title: item.find('.list-title').text(), |
| 70 | + link: item.attr('href'), |
| 71 | + description: item.find('.list-desc').text(), |
| 72 | + pubDate: parseDate(item.find('.list-time').attr('data-time')), |
| 73 | + image: item.find('.img-box img').attr('src'), |
| 74 | + }; |
| 75 | + }), |
| 76 | +}; |
| 77 | + |
| 78 | +// 详情页解析逻辑 |
| 79 | +const detailParsers = { |
| 80 | + ow: ($) => $('.deatil-content').first().html(), |
| 81 | + hs: ($) => $('.article').first().html(), |
| 82 | + wow: ($) => $('.detail').first().html(), |
| 83 | +}; |
| 84 | + |
| 85 | +function getList(category, $) { |
| 86 | + return parsers[category] ? parsers[category]($) : []; |
| 87 | +} |
| 88 | + |
| 89 | +async function fetchDetail(item, category) { |
| 90 | + return await cache.tryGet(item.link, async () => { |
| 91 | + const response = await ofetch(item.link); |
| 92 | + const $ = load(response); |
| 93 | + |
| 94 | + const parseDetail = detailParsers[category]; |
| 95 | + item.description = parseDetail($); |
| 96 | + return item; |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +async function handler(ctx) { |
| 101 | + const category = ctx.req.param('category') || 'ow'; |
| 102 | + if (!categoryNames[category]) { |
| 103 | + throw new Error('Invalid category'); |
| 104 | + } |
| 105 | + |
| 106 | + const rootUrl = `https://${category}.blizzard.cn/news`; |
| 107 | + |
| 108 | + const response = await ofetch(rootUrl); |
| 109 | + const $ = load(response); |
| 110 | + |
| 111 | + const list = getList(category, $); |
| 112 | + if (!list.length) { |
| 113 | + throw new Error('No news found'); |
| 114 | + } |
| 115 | + |
| 116 | + const items = await Promise.all( |
| 117 | + list.map((item) => fetchDetail(item, category)) |
| 118 | + ); |
| 119 | + |
| 120 | + return { |
| 121 | + title: `${categoryNames[category]}新闻`, |
| 122 | + link: rootUrl, |
| 123 | + item: items, |
| 124 | + }; |
| 125 | +} |
0 commit comments