fix(route): hkepc next page (#11993)
* fix(route): hkepc next page * fix: split data
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
const baseUrl = 'https://www.hkepc.com';
|
||||
|
||||
module.exports = {
|
||||
baseUrl,
|
||||
categoryMap: {
|
||||
price: {
|
||||
url: `${baseUrl}/news`,
|
||||
feedSuffix: ' - 腦場新聞',
|
||||
selector: '#sidebar > div:nth-child(1) > div.content > ul > li',
|
||||
},
|
||||
review: {
|
||||
url: `${baseUrl}/news`,
|
||||
feedSuffix: ' - 新品快遞',
|
||||
selector: '#sidebar > div:nth-child(2) > div.content > ul > li',
|
||||
},
|
||||
coverStory: {
|
||||
url: `${baseUrl}/news`,
|
||||
feedSuffix: ' - 專題報導',
|
||||
selector: '#sidebar > div:nth-child(3) > div.content > ul > li',
|
||||
},
|
||||
news: {
|
||||
url: `${baseUrl}/news`,
|
||||
feedSuffix: ' - 新聞中心',
|
||||
selector: '#sidebar > div:nth-child(4) > div.content > ul > li',
|
||||
},
|
||||
press: {
|
||||
url: `${baseUrl}/news`,
|
||||
feedSuffix: ' - 業界資訊',
|
||||
selector: '#sidebar > div:nth-child(5) > div.content > ul > li',
|
||||
},
|
||||
member: {
|
||||
url: `${baseUrl}/news`,
|
||||
feedSuffix: ' - 會員消息',
|
||||
selector: '#sidebar > div:nth-child(6) > div.content > ul > li',
|
||||
},
|
||||
digital: {
|
||||
url: baseUrl,
|
||||
feedSuffix: ' - 流動數碼',
|
||||
selector: '#contentR5 > div.left > div.article > div.heading',
|
||||
},
|
||||
entertainment: {
|
||||
url: baseUrl,
|
||||
feedSuffix: ' - 生活娛樂',
|
||||
selector: '#contentR5 > div.right > div.article > div.heading',
|
||||
},
|
||||
latest: {
|
||||
url: baseUrl,
|
||||
feedSuffix: ' - 最新消息',
|
||||
selector: 'div .item',
|
||||
},
|
||||
'': {
|
||||
url: baseUrl,
|
||||
feedSuffix: ' - 最新消息',
|
||||
selector: 'div .item',
|
||||
},
|
||||
ocLab: {
|
||||
url: `${baseUrl}/ocLab`,
|
||||
feedSuffix: ' - 超頻領域',
|
||||
selector: '.heading',
|
||||
},
|
||||
},
|
||||
};
|
||||
+55
-164
@@ -2,185 +2,84 @@ const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const timezone = require('@/utils/timezone');
|
||||
const path = require('path');
|
||||
const { art } = require('@/utils/render');
|
||||
const { baseUrl, categoryMap } = require('./data');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const category = ctx.params.category ?? '';
|
||||
const baseUrl = `https://www.hkepc.com`;
|
||||
|
||||
let response;
|
||||
if (category === 'price' || category === 'review' || category === 'coverStory' || category === 'news' || category === 'press' || category === 'member') {
|
||||
response = await got({
|
||||
url: `${baseUrl}/news`,
|
||||
headers: {
|
||||
Referer: baseUrl,
|
||||
},
|
||||
});
|
||||
} else if (category === 'digital' || category === 'entertainment' || category === 'latest' || category === '') {
|
||||
response = await got(baseUrl);
|
||||
} else {
|
||||
// category === ocLab
|
||||
response = await got({
|
||||
url: `${baseUrl}/${category}`,
|
||||
headers: {
|
||||
Referer: baseUrl,
|
||||
},
|
||||
});
|
||||
}
|
||||
const { data: response } = await got(categoryMap[category].url, {
|
||||
headers: {
|
||||
Referer: baseUrl,
|
||||
},
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
const $ = cheerio.load(response);
|
||||
|
||||
let feedTitle = '電腦領域 HKEPC';
|
||||
let items;
|
||||
switch (category) {
|
||||
case 'price':
|
||||
feedTitle += ' - 腦場新聞';
|
||||
items = $('#sidebar > div:nth-child(1) > div.content > ul > li')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'review':
|
||||
feedTitle += ' - 新品快遞';
|
||||
items = $('#sidebar > div:nth-child(2) > div.content > ul > li')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'coverStory':
|
||||
feedTitle += ' - 專題報導';
|
||||
items = $('#sidebar > div:nth-child(3) > div.content > ul > li')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'news':
|
||||
feedTitle += ' - 新聞中心';
|
||||
items = $('#sidebar > div:nth-child(4) > div.content > ul > li')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'press':
|
||||
feedTitle += ' - 業界資訊';
|
||||
items = $('#sidebar > div:nth-child(5) > div.content > ul > li')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'member':
|
||||
feedTitle += ' - 會員消息';
|
||||
items = $('#sidebar > div:nth-child(6) > div.content > ul > li')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'digital':
|
||||
feedTitle += ' - 流動數碼';
|
||||
items = $('#contentR5 > div.left > div.article > div.heading')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'entertainment':
|
||||
feedTitle += ' - 生活娛樂';
|
||||
items = $('#contentR5 > div.right > div.article > div.heading')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'latest':
|
||||
case '':
|
||||
feedTitle += ' - 最新消息';
|
||||
items = $('div .item')
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
case 'ocLab':
|
||||
feedTitle += ' - 超頻領域';
|
||||
items = $('.heading')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
const list = $(categoryMap[category].selector)
|
||||
.find('a')
|
||||
.toArray()
|
||||
.map((item) => ({
|
||||
title: $(item).text(),
|
||||
link: baseUrl + $(item).attr('href'),
|
||||
}));
|
||||
|
||||
items = await Promise.all(
|
||||
items.map((item) =>
|
||||
const items = await Promise.all(
|
||||
list.map((item) =>
|
||||
ctx.cache.tryGet(item.link, async () => {
|
||||
const detailResponse = await got({
|
||||
url: item.link,
|
||||
const { data: response } = await got(item.link, {
|
||||
headers: {
|
||||
Referer: baseUrl,
|
||||
},
|
||||
});
|
||||
|
||||
const content = cheerio.load(detailResponse.data);
|
||||
const $ = cheerio.load(response);
|
||||
const content = $('#view');
|
||||
const nextPages = $('#articleFooter .navigation a')
|
||||
.toArray()
|
||||
.map((a) => `${baseUrl}${a.attribs.href}`)
|
||||
.slice(1);
|
||||
|
||||
if (nextPages.length) {
|
||||
const pages = await Promise.all(
|
||||
nextPages.map(async (url) => {
|
||||
const { data: response } = await got(url, {
|
||||
headers: {
|
||||
referer: item.link,
|
||||
},
|
||||
});
|
||||
const $ = cheerio.load(response);
|
||||
return $('#view').html();
|
||||
})
|
||||
);
|
||||
content.append(pages);
|
||||
}
|
||||
|
||||
// remove unwanted elements
|
||||
content('#view > div.advertisement').remove();
|
||||
content('div#comments').remove();
|
||||
content('div#share_btn').remove();
|
||||
content.find('#view > div.advertisement').remove();
|
||||
content.find('div#comments').remove();
|
||||
content.find('div#share_btn').remove();
|
||||
content.find('#articleFooter').remove();
|
||||
|
||||
// Non-breaking space U+00A0, ` ` in html
|
||||
// Taken from /caixin/blog.js
|
||||
content('#view > p')
|
||||
.filter((_, e) => e.children[0].data === String.fromCharCode(160))
|
||||
content
|
||||
.find('#view > p')
|
||||
.filter((_, e) => e.children[0]?.data === String.fromCharCode(160))
|
||||
.remove();
|
||||
|
||||
// fix lazyload image
|
||||
content('#view > p > img').each((_, e) => {
|
||||
e = content(e);
|
||||
e.after(
|
||||
art(path.join(__dirname, 'templates/image.art'), {
|
||||
src: e.attr('rel') ?? e.attr('src'),
|
||||
alt: e.attr('alt'),
|
||||
})
|
||||
);
|
||||
e.remove();
|
||||
content.find('#view > p > img').each((_, e) => {
|
||||
if (e.attribs.rel) {
|
||||
e.attribs.src = e.attribs.rel;
|
||||
}
|
||||
});
|
||||
|
||||
item.author = content('.newsAuthor').text().trim() ?? content('#articleHead div.author').text().trim();
|
||||
item.category = content('div#relatedArticles div.tags a')
|
||||
item.author = $('.newsAuthor').text().trim() || $('#articleHead div.author').text().trim();
|
||||
item.category = $('div#relatedArticles div.tags a')
|
||||
.toArray()
|
||||
.map((e) => content(e).text().trim());
|
||||
item.description = art(path.join(__dirname, 'templates/description.art'), {
|
||||
desc: content('#view').html(),
|
||||
});
|
||||
item.pubDate = timezone(parseDate(content('.publishDate').text()), +8);
|
||||
.map((e) => $(e).text().trim());
|
||||
item.description = content.html();
|
||||
item.pubDate = timezone(parseDate($('.publishDate').text()), +8);
|
||||
item.guid = item.link.substring(0, item.link.lastIndexOf('/'));
|
||||
|
||||
return item;
|
||||
@@ -189,15 +88,7 @@ module.exports = async (ctx) => {
|
||||
);
|
||||
|
||||
ctx.state.data = {
|
||||
title: feedTitle,
|
||||
link: `https://www.hkepc.com/${category}`,
|
||||
description: '電腦領域 HKEPC Hardware - 全港 No.1 PC網站',
|
||||
language: 'zh-hk',
|
||||
item: items,
|
||||
};
|
||||
|
||||
ctx.state.json = {
|
||||
title: feedTitle,
|
||||
title: `電腦領域 HKEPC${categoryMap[category].feedSuffix}`,
|
||||
link: `https://www.hkepc.com/${category}`,
|
||||
description: '電腦領域 HKEPC Hardware - 全港 No.1 PC網站',
|
||||
language: 'zh-hk',
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
{{@ desc }}
|
||||
@@ -1 +0,0 @@
|
||||
<img src="{{ src }}" alt="{{ alt }}">
|
||||
Reference in New Issue
Block a user