* Update topics.js * update doc(cn) for nautilus * update doc(en) for nautilus * refactor: use cache.tryGet * refactor: migrate to v2 * Update category.js * Revert "refactor:" This reverts commit 963d065833e5877d5200c3b07502b9ecc09eb9e6. * Revert "refactor: use cache.tryGet" This reverts commit 879bc58b8c917a144ac3670d3f2bc294e955bb60. * Revert "update doc(en) for nautilus" This reverts commit 41ce3f8f5637f5b8aaa211d8f169fb996b7834f4. * Revert "update doc(cn) for nautilus" This reverts commit 74458c78b5ac5df2aeb219d202c6e7a75d172e30. * Revert "Update topics.js" This reverts commit 988c443264189707d75645541c1be8ff01131f03.
41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
const cheerio = require('cheerio');
|
|
const got = require('@/utils/got');
|
|
|
|
const fetch = require('./fetch_article');
|
|
|
|
module.exports = async (ctx) => {
|
|
const baseURL = 'https://www.twreporter.org';
|
|
const url = baseURL + `/categories/${ctx.params.cid}`;
|
|
const res = await got(url);
|
|
const $ = cheerio.load(res.data);
|
|
const regexp = /^window\.__REDUX_STATE__=(.*);$/gm;
|
|
const raw = $('script[charset="UTF-8"]').html().replace(regexp, '$1');
|
|
const list = JSON.parse(raw);
|
|
const ids = list.entities.posts.allIds;
|
|
|
|
const links = [];
|
|
const titles = [];
|
|
const category = list.entities.posts.byId[ids[0]].categories[0].name;
|
|
|
|
for (const id of ids) {
|
|
links.push(baseURL + '/a/' + list.entities.posts.byId[id].slug);
|
|
titles.push(list.entities.posts.byId[id].title);
|
|
}
|
|
|
|
const out = await Promise.all(
|
|
links.map((item, index) => {
|
|
const title = titles[index];
|
|
return ctx.cache.tryGet(item, async () => {
|
|
const single = await fetch(item);
|
|
single.title = title;
|
|
return single;
|
|
});
|
|
})
|
|
);
|
|
ctx.state.data = {
|
|
title: `報導者 | ${category}`,
|
|
link: url,
|
|
item: out,
|
|
};
|
|
};
|