csdnsynchexo
Version:
支持csdn/博客园/掘金/segmentfault/腾讯云加社区等平台一键迁移hexo
66 lines (65 loc) • 2.37 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Base } from '../base.js';
import { Sitdown } from 'sitdown';
import { load } from 'cheerio';
import { catchCount } from '../../decorator/index.js';
const sitdown = new Sitdown({
keepFilter: ['style'],
codeBlockStyle: 'fenced',
bulletListMarker: '-',
hr: '---',
});
export class Segmentfault extends Base {
headers = {};
async getDetail(url) {
const { data: html } = await this.axios.get(`https://segmentfault.com${url}`);
const $ = load(html);
const content = sitdown.HTMLToMD($('.article').html());
const date = $('time').attr('datetime');
const tags = [];
const title = $('#sf-article_title .text-body').text();
$('.m-n1 .badge-tag').each(function () {
tags.push($(this).text().replace(/\s/g, ''));
});
return {
date,
categories: [],
content,
title,
tags,
};
}
async getMain() {
let page = 1;
const urls = [];
let emptyBreak = false;
while (true) {
const { data: html } = await this.axios.get(`https://segmentfault.com/u/${this.config.userId}/articles?page=${page++}`);
if (emptyBreak) {
break;
}
const $ = load(html);
const list = $('.profile-mine__content--title');
if (list?.length > 0) {
list.each(function () {
const url = $(this).attr('href');
if (url) {
urls.push(url);
}
});
}
else {
break;
}
}
return urls;
}
}
__decorate([
catchCount()
], Segmentfault.prototype, "getDetail", null);