Skip to content

Commit

Permalink
fix(route): hkepc next page (DIYgod#11993)
Browse files Browse the repository at this point in the history
* fix(route): hkepc next page

* fix: split data
  • Loading branch information
TonyRL authored Mar 1, 2023
1 parent edbb607 commit eb5e1f3
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 166 deletions.
62 changes: 62 additions & 0 deletions lib/v2/hkepc/data.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
};
219 changes: 55 additions & 164 deletions lib/v2/hkepc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down
1 change: 0 additions & 1 deletion lib/v2/hkepc/templates/description.art

This file was deleted.

1 change: 0 additions & 1 deletion lib/v2/hkepc/templates/image.art

This file was deleted.

0 comments on commit eb5e1f3

Please sign in to comment.