From ec80154f2ff0bda74cda0f3e72793cb4a9423eb8 Mon Sep 17 00:00:00 2001 From: sgqy Date: Sat, 3 Nov 2018 10:57:31 +0800 Subject: [PATCH] fix [solidot] to show content image (#1053) --- routes/solidot/_article.js | 46 +++++++++++++++++++++++++++++ routes/solidot/main.js | 59 ++++++-------------------------------- 2 files changed, 54 insertions(+), 51 deletions(-) create mode 100644 routes/solidot/_article.js diff --git a/routes/solidot/_article.js b/routes/solidot/_article.js new file mode 100644 index 000000000..9759063cf --- /dev/null +++ b/routes/solidot/_article.js @@ -0,0 +1,46 @@ +const axios = require('../../utils/axios'); // get web content +const cheerio = require('cheerio'); // html parser + +const domain = 'https://www.solidot.org'; + +module.exports = async function get_article(url) { + if (/^\/.*$/.test(url)) { + url = domain + url; + } + const response = await axios({ + method: 'get', + url: url, + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0', + }); + const data = response.data; + const $ = cheerio.load(data); + + const date_raw = $('div.talk_time') + .clone() + .children() + .remove() + .end() + .text(); + const date_str_zh = date_raw.replace(/^[^`]*发表于(.*分)[^`]*$/g, '$1'); // use [^`] to match \n + const date_str = date_str_zh + .replace(/[年月]/g, '-') + .replace(/时/g, ':') + .replace(/[日分]/g, ''); + let date = new Date(date_str); + date.setHours(date.getHours() - 8); + date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())); + + const item = { + title: $('div.bg_htit > h2').text(), + pubDate: date.toUTCString(), + author: $('div.talk_time > b') + .text() + .replace(/^来自(.*)部门$/g, '$1'), + link: url, + description: $('div.p_mainnew') + .html() + .replace(/href="\//g, 'href="' + domain + '/'), + category: $('div.icon_float > a').attr('title'), + }; + return item; +}; diff --git a/routes/solidot/main.js b/routes/solidot/main.js index 298c89035..039f10818 100644 --- a/routes/solidot/main.js +++ b/routes/solidot/main.js @@ -5,6 +5,7 @@ const axios = require('../../utils/axios'); // get web content const cheerio = require('cheerio'); // html parser +const get_article = require('./_article'); const base_url = 'https://$type$.solidot.org'; module.exports = async (ctx) => { @@ -19,60 +20,16 @@ module.exports = async (ctx) => { const data = response.data; // content is html format const $ = cheerio.load(data); - // get content - const a = $('div.block_m'); - - const msg_list = []; + // get urls + const a = $('div.block_m').find('div.bg_htit > h2 > a'); + const urls = []; for (let i = 0; i < a.length; ++i) { - const title = $(a[i]) - .find('div.bg_htit > h2 > a') - .text(); - const link = - list_url + - $(a[i]) - .find('div.bg_htit > h2 > a') - .attr('href'); - const content = $(a[i]) - .find('div.p_mainnew') - .html() - .replace(/href="\//g, 'href="' + list_url); - const author = $(a[i]) - .find('div.talk_time > b') - .text() - .replace(/^来自/, ''); - const category = $(a[i]) - .find('div.icon_float > a') - .attr('title'); - const cat_img = $(a[i]) - .find('div.icon_float > a') - .html(); - const date_raw = $(a[i]) - .find('div.talk_time') - .clone() - .children() - .remove() - .end() - .text(); - const date_str_zh = date_raw.replace(/^[^`]*发表于(.*分)[^`]*$/g, '$1'); // use [^`] to match \n - const date_str = date_str_zh - .replace(/[年月]/g, '-') - .replace(/时/g, ':') - .replace(/[日分]/g, ''); - let date = new Date(date_str); - date.setHours(date.getHours() - 8); - date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())); - const item = { - title: title, - author: author, - pubDate: date.toUTCString(), - category: category, // not supported: 2018-10-29 - link: link, - description: content + '

' + cat_img, - }; - - msg_list.push(item); + urls.push($(a[i]).attr('href')); } + // get articles + const msg_list = await Promise.all(urls.map((u) => get_article(u))); + // feed the data ctx.state.data = { title: '奇客的资讯,重要的东西',