Skip to content

Commit

Permalink
fix(yuque-sdk): 修复爬取目录时鉴权问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LetTTGACO committed Aug 10, 2023
1 parent c9a0ee5 commit 501d8bf
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/sdk-yuque/src/pwd/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { out, request, RequestOptions } from '@elog/shared'
import { encrypt, getProps } from '../utils'
import { YuQueResponse, YuqueDoc, YuqueDocProperties } from '../types'
import { DocDetail, YuqueCatalog, DocCatalog } from '@elog/types'
import { JSDOM } from 'jsdom'
import { JSDOM, VirtualConsole } from 'jsdom'
import { YuqueWithPwdConfig, YuqueLogin, YuqueLoginCookie } from './types'

/** 默认语雀API 路径 */
Expand Down Expand Up @@ -102,16 +102,25 @@ class YuqueClient {
* 获取目录
*/
async getToc() {
const url = `${this.baseUrl}/${this.namespace}`
const dom = await JSDOM.fromURL(url, { runScripts: 'dangerously' })
const { book } = dom?.window?.appData || {}
dom.window.close()
if (!book) {
out.warning('爬取语雀目录失败,请稍后重试')
try {
const res = await this.request(this.namespace, { method: 'get' })
const virtualConsole = new VirtualConsole()
const dom = new JSDOM(`${res}`, { runScripts: 'dangerously', virtualConsole })
virtualConsole.on('error', () => {
// don't do anything
})
const { book } = dom?.window?.appData || {}
dom.window.close()
if (!book) {
out.warning('爬取语雀目录失败,请稍后重试')
process.exit(-1)
}
this.bookId = book.id
return book?.toc || []
} catch (e: any) {
out.warning('爬取语雀目录失败,请稍后重试', e.message)
process.exit(-1)
}
this.bookId = book.id
return book?.toc || []
}

/**
Expand Down

0 comments on commit 501d8bf

Please sign in to comment.