fix(route): huxiu article (#11631)
* fix(route): huxiu article * fix: huxiu * refactor: api endpoints
This commit is contained in:
+14
-11
@@ -1,23 +1,26 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const utils = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const link = 'https://www.huxiu.com/article';
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
const link = `${utils.baseUrl}/article/`;
|
||||
const { data } = await got.post(`${utils.articleApi}/web/article/articleList`, {
|
||||
headers: {
|
||||
Referer: link,
|
||||
},
|
||||
form: {
|
||||
platform: 'www',
|
||||
pagesize: ctx.query.limit ? parseInt(ctx.query.limit) : 22,
|
||||
},
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
const list = $('.article-item-wrap > a')
|
||||
.slice(0, 10)
|
||||
.get()
|
||||
.map((e) => $(e).attr('href'));
|
||||
const list = data.data.dataList.map((item) => ({
|
||||
title: item.title,
|
||||
link: `${utils.baseUrl}/article/${item.aid}.html`,
|
||||
description: item.summary,
|
||||
pubDate: parseDate(item.dateline, 'X'),
|
||||
author: item.user_info.username,
|
||||
}));
|
||||
|
||||
const items = await utils.ProcessFeed(list, ctx.cache);
|
||||
|
||||
|
||||
+11
-20
@@ -1,28 +1,14 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const utils = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const link = `https://www.huxiu.com/member/${id}/article.html`;
|
||||
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
headers: {
|
||||
Referer: link,
|
||||
},
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
const author = $('.author-name').text().trim();
|
||||
const link = `${utils.baseUrl}/member/${id}/article.html`;
|
||||
|
||||
const articleList = await got({
|
||||
method: 'post',
|
||||
url: 'https://account-api.huxiu.com/web/article/articleList',
|
||||
headers: {
|
||||
Referer: 'https://www.huxiu.com/',
|
||||
},
|
||||
url: `${utils.accountApi}/web/article/articleList`,
|
||||
form: {
|
||||
platform: 'www',
|
||||
uid: id,
|
||||
@@ -31,15 +17,20 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
}).json();
|
||||
|
||||
const list = articleList.data.datalist.slice(0, 10).map((item) => item.url);
|
||||
const list = articleList.data.datalist.map((item) => ({
|
||||
title: item.title,
|
||||
link: `${utils.baseUrl}/article/${item.aid}.html`,
|
||||
description: item.summary,
|
||||
pubDate: parseDate(item.time),
|
||||
}));
|
||||
|
||||
const items = await utils.ProcessFeed(list, ctx.cache);
|
||||
|
||||
const authorInfo = `虎嗅网 - ${author}`;
|
||||
const authorInfo = `虎嗅网 - ${items[0].author}`;
|
||||
|
||||
ctx.state.data = {
|
||||
title: authorInfo,
|
||||
link,
|
||||
description: authorInfo,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
+26
-36
@@ -1,51 +1,41 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate, parseRelativeDate } = require('@/utils/parse-date');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const { baseUrl, briefApi, ProcessFeed } = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const link = `https://api-brief.huxiu.com/briefColumn/getContentListByCategoryId`;
|
||||
const response = await got
|
||||
const link = `${briefApi}/briefColumn/getContentListByCategoryId`;
|
||||
const { data: response } = await got
|
||||
.post(link, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
form: {
|
||||
platform: 'www',
|
||||
brief_column_id: id,
|
||||
},
|
||||
body: `platform=www&brief_column_id=${id}&last_id=`,
|
||||
})
|
||||
.json();
|
||||
const {
|
||||
data: { data: briefDetail },
|
||||
} = await got.post(`${briefApi}/briefColumn/detail`, {
|
||||
form: {
|
||||
platform: 'www',
|
||||
brief_column_id: id,
|
||||
},
|
||||
});
|
||||
|
||||
const list = response.data.datalist;
|
||||
const list = response.datalist.map((item) => ({
|
||||
title: item.title,
|
||||
link: `${baseUrl}/brief/${item.brief_id}`,
|
||||
description: item.preface,
|
||||
pubDate: parseDate(item.publish_time, 'X'),
|
||||
}));
|
||||
|
||||
const items = await Promise.all(
|
||||
list.map((item) => {
|
||||
const link = `https://www.huxiu.com/brief/${item.brief_id}`;
|
||||
|
||||
return ctx.cache.tryGet(link, async () => {
|
||||
const detailResponse = await got(link);
|
||||
const $ = cheerio.load(detailResponse.data);
|
||||
|
||||
const description = $('#js-part').html();
|
||||
|
||||
return {
|
||||
title: item.title,
|
||||
pubDate: item.format_publish_time.includes('前') ? parseRelativeDate(item.format_publish_time) : parseDate(item.format_publish_time),
|
||||
description,
|
||||
link,
|
||||
};
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const title = async () => {
|
||||
const res = await got(`https://www.huxiu.com/briefColumn/${id}.html`);
|
||||
const $ = cheerio.load(res.data);
|
||||
|
||||
return $('#top > div:nth-child(1) > div > div.brief-content__name').text();
|
||||
};
|
||||
const items = await ProcessFeed(list, ctx.cache);
|
||||
|
||||
ctx.state.data = {
|
||||
title: `虎嗅 - ${await title()}`,
|
||||
link: `https://www.huxiu.com/briefColumn/${id}.html`,
|
||||
title: `虎嗅 - ${briefDetail.name}`,
|
||||
description: briefDetail.summary,
|
||||
image: briefDetail.head_img,
|
||||
link: `${baseUrl}/briefColumn/${id}.html`,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
+20
-15
@@ -1,34 +1,39 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const utils = require('./utils');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const link = `https://www.huxiu.com/collection/${id}.html`;
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
headers: {
|
||||
Referer: link,
|
||||
const link = `${utils.baseUrl}/collection/${id}.html`;
|
||||
const { data: response } = await got(link, {
|
||||
https: {
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
});
|
||||
|
||||
const data = response.data;
|
||||
const $ = cheerio.load(data);
|
||||
const $ = cheerio.load(response);
|
||||
|
||||
const list = $('.collection-lirt-item > a')
|
||||
.slice(0, 5)
|
||||
.get()
|
||||
.map((e) => $(e).attr('href'));
|
||||
const initialState = utils.parseInitialState($);
|
||||
|
||||
const { collectionDetail } = initialState.collectionDetail;
|
||||
const list = collectionDetail.article_list.datalist.map((e) => ({
|
||||
title: e.title,
|
||||
link: `${utils.baseUrl}/article/${e.aid}.html`,
|
||||
description: e.summary,
|
||||
pubDate: parseDate(e.dateline, 'X'),
|
||||
author: e.user_info.username,
|
||||
}));
|
||||
|
||||
const items = await utils.ProcessFeed(list, ctx.cache);
|
||||
|
||||
const info = `虎嗅 - ${$('h1').text()}`;
|
||||
const description = $('p').text();
|
||||
const info = `虎嗅 - ${collectionDetail.name}`;
|
||||
|
||||
ctx.state.data = {
|
||||
title: info,
|
||||
description: collectionDetail.summary,
|
||||
image: collectionDetail.icon,
|
||||
link,
|
||||
description,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -2,11 +2,10 @@ const got = require('@/utils/got');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const { art } = require('@/utils/render');
|
||||
const path = require('path');
|
||||
const { baseUrl: rootUrl, momentApi: apiRootUrl } = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const rootUrl = 'https://www.huxiu.com';
|
||||
const currentUrl = `${rootUrl}/moment`;
|
||||
const apiRootUrl = 'https://moment-api.huxiu.com';
|
||||
const apiUrl = `${apiRootUrl}/web-v2/moment/feed`;
|
||||
|
||||
const response = await got({
|
||||
|
||||
+19
-7
@@ -3,18 +3,30 @@ const utils = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { keyword } = ctx.params;
|
||||
const url = `https://search-api.huxiu.com/api/article?platform=www&s=${encodeURIComponent(keyword)}&sort=&page=1&pagesize=20`;
|
||||
const link = 'https://www.huxiu.com';
|
||||
const url = `${utils.searchApi}/api/article`;
|
||||
const link = utils.baseUrl;
|
||||
|
||||
const response = await got({
|
||||
method: 'post',
|
||||
url,
|
||||
const { data } = await got.post(url, {
|
||||
headers: {
|
||||
Referer: link,
|
||||
Referer: utils.baseUrl,
|
||||
},
|
||||
searchParams: {
|
||||
platform: 'www',
|
||||
s: keyword,
|
||||
sort: '',
|
||||
page: 1,
|
||||
pagesize: 20,
|
||||
appid: 'hx_search',
|
||||
...utils.generateSignature(),
|
||||
},
|
||||
});
|
||||
|
||||
const list = response.data.data.datalist.slice(0, 10).map((d) => `${link}/article/${d.aid}.html`);
|
||||
const list = data.data.datalist.map((d) => ({
|
||||
title: d.title,
|
||||
link: d.url.includes('article') ? `${d.url}.html` : d.url,
|
||||
description: d.content,
|
||||
author: d.author,
|
||||
}));
|
||||
|
||||
const items = await utils.ProcessFeed(list, ctx.cache);
|
||||
|
||||
|
||||
+17
-10
@@ -1,23 +1,30 @@
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const utils = require('./utils');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const timezone = require('@/utils/timezone');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const link = `https://www.huxiu.com/tags/${id}.html`;
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: link,
|
||||
headers: {
|
||||
Referer: link,
|
||||
const link = `${utils.baseUrl}/tags/${id}.html`;
|
||||
const { data } = await got(link, {
|
||||
https: {
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
});
|
||||
|
||||
const data = response.data;
|
||||
const $ = cheerio.load(data);
|
||||
const list = $('.related-article li a')
|
||||
.get()
|
||||
.map((e) => $(e).attr('href'));
|
||||
const list = $('.related-article li')
|
||||
.toArray()
|
||||
.map((e) => {
|
||||
e = $(e);
|
||||
const a = e.find('a');
|
||||
return {
|
||||
title: a.text(),
|
||||
link: `${utils.baseUrl}${a.attr('href')}`,
|
||||
pubDate: timezone(parseDate(e.find('.time').text()), 8),
|
||||
};
|
||||
});
|
||||
|
||||
const items = await utils.ProcessFeed(list, ctx.cache);
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
{{ if brief.preface }}{{@ brief.preface }}<br>{{ /if }}
|
||||
|
||||
{{ if brief.audio_info }}
|
||||
<figure>
|
||||
<figcaption>{{ brief.audio_title }}</figcaption>
|
||||
<audio controls src="{{ brief.audio_info.audio_path }}"></audio>
|
||||
</figure>
|
||||
<br>
|
||||
{{ /if }}
|
||||
|
||||
{{ if brief.part_list }}{{ each brief.part_list part }}
|
||||
{{ if part.title }}<h2>{{ part.title }}</h2>{{ /if }}
|
||||
{{ if part.content_info }}{{ each part.content_info content }}
|
||||
{{ if content.type === 'text' }}{{@ content.text }}{{ /if }}
|
||||
{{ if content.type === 'video' }}
|
||||
<video controls poster="{{ content.video.poster }}"><source src="{{ content.video.url }}" type="video/mp4"></video>
|
||||
{{ /if }}
|
||||
{{ if content.type === 'image'}}{{ /if }}
|
||||
{{ /each }}<br>{{ /if }}
|
||||
{{ /each }}{{ /if }}
|
||||
|
||||
{{ if brief.peroration }}{{ brief.peroration }}{{ /if }}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{ if img }}
|
||||
<img src="{{ img }}">
|
||||
{{ /if }}
|
||||
@@ -0,0 +1,7 @@
|
||||
{{ if video_info }}
|
||||
<video controls poster="{{ video_info.cover }}" width="{{ video_info.width }}" height="{{ video_info.height }}">
|
||||
{{ if video_info.fhd_link }}<source src="{{ video_info.fhd_link }}" type="video/mp4">{{ /if }}
|
||||
{{ if video_info.hd_link }}<source src="{{ video_info.hd_link }}" type="video/mp4">{{ /if }}
|
||||
{{ if video_info.sd_link }}<source src="{{ video_info.sd_link }}" type="video/mp4">{{ /if }}
|
||||
</video>
|
||||
{{ /if }}
|
||||
+139
-54
@@ -1,69 +1,154 @@
|
||||
const cheerio = require('cheerio');
|
||||
const date = require('@/utils/date');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const got = require('@/utils/got');
|
||||
const { art } = require('@/utils/render');
|
||||
const path = require('path');
|
||||
const CryptoJS = require('crypto-js');
|
||||
|
||||
const ProcessFeed = async (list, cache) => {
|
||||
const host = 'https://www.huxiu.com';
|
||||
const accountApi = 'https://account-api.huxiu.com';
|
||||
const articleApi = 'https://api-article.huxiu.com';
|
||||
const baseUrl = 'https://www.huxiu.com';
|
||||
const briefApi = 'https://api-brief.huxiu.com';
|
||||
const momentApi = 'https://moment-api.huxiu.com';
|
||||
const searchApi = 'https://search-api.huxiu.com';
|
||||
|
||||
const items = await Promise.all(
|
||||
list.map(async (e) => {
|
||||
const link = new URL(e, host).href;
|
||||
|
||||
const single = await cache.tryGet(link, async () => {
|
||||
const response = await got(link);
|
||||
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
// const __INITIAL_STATE__ = Function(
|
||||
// 'let window={};' +
|
||||
// $('*')
|
||||
// .html()
|
||||
// .match(/window\.__INITIAL_STATE__.*};/gm)[0] +
|
||||
// 'return window.__INITIAL_STATE__'
|
||||
// )();
|
||||
|
||||
$('.article__content')
|
||||
.find('.lazyImg')
|
||||
.each((i, e) => {
|
||||
$(e).attr('src', $(e).attr('_src'));
|
||||
});
|
||||
|
||||
$('.article__content')
|
||||
.find('.text-big-title')
|
||||
.each((i, e) => (e.tagName = 'h3'));
|
||||
|
||||
$('p').each(function () {
|
||||
if ($(this).find('img').length === 0 && $(this).text().match(/^\s*$/)) {
|
||||
$(this).remove();
|
||||
}
|
||||
const ProcessFeed = (list, cache) =>
|
||||
Promise.all(
|
||||
list.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
const response = await got(item.link, {
|
||||
https: {
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
});
|
||||
$('img.dialog_add_wxxy_qrcode_icon').remove();
|
||||
const $ = cheerio.load(response.data);
|
||||
const initialState = parseInitialState($);
|
||||
|
||||
let description = '';
|
||||
|
||||
// if ('video_info' in __INITIAL_STATE__.articleDetail.articleDetail) {
|
||||
// description += `<video height="100%" poster="${__INITIAL_STATE__.articleDetail.articleDetail.video_info.cover}" controls src="${__INITIAL_STATE__.articleDetail.articleDetail.video_info.hd_link}"></video>`;
|
||||
// }
|
||||
if ($('.top-img').html() !== null) {
|
||||
description += $('.top-img').html();
|
||||
if (item.link.startsWith(`${baseUrl}/article/`)) {
|
||||
fetchArticle(initialState, item);
|
||||
} else if (item.link.startsWith(`${baseUrl}/brief/`)) {
|
||||
fetchBrief(initialState, item);
|
||||
}
|
||||
|
||||
return {
|
||||
title: $('.article__title').text().trim(),
|
||||
description: description + $('.article__content').html(),
|
||||
pubDate: date($('.article__time').text(), 8),
|
||||
author: $('.author-info__username').text(),
|
||||
link,
|
||||
};
|
||||
});
|
||||
|
||||
return single;
|
||||
})
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return items;
|
||||
const fetchArticle = (initialState, item) => {
|
||||
const { articleDetail } = initialState.articleDetail;
|
||||
const topImg = art(path.join(__dirname, 'templates/img.art'), {
|
||||
img: articleDetail.pic_path.split('?')[0],
|
||||
});
|
||||
let video = '';
|
||||
if (articleDetail.video_info) {
|
||||
video = art(path.join(__dirname, 'templates/video.art'), {
|
||||
video_info: articleDetail.video_info,
|
||||
});
|
||||
}
|
||||
|
||||
const $ = cheerio.load(articleDetail.content, null, false);
|
||||
|
||||
$('.text-big-title').each((_, e) => (e.tagName = 'h3'));
|
||||
fixImg($);
|
||||
removeExtraLinebreak($);
|
||||
// $('img.dialog_add_wxxy_qrcode_icon').remove();
|
||||
|
||||
item.description = video + topImg + $.html();
|
||||
item.author = articleDetail.author;
|
||||
item.category = articleDetail.tags_info.map((tag) => tag.name);
|
||||
};
|
||||
|
||||
const fetchBrief = (initialState, item) => {
|
||||
const { brief_detail } = initialState.briefStoreModule;
|
||||
const { brief_column } = brief_detail;
|
||||
|
||||
const description = art(path.join(__dirname, 'templates/brief.art'), {
|
||||
brief: brief_detail.brief,
|
||||
});
|
||||
|
||||
const $ = cheerio.load(description, null, false);
|
||||
|
||||
$('button.black-dot').remove();
|
||||
fixImg($);
|
||||
removeExtraLinebreak($);
|
||||
|
||||
item.description = $.html();
|
||||
item.author = brief_detail.brief.publisher_list.map((item) => item.username).join(', ');
|
||||
item.pubDate = parseDate(brief_column.update_time, 'X');
|
||||
};
|
||||
|
||||
const parseInitialState = ($) =>
|
||||
JSON.parse(
|
||||
$('script')
|
||||
.text()
|
||||
.match(/window\.__INITIAL_STATE__=(\{.*?\});\(function\(\)/)[1]
|
||||
);
|
||||
|
||||
const fixImg = ($) => {
|
||||
$('img.lazyImg, img.js-preview').each((_, e) => {
|
||||
if (e.attribs._src) {
|
||||
e.attribs.src = e.attribs._src.split('?')[0];
|
||||
delete e.attribs._src;
|
||||
}
|
||||
if (e.attribs.src.includes('?')) {
|
||||
e.attribs.src = e.attribs.src.split('?')[0];
|
||||
}
|
||||
if (e.attribs['data-w']) {
|
||||
e.attribs.width = e.attribs['data-w'];
|
||||
delete e.attribs['data-w'];
|
||||
}
|
||||
if (e.attribs['data-h']) {
|
||||
e.attribs.height = e.attribs['data-h'];
|
||||
delete e.attribs['data-h'];
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const removeExtraLinebreak = ($) => {
|
||||
$('p').each((_, e) => {
|
||||
e = $(e);
|
||||
if (e.find('img').length === 0 && e.text().match(/^\s*$/)) {
|
||||
e.remove();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const generateNonce = () => {
|
||||
let nonce = '';
|
||||
const e = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
const t = 16;
|
||||
for (let i = 0; i < t; i++) {
|
||||
nonce += e.charAt(Math.floor(Math.random() * e.length));
|
||||
}
|
||||
return nonce;
|
||||
};
|
||||
|
||||
const generateSignature = () => {
|
||||
const timestamp = Math.round(new Date().getTime() / 1000).toString();
|
||||
// const appSecret = '4nHzGgGt7WX4zFTsTKocpxg4dzU6-wUi';
|
||||
const appSecret = 'hUzaVKtNfDE-6UiyaJdfsmjW-8dwoyVc'; // appid: 'hx_search'
|
||||
const nonce = generateNonce();
|
||||
const r = [appSecret, timestamp, nonce].sort();
|
||||
return {
|
||||
nonce,
|
||||
timestamp,
|
||||
signature: CryptoJS.SHA1(r[0] + r[1] + r[2]).toString(),
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
accountApi,
|
||||
articleApi,
|
||||
baseUrl,
|
||||
briefApi,
|
||||
momentApi,
|
||||
searchApi,
|
||||
ProcessFeed,
|
||||
fetchArticle,
|
||||
fetchBrief,
|
||||
parseInitialState,
|
||||
fixImg,
|
||||
removeExtraLinebreak,
|
||||
generateSignature,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user