Files
RSSHub/lib/routes/twreporter/fetch_article.js
T
Sukka d82847f541 style/chore(eslint): enforce new rules (#8040)
* style: prefer object shorthand syntax
* refactor: prefer Array#map over Array#forEach
* style: prefer arrow callback
* chore(eslint): update rules
* style: auto fix by eslint
2021-08-17 22:23:23 +08:00

67 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async function fetch(address) {
const res = await got.get(address);
const capture = cheerio.load(res.data);
capture('.gIMvvS').remove();
let metaInfoBox = capture('.iRdvCH')
.filter((index) => index === 0)
.get();
// For photography
if (metaInfoBox.length === 0) {
metaInfoBox = capture('.dPQBaW')
.filter((index) => index === 0)
.get();
}
const acquire = cheerio.load(metaInfoBox);
let time = acquire('.gimsRe').text();
// For `photography`
if (!time) {
time = acquire('.kHluJP').text();
}
// # Author(s) of the article
//
// There exists two formats for this section.
// 1. 文字 /...` with `摄影 / ..` in separate line, or
// 2. just a simple line starts with `文/ ...`
// For the first condition, we use a array
// to record the list of author(s), and use
// `;` to connect two lines.
const authors = [];
if (acquire('.loxoWO').text() === '') {
for (const item of acquire('.flciyI').get()) {
const $ = cheerio.load(item);
const job = $('.hGsNtm').text();
// An article may have multiple authors
const name = [];
for (const item of $('.cidPTd > a').get()) {
const $ = cheerio.load(item);
name.push($('.fJSaZP').text());
}
const author = job + '/' + name.join(',');
authors.push(author);
}
} else {
authors.push(acquire('.loxoWO').text());
}
const author = authors.join(';');
// contents = cover photo + italic intro + text
const contents = '<em>' + capture('.hxFBKc').html() + '</em><br>' + capture('.irqyDp').html();
return {
author,
description: contents,
link: address,
guid: address,
pubDate: new Date(time).toUTCString(),
};
};