feat: 新增 wikihow/正版中国 (#2679)

* feat: 新增 wikihow/正版中国/小众软件

* 移除 小众软件
This commit is contained in:
sanmmmm
2019-07-24 14:23:36 +08:00
committed by DIYgod
parent 982268b42b
commit 901bcbd3f9
7 changed files with 243 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
const cheerio = require('cheerio');
const getDeadlineStr = ($, countDownNodeSelector) => {
const countDownNode = $(countDownNodeSelector);
let deadlineStr = '';
if (countDownNode.text().includes('活动已经结束')) {
return '截至日期: 活动已经结束';
}
const countdownCodeStr = countDownNode.children('script').html() || '';
const arr = /ShowCountDown\("\d*"\s*,\s*([\d,]+)\s*\)/.exec(countdownCodeStr.trim());
if (arr) {
const dateStr = arr[1].replace(/,/g, (...args) => {
const index = args[args.length - 2];
return index === 10 ? ' ' : index < 10 ? '-' : ':';
});
deadlineStr = `截至日期: ${new Date(dateStr).toUTCString()}`;
}
return deadlineStr;
};
const parseListItem = ($, listSelector) =>
$(`${listSelector} .content-box`)
.map((_, ele) => {
const $item = cheerio.load(ele);
const infoNode = $item('.posts-default-title > h2 > a');
const title = infoNode.attr('title');
const link = infoNode.attr('href');
const pubDateStr = $item('.posts-default-info .icon-time').text();
const pubDate = new Date(pubDateStr).toUTCString();
const thumbnail = $item('.posts-default-img img').attr('data-original');
const deadlineStr = getDeadlineStr($item, '.countDownCont');
const digest = $item('.posts-default-content > .posts-text')
.text()
.trim();
return {
title,
link,
pubDate,
description: [title, deadlineStr, digest, `<img src="${thumbnail}"/>`].filter((str) => !!str).join('<br/>'),
};
})
.get();
module.exports = {
getDeadlineStr,
parseListItem,
};