UNPKG

csdnsynchexo

Version:

支持csdn/博客园/掘金/segmentfault/腾讯云加社区等平台一键迁移hexo

99 lines (98 loc) 3.61 kB
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 { catchCount } from '../../decorator/index.js'; import { info } from '../../log/index.js'; const sitdown = new Sitdown({ keepFilter: ['style'], codeBlockStyle: 'fenced', bulletListMarker: '-', hr: '---', }); export class Juejin extends Base { headers = {}; async getMain() { const articleIds = []; let cursor = 0; while (true) { const { data: res } = await this.axios.post('https://api.juejin.cn/content_api/v1/article/query_list', { cursor: String(cursor), sort_type: 2, user_id: this.config.userId, }); const { err_no, data, has_more } = res; if (err_no === 0 && data) { articleIds.push(...data.map((x) => (this.config.cookie ? x.draft_id : x.article_id))); } if (has_more) { cursor += 10; } else { break; } } return articleIds; } async getDetail(id) { const { data: { data: { article_info: { content, ctime, mark_content, title }, tags, category, }, }, } = await this.axios.post('https://api.juejin.cn/content_api/v1/article/detail', { article_id: id, client_type: 2608, }); return { title, tags: tags.map((x) => x.tag_name), content: mark_content || sitdown.HTMLToMD(content), categories: [category?.category_name], date: new Date(Number(ctime) * 1000).toLocaleString('en-US', { timeZone: 'Asia/Shanghai', }), }; } async getDetail2(draftId) { const { data: { data: { article_draft: { mark_content, title, ctime }, category: { category_name }, tags, }, }, } = await this.axios.post('https://juejin.cn/content_api/v1/article_draft/detail', { draft_id: draftId, }); return { content: mark_content, title, date: new Date(Number(ctime) * 1000).toLocaleString('en-US', { timeZone: 'Asia/Shanghai', }), categories: [category_name], tags: tags.map((x) => x.tag_name), }; } async run() { const list = await this.getMain(); const resultList = []; if (this.config.cookie) { info('提供cookie'); } else { info('无cookie'); } for (const id of list) { info(`拉取:${id}`); let result; if (this.config.cookie) { result = await this.getDetail2(id); } else { result = await this.getDetail(id); } resultList.push(result); } return resultList; } } __decorate([ catchCount() ], Juejin.prototype, "getDetail", null); __decorate([ catchCount() ], Juejin.prototype, "getDetail2", null);