diff --git a/lib/v2/huxiu/article.js b/lib/v2/huxiu/article.js index 4f4333273..757aeaa05 100644 --- a/lib/v2/huxiu/article.js +++ b/lib/v2/huxiu/article.js @@ -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); diff --git a/lib/v2/huxiu/author.js b/lib/v2/huxiu/author.js index 1bcda66e5..03a7cbd76 100644 --- a/lib/v2/huxiu/author.js +++ b/lib/v2/huxiu/author.js @@ -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, }; }; diff --git a/lib/v2/huxiu/briefColumn.js b/lib/v2/huxiu/briefColumn.js index 44a6261f9..eeca7b6a2 100644 --- a/lib/v2/huxiu/briefColumn.js +++ b/lib/v2/huxiu/briefColumn.js @@ -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, }; }; diff --git a/lib/v2/huxiu/collection.js b/lib/v2/huxiu/collection.js index 11090a9d0..ade81c626 100644 --- a/lib/v2/huxiu/collection.js +++ b/lib/v2/huxiu/collection.js @@ -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, }; }; diff --git a/lib/v2/huxiu/moment.js b/lib/v2/huxiu/moment.js index 63403aa27..fe8b9d3e0 100644 --- a/lib/v2/huxiu/moment.js +++ b/lib/v2/huxiu/moment.js @@ -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({ diff --git a/lib/v2/huxiu/search.js b/lib/v2/huxiu/search.js index 1244345eb..a379921bf 100644 --- a/lib/v2/huxiu/search.js +++ b/lib/v2/huxiu/search.js @@ -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); diff --git a/lib/v2/huxiu/tag.js b/lib/v2/huxiu/tag.js index 333c6c811..a1d1909a2 100644 --- a/lib/v2/huxiu/tag.js +++ b/lib/v2/huxiu/tag.js @@ -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); diff --git a/lib/v2/huxiu/templates/brief.art b/lib/v2/huxiu/templates/brief.art new file mode 100644 index 000000000..0babfafd6 --- /dev/null +++ b/lib/v2/huxiu/templates/brief.art @@ -0,0 +1,22 @@ +{{ if brief.preface }}{{@ brief.preface }}
{{ /if }} + +{{ if brief.audio_info }} +
+
{{ brief.audio_title }}
+ +
+
+{{ /if }} + +{{ if brief.part_list }}{{ each brief.part_list part }} + {{ if part.title }}

{{ part.title }}

{{ /if }} + {{ if part.content_info }}{{ each part.content_info content }} + {{ if content.type === 'text' }}{{@ content.text }}{{ /if }} + {{ if content.type === 'video' }} + + {{ /if }} + {{ if content.type === 'image'}}{{ /if }} + {{ /each }}
{{ /if }} +{{ /each }}{{ /if }} + +{{ if brief.peroration }}{{ brief.peroration }}{{ /if }} diff --git a/lib/v2/huxiu/templates/img.art b/lib/v2/huxiu/templates/img.art new file mode 100644 index 000000000..d7b1dd45a --- /dev/null +++ b/lib/v2/huxiu/templates/img.art @@ -0,0 +1,3 @@ +{{ if img }} + +{{ /if }} diff --git a/lib/v2/huxiu/templates/video.art b/lib/v2/huxiu/templates/video.art new file mode 100644 index 000000000..338b13108 --- /dev/null +++ b/lib/v2/huxiu/templates/video.art @@ -0,0 +1,7 @@ +{{ if video_info }} + +{{ /if }} diff --git a/lib/v2/huxiu/utils.js b/lib/v2/huxiu/utils.js index 378159a19..3774a8ec7 100644 --- a/lib/v2/huxiu/utils.js +++ b/lib/v2/huxiu/utils.js @@ -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 += ``; - // } - 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, };