feat: Add a New Section for Science Journal 📚 (#4068)

This commit is contained in:
Chang Y
2020-02-25 00:47:10 +08:00
committed by GitHub
parent 2f4d83beab
commit a2d8f93eea
20 changed files with 1187 additions and 146 deletions
+22 -5
View File
@@ -1962,6 +1962,11 @@ router.get('/factcheck', require('./routes/tencent/factcheck'));
router.get('/x-mol/news/:tag?', require('./routes/x-mol/news.js'));
router.get('/x-mol/paper/:type/:magazine', require('./routes/x-mol/paper'));
// 知识分子
router.get('/zhishifenzi/news/:type?', require('./routes/zhishifenzi/news'));
router.get('/zhishifenzi/depth', require('./routes/zhishifenzi/depth'));
router.get('/zhishifenzi/innovation/:type?', require('./routes/zhishifenzi/innovation'));
// 電撃Online
router.get('/dengekionline/:type?', require('./routes/dengekionline/new'));
@@ -1982,18 +1987,30 @@ router.get('/hatena/anonymous_diary/archive', require('./routes/hatena/anonymous
router.get('/kaggle/discussion/:forumId/:sort?', require('./routes/kaggle/discussion'));
router.get('/kaggle/competitions/:category?', require('./routes/kaggle/competitions'));
// eLife
// eLife [Sci Journal]
router.get('/elife/latest', require('./routes/elife/latest'));
router.get('/elife/:tid', require('./routes/elife/subject'));
// PNAS
// PNAS [Sci Journal]
router.get('/pnas/latest', require('./routes/pnas/latest'));
router.get('/pnas/:tid', require('./routes/pnas/topic'));
// nature
// nature [Sci Journal]
router.get('/nature/nature/research', require('./routes/nature/nature/research'));
router.get('/nature/nature/news', require('./routes/nature/nature/news'));
router.get('/nature/nature/highlight', require('./routes/nature/nature/highlight'));
// nature 子刊 [Sci Journal]
router.get('/nature/nmeth/research', require('./routes/nature/nmeth/research'));
router.get('/nature/ng/research', require('./routes/nature/ng/research'));
router.get('/nature/nbt/research', require('./routes/nature/nbt/research'));
router.get('/nature/neuro/research', require('./routes/nature/neuro/research'));
router.get('/nature/natmachintell/research', require('./routes/nature/natmachintell/research'));
router.get('/nature/neuroscience/research', require('./routes/nature/neuroscience/research'));
router.get('/nature/research', require('./routes/nature/research'));
// science [Sci Journal]
router.get('/sciencemag/science/current', require('./routes/sciencemag/science/current'));
router.get('/sciencemag/science/early', require('./routes/sciencemag/science/early'));
// science 子刊 [Sci Journal]
router.get('/sciencemag/advances/current', require('./routes/sciencemag/advances/current'));
// dlsite
router.get('/dlsite/new/:type', require('./routes/dlsite/new'));
+60
View File
@@ -0,0 +1,60 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://www.nature.com`;
const url = `${base}/nature/articles?type=research-highlight`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.border-bottom-1.pb20').get();
const out = await Promise.all(
list.slice(0, 2).map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
author =
$('.js-list-authors-3 li')
.slice(0, 1)
.text() + ' et al.';
} else {
author = $('.js-list-authors-3 li').text();
}
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
let figure = capture('figure .figure--bleed').html();
if (figure === null) {
figure = '';
}
let contents = capture('.article-item--open .article-item__body').html();
if (contents === null) {
contents = '';
}
const single = {
title,
author: author,
description: figure + contents,
link: address,
guid: address,
pubDate: new Date(time).toUTCString(),
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Nature | Research Highlight`,
description: `Nature, a nature research journal`,
link: url,
item: out,
};
};
+54
View File
@@ -0,0 +1,54 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://www.nature.com`;
const url = `${base}/nature/articles?type=news`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.border-bottom-1.pb20').get();
const out = await Promise.all(
list.slice(0, 2).map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
author =
$('.js-list-authors-3 li')
.slice(0, 1)
.text() + ' et al.';
} else {
author = $('.js-list-authors-3 li').text();
}
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
const contents = capture('.content .article__body').html();
const single = {
title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: new Date(time).toUTCString(),
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Nature | Latest News`,
description: `Nature, a nature research journal`,
link: url,
item: out,
};
};
@@ -2,7 +2,8 @@ const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const url = `https://www.nature.com/nature/research`;
const base = `https://www.nature.com`;
const url = `${base}/nature/research`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
@@ -13,8 +14,8 @@ module.exports = async (ctx) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `https://www.nature.com${partial}`;
const description = $('.hide-overflow.inline').text();
const address = `${base}${partial}`;
const brief = $('.hide-overflow.inline').text();
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
@@ -31,18 +32,27 @@ module.exports = async (ctx) => {
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
const abs = capture('div#Abs1-content.c-article-section__content').html();
let contents;
if (description !== '' && abs !== null) {
contents = `${description}<p><div><center style="font-size: 16pt"><b>Abstraction</b></center><p>${abs}</p></div>`;
} else {
if (abs !== null) {
contents = `<center><b>Abstraction</b></center>` + abs;
}
if (description !== '') {
contents = description;
}
const abs = capture('div#Abs1-content.c-article-section__content > p').html();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div>
<h2 align="left">Brief</h2>
<p>${brief}</p>
</div>
`;
}
let absContents = '';
if (abs !== null) {
absContents = `
<div>
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
const contents = briefContents + absContents;
const single = {
title,
@@ -58,6 +68,7 @@ module.exports = async (ctx) => {
);
ctx.state.data = {
title: `Nature | Latest Research`,
description: `Nature, a nature research journal`,
link: url,
item: out,
};
+75
View File
@@ -0,0 +1,75 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://www.nature.com`;
const url = `${base}/nbt/research`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.border-bottom-1.pb20').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
const brief = $('.hide-overflow.inline').text();
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
author =
$('.js-list-authors-3 li')
.slice(0, 1)
.text() + ' et al.';
} else {
author = $('.js-list-authors-3 li').text();
}
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
const abs = capture('div#Abs1-content.c-article-section__content > p').html();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div>
<h2 align="left">Brief</h2>
<p>${brief}</p>
</div>
`;
}
let absContents = '';
if (abs !== null) {
absContents = `
<div>
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
const contents = briefContents + absContents;
const single = {
title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: new Date(time).toUTCString(),
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Nature Biotechnology | Latest Research`,
description: `Nature Biotechnology, a nature research journal`,
link: url,
item: out,
};
};
@@ -2,19 +2,20 @@ const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const url = `https://www.nature.com/neuro/research`;
const base = `https://www.nature.com`;
const url = `${base}/neuro/research`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.border-bottom-1.pb20').get();
const out = await Promise.all(
list.map(async (item) => {
list.slice(0, 3).map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `https://www.nature.com${partial}`;
const description = $('.hide-overflow.inline').text();
const address = `${base}${partial}`;
const brief = $('.hide-overflow.inline').text();
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
@@ -31,18 +32,27 @@ module.exports = async (ctx) => {
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
const abs = capture('div#Abs1-content.c-article-section__content').html();
let contents;
if (description !== '' && abs !== null) {
contents = `${description}<p><div><center style="font-size: 16pt"><b>Abstraction</b></center><p>${abs}</p></div>`;
} else {
if (abs !== null) {
contents = `<center><b>Abstraction</b></center>` + abs;
}
if (description !== '') {
contents = description;
}
const abs = capture('div#Abs1-content.c-article-section__content > p').html();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div>
<h2 align="left">Brief</h2>
<p>${brief}</p>
</div>
`;
}
let absContents = '';
if (abs !== null) {
absContents = `
<div>
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
const contents = briefContents + absContents;
const single = {
title,
+75
View File
@@ -0,0 +1,75 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://www.nature.com`;
const url = `${base}/ng/research`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.border-bottom-1.pb20').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
const brief = $('.hide-overflow.inline').text();
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
author =
$('.js-list-authors-3 li')
.slice(0, 1)
.text() + ' et al.';
} else {
author = $('.js-list-authors-3 li').text();
}
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
const abs = capture('div#Abs1-content.c-article-section__content > p').html();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div>
<h2 align="left">Brief</h2>
<p>${brief}</p>
</div>
`;
}
let absContents = '';
if (abs !== null) {
absContents = `
<div>
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
const contents = briefContents + absContents;
const single = {
title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: new Date(time).toUTCString(),
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Nature Genetics | Latest Research`,
description: `Nature Genetics, a nature research journal`,
link: url,
item: out,
};
};
+75
View File
@@ -0,0 +1,75 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://www.nature.com`;
const url = `${base}/nmeth/research`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.border-bottom-1.pb20').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 > a').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
const brief = $('.hide-overflow.inline').text();
const time = $('time').text();
let author;
if ($('.js-list-authors-3 li').length > 3) {
author =
$('.js-list-authors-3 li')
.slice(0, 1)
.text() + ' et al.';
} else {
author = $('.js-list-authors-3 li').text();
}
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(address);
const capture = cheerio.load(res.data);
const abs = capture('div#Abs1-content.c-article-section__content > p').html();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div>
<h2 align="left">Brief</h2>
<p>${brief}</p>
</div>
`;
}
let absContents = '';
if (abs !== null) {
absContents = `
<div>
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
const contents = briefContents + absContents;
const single = {
title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: new Date(time).toUTCString(),
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Nature Method | Latest Research`,
description: `Nature Method, a nature research journal`,
link: url,
item: out,
};
};
+1
View File
@@ -23,6 +23,7 @@ module.exports = async (ctx) => {
'reading',
'government',
'study',
'journal',
'finance',
'other',
];
+98
View File
@@ -0,0 +1,98 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://advances.sciencemag.org`;
const res = await got.get(base);
const pageCapture = cheerio.load(res.data);
const list = pageCapture('ul > li > div > div > article > div').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3').text();
const partial = $('h3 > a').attr('href');
// TODO: .full.txt is a way for getting text preview
const address = `${base}${partial}`;
let author;
const authorList = $('span.highwire-citation-authors > span.highwire-citation-author')
.map(function(i, el) {
return $(el).text();
})
.get();
if (authorList.length > 5) {
author = authorList.slice(0, 5).join(', ') + ' <i>et al.</i>';
} else {
author = authorList.join(', ');
}
const time = new Date($('p.highwire-cite-metadata > time').text()).toUTCString();
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
// contents
// brief content
const brief = $('div.highwire-cite-snippet > div > div > p').text();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div id="content-brief">
<h2>Brief</h2>
<p>${brief}</p>
</div>
`;
}
const itemPage = await got.get(address);
const itemCapture = cheerio.load(itemPage.data);
// section and subject content
const section = itemCapture('header > div.overline > span.overline__section').text();
const subject = itemCapture('header > div.overline > span.overline__subject').text();
let sectionContents = '';
if (section !== '' || subject !== '') {
sectionContents = `
<div id="content-section">
<span style="color: #d40016; text-transform: uppercase; font-weight: 700;">${section}</span>
<span style="color: #666; text-transform: uppercase; font-weight: 400; border-left: 1px solid #e6e6e6;">${subject}</span>
</div>
`;
}
// abs content
const abs = itemCapture('div.section.abstract > p').text();
let absContents = '';
if (abs !== '') {
absContents = `
<div id="content-abs">
<h2>Abstract</h2>
${abs}
</div>
`;
}
const contents = sectionContents + briefContents + absContents;
const single = {
title: title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: time,
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Science | First Release`,
description: `Science, a research journal. For papers that published online.`,
link: base,
item: out,
};
};
+110
View File
@@ -0,0 +1,110 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://science.sciencemag.org`;
const res = await got.get(base);
const pageCapture = cheerio.load(res.data);
// just select paper relative sections
const sectionList = ['issue-toc-section-research-articles', 'issue-toc-section-review', 'issue-toc-section-reports'];
const list = [].concat.apply(
[],
sectionList.map((section) => {
const sec = cheerio.load(pageCapture(`ul > li .issue-toc-section.${section}`).html());
const sectionName = sec('h2').text();
const sectionList = sec('ul > li > div > div > article > div')
.append(`<div class="toc-section-type">${sectionName}</div>`)
.get();
return sectionList;
})
);
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
const section = $('div .toc-section-type').text();
let author;
const authorList = $('span.highwire-citation-authors > span.highwire-citation-author')
.map(function(i, el) {
return $(el).text();
})
.get();
if (authorList.length > 5) {
author = authorList.slice(0, 5).join(', ') + ' <i>et al.</i>';
} else {
author = authorList.join(', ');
}
const time = new Date($('p.highwire-cite-metadata > time').text()).toUTCString();
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
// contents
// section content
let sectionContents = '';
if (section !== '') {
sectionContents = `
<div id="content-section">
<span style="color: #d40016; text-transform: uppercase; font-weight: 700;">${section}</span>
</div>
`;
}
// brief content
const brief = $('div.highwire-cite-snippet > div > div > p').text();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div id="content-brief">
<p>${brief}</p>
</div>
`;
}
const itemPage = await got.get(address);
const itemCapture = cheerio.load(itemPage.data);
const abs = itemCapture('div > div.abstract-view > div.section')
.map(function(i, el) {
return $(el).html();
})
.get()
.join('<br>');
// abs content
let absContents = '';
if (abs !== null) {
absContents = `
<div id="content-abs">
${abs}
</div>
`;
}
const contents = sectionContents + briefContents + absContents;
const single = {
title: title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: time,
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Science | Current Table of Contents`,
description: `Science, a research journal`,
link: base,
item: out,
};
};
+99
View File
@@ -0,0 +1,99 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const base = `https://science.sciencemag.org/content/early/recent`;
const res = await got.get(base);
const pageCapture = cheerio.load(res.data);
const list = pageCapture('ul > li > div > article > div').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3').text();
const partial = $('h3 > a').attr('href');
const address = `${base}${partial}`;
let author;
const authorList = $('span.highwire-citation-authors > span.highwire-citation-author')
.map(function(i, el) {
return $(el).text();
})
.get();
if (authorList.length > 5) {
author = authorList.slice(0, 5).join(', ') + ' <i>et al.</i>';
} else {
author = authorList.join(', ');
}
const time = new Date($('p.highwire-cite-metadata > time').text()).toUTCString();
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
// contents
// brief content
const brief = $('div.highwire-cite-snippet > div > div > p').text();
let briefContents = '';
if (brief !== '') {
briefContents = `
<div id="content-brief">
<p>${brief}</p>
</div>
`;
}
const itemPage = await got.get(address);
const itemCapture = cheerio.load(itemPage.data);
// section content
const section = itemCapture('header > div.overline').text();
let sectionContents = '';
if (section !== '') {
sectionContents = `
<div id="content-section">
<span style="color: #d40016; text-transform: uppercase; font-weight: 700;">${section}</span>
<span style="color: grey; text-transform: uppercase; font-weight: 700;">[Published Online]</span>
</div>
`;
}
// abs content
const abs = itemCapture('div > div.abstract-view > div.section')
.map(function(i, el) {
return $(el).html();
})
.get()
.join('<br>');
let absContents = '';
if (abs !== null) {
absContents = `
<div id="content-abs">
${abs}
</div>
`;
}
const contents = sectionContents + briefContents + absContents;
const single = {
title: title,
author: author,
description: contents,
link: address,
guid: address,
pubDate: time,
};
ctx.cache.set(address, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `Science | First Release`,
description: `Science, a research journal. For papers that published online.`,
link: base,
item: out,
};
};
+60
View File
@@ -0,0 +1,60 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const base = 'http://zhishifenzi.com';
module.exports = async (ctx) => {
const link = `${base}/depth/`;
const response = await got.get(encodeURI(link));
const pageCapture = cheerio.load(response.data);
const title = '深度';
const list = pageCapture('div.inner_depth_list > ul > li').get();
const out = await Promise.all(
list.slice(0, 3).map(async (elem) => {
const $ = cheerio.load(elem);
const title = $('h5 > a').text();
const partial = $('h5 > a').attr('href');
const address = url.resolve(base, partial);
const pubDate = date(
$('div.inner_depth_list_tags > p')
.first()
.text()
);
// const keywords = $('div.inner_depth_list_tags > p')
// .last()
// .html();
const item = {
title,
pubDate,
link: encodeURI(address),
};
const value = await ctx.cache.get(address);
if (value) {
item.description = value;
} else {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
item.description = detailCapture('.main div.inner_content')
.html()
.replace(/src="\//g, `src="${base}/`);
ctx.cache.set(address, item.description);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `知識分子 | ${title}`,
description: `知識分子 | 科學 文明 智慧`,
link: link,
item: out,
};
};
+74
View File
@@ -0,0 +1,74 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const base = 'http://zhishifenzi.com';
module.exports = async (ctx) => {
let link = `${base}/innovation`;
const type = ctx.params.type;
// type can be on of:
// ["multiple", "company", "product", "technology", "character", "policy"]
if (typeof type !== 'undefined') {
link = `${link}/${type}`;
}
const response = await got.get(encodeURI(link));
const pageCapture = cheerio.load(response.data);
let title = pageCapture('div.innovation_nav > ul > li.sel')
.text()
.split(':')
.pop();
if (title === '') {
title = '全部創新';
} else {
title = `創新「${title}」`;
}
const list = pageCapture('div.inner_news_list > ul > li.clearfix').get();
const out = await Promise.all(
list.slice(0, 4).map(async (elem) => {
const $ = cheerio.load(elem);
const title = $('div > h5 > a').text();
const partial = $('div > h5 > a').attr('href');
const address = url.resolve(base, partial);
const pubDate = date(
$('span')
.first()
.text()
);
// const keywords = $('span')
// .last()
// .html();
const item = {
title,
pubDate,
link: encodeURI(address),
};
const value = await ctx.cache.get(address);
if (value) {
item.description = value;
} else {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
item.description = detailCapture('.main div.inner_content')
.html()
.replace(/src="\//g, `src="${base}/`);
ctx.cache.set(address, item.description);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `知識分子 | ${title}`,
description: `知識分子 | 科學 文明 智慧`,
link: link,
item: out,
};
};
+74
View File
@@ -0,0 +1,74 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const base = 'http://zhishifenzi.com';
module.exports = async (ctx) => {
let link = `${base}/news/`;
const type = ctx.params.type;
// type can be on of:
// ["biology", "medicine", "ai", "physics", "chymistry", "astronomy", "others"]
if (typeof type !== 'undefined') {
link = `${base}/news/${type}`;
}
const response = await got.get(encodeURI(link));
const pageCapture = cheerio.load(response.data);
let title = pageCapture('div.all_categories > ul > li.category')
.text()
.split(':')
.pop();
if (title === '') {
title = '全部資訊';
} else {
title = `資訊「${title}」`;
}
const list = pageCapture('div.inner_news_list > ul > li.clearfix').get();
const out = await Promise.all(
list.map(async (elem) => {
const $ = cheerio.load(elem);
const title = $('div > h5 > a').text();
const partial = $('div > h5 > a').attr('href');
const address = url.resolve(base, partial);
const pubDate = date(
$('span')
.first()
.text()
);
// const keywords = $('span')
// .last()
// .html();
const item = {
title,
pubDate,
link: encodeURI(address),
};
const value = await ctx.cache.get(address);
if (value) {
item.description = value;
} else {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
item.description = detailCapture('.main div.inner_content')
.html()
.replace(/src="\//g, `src="${base}/`);
ctx.cache.set(address, item.description);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `知識分子 | ${title}`,
description: `知識分子 | 科學 文明 智慧`,
link: link,
item: out,
};
};