Skip to content

Commit

Permalink
feat: 添加规则调试工具
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed Jul 23, 2024
1 parent a988a4e commit 23a01c6
Show file tree
Hide file tree
Showing 23 changed files with 694 additions and 389 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './analyzer/RuleManager'
export * from './analyzer/AnalyzerManager'
export { fetch as analyzerUrl } from './analyzer/AnalyzerUrl'
2 changes: 1 addition & 1 deletion packages/rule-utils/src/maccms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function cmsToRule(json: any, url: string) {
discoverDescription: '$.vod_content##</?s?pa?n?.*?>',
discoverResult: '$.vod_id',
discoverAuthor: '',
discoverItems: '',
// discoverItems: '',
enableSearch: true,
searchUrl: '/api.php/provide/vod?ac=detail&pg=$page&wd=$keyword',
searchAuthor: '',
Expand Down
6 changes: 3 additions & 3 deletions packages/rule-utils/src/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface Rule {
discoverAuthor: string // 发现页 - 作者
discoverDescription: string // 发现页 - 描述
discoverResult: string // 发现页 - 结果
discoverItems: string
// discoverItems: string
discoverTags: string
discoverChapter: string
discoverNextUrl?: string
Expand Down Expand Up @@ -82,7 +82,7 @@ export interface Rule {
viewStyle?: number
}

export function createRule(rule: Rule | any): Rule {
export function createRule(rule: Partial<Rule>): Rule {
const now = Date.now() * 1000

return Object.assign({
Expand Down Expand Up @@ -114,7 +114,7 @@ export function createRule(rule: Rule | any): Rule {
chapterCover: '',
chapterTime: '',
discoverAuthor: '',
discoverItems: '',
// discoverItems: '',
searchList: '',
searchTags: '',
searchName: '',
Expand Down
155 changes: 117 additions & 38 deletions packages/shared/src/controller/RuleManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import md5 from 'blueimp-md5'
import { RuleManager as RM } from '@any-reader/core'
import { AnalyzerManager, RuleManager as RM, analyzerUrl } from '@any-reader/core'
import type { Rule } from '@any-reader/rule-utils'
import { ContentType } from '@any-reader/rule-utils'
import { Cacheable, Controller, Post } from '../decorators'
import type { BookChapter } from '../utils/book-manager'
Expand All @@ -9,24 +10,27 @@ import { BaseController } from './BaseController'
@Controller('/rule-manager')
export class RuleManager extends BaseController {
@Post('discover-map')
async discoverMap(arg: { ruleId: string }) {
const rule = await this.getRule(arg.ruleId)
const ruleManager = new RM(rule)
return await ruleManager.discoverMap()
async discoverMap({ ruleId }: { ruleId: string }) {
const rule = await this.getRule(ruleId)
return await this.discoverMapByRule({ rule })
}

@Post('discover')
async discover(arg: { ruleId: string; data: any }) {
const rule = await this.getRule(arg.ruleId)
const ruleManager = new RM(rule)
return ruleManager.discover(arg.data.value)
async discover({ ruleId, data }: { ruleId: string; data: any }) {
const rule = await this.getRule(ruleId)
return await this.discoverByRule({ rule, data })
}

@Post('search-by-rule-id')
async searchByRuleId(arg: { ruleId: string; keyword: string }) {
const rule = await this.getRule(arg.ruleId)
const analyzer = new RM(rule)
return await analyzer.search(arg.keyword)
async searchByRuleId({
ruleId,
keyword,
}: {
ruleId: string
keyword: string
}) {
const rule = await this.getRule(ruleId)
return await this.searchByRule({ rule, keyword })
}

@Post('chapter')
Expand All @@ -37,17 +41,16 @@ export class RuleManager extends BaseController {
return `chapter@${ruleId || '__local__'}@${md5(filePath)}`
},
})
async getChapter({ ruleId, filePath }: { ruleId?: string; filePath: string }) {
async getChapter({
ruleId,
filePath,
}: {
ruleId?: string
filePath: string
}) {
if (ruleId) {
const rule = await this.getRule(ruleId)
const rm = new RM(rule)
const list = await rm.getChapter(filePath)
return list.map((e: any) => ({
...e,
name: e.name,
chapterPath: e.url,
}),
)
return await this.chapterByRule({ rule, filePath })
}
// 本地
return bookManager.getChapter(filePath)
Expand All @@ -60,32 +63,108 @@ export class RuleManager extends BaseController {
return `content@${ruleId || '__local__'}@${md5(filePath)}@v2_${md5(chapterPath)}`
},
})
async content({ filePath, chapterPath, ruleId }: any) {
async content({
filePath,
chapterPath,
ruleId,
}: {
ruleId: string
filePath: string
chapterPath: string
}) {
// 在线
if (ruleId) {
const rule = await this.getRule(ruleId)
const rm = new RM(rule)
const content: string[] = await rm.getContent(chapterPath)
let text: string | string[] = ''
if (rule.contentType === ContentType.MANGA)
text = content.map(src => `<img src="${src}"/>`)
else if (rule.contentType === ContentType.VIDEO)
text = content?.[0] || ''
else
text = content

return {
content: text,
}
return await this.contentByRule({ rule, chapterPath })
}
// 本地
const content = await bookManager.getContent(toBookChapter(filePath, chapterPath))
const content = await bookManager.getContent(
toBookChapter(filePath, chapterPath),
)
return {
content,
}
}

private async getRule(ruleId: string) {
@Post('search-by-rule')
async searchByRule({ rule, keyword }: { rule: Rule; keyword: string }) {
const analyzer = new RM(rule)
return await analyzer.search(keyword)
}

@Post('chapter-by-rule')
async chapterByRule({ rule, filePath }: { rule: Rule; filePath: string }) {
const rm = new RM(rule)
const list = await rm.getChapter(filePath)
return list.map((e: any) => ({
...e,
name: e.name,
chapterPath: e.url,
}))
}

@Post('content-by-rule')
async contentByRule({
chapterPath,
rule,
}: {
rule: Rule
chapterPath: string
}) {
const rm = new RM(rule)
const content: string[] = await rm.getContent(chapterPath)
let text: string | string[] = ''
if (rule.contentType === ContentType.MANGA)
text = content.map(src => `<img src="${src}"/>`)
else if (rule.contentType === ContentType.VIDEO)
text = content?.[0] || ''
else text = content

return {
content: text,
}
}

@Post('discover-map-by-rule')
async discoverMapByRule({ rule }: { rule: Rule }) {
const ruleManager = new RM(rule)
return await ruleManager.discoverMap()
}

@Post('discover-by-rule')
async discoverByRule({ rule, data }: { rule: Rule; data: any }) {
const ruleManager = new RM(rule)
return await ruleManager.discover(data.value)
}

@Post('analyzer-text')
async analyzerText({
inputText,
ruleText,
isArray,
}: {
inputText: string
ruleText: string
isArray: boolean
}) {
const analyzerManager = new AnalyzerManager(inputText)
return isArray ? await analyzerManager.getElements(ruleText) : await analyzerManager.getString(ruleText)
}

@Post('analyzer-url')
async analyzerUrl({
rule,
url,
keyword,
}: {
url: string
rule: Rule
keyword: string
}) {
return await analyzerUrl(url, keyword, '', rule)
}

private async getRule(ruleId: string): Promise<Rule> {
const rule = await this.db.getResourceRule().findById(ruleId)
if (!rule)
throw new Error('规则不存在')
Expand Down
6 changes: 4 additions & 2 deletions packages/vscode/src/webview/WebviewEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class WebviewEvent {
this._pm = new EasyPostMessage(createAdapter(webview));
this._pm.answer('post@vscode/executeCommand', this.executeCommand.bind(this));

createApp({
const app = createApp({
configPath: CONFIG_PATH,
dataSourceOptions: {
driver: require('sql.js/dist/sql-wasm'),
Expand All @@ -31,7 +31,9 @@ export class WebviewEvent {
}
}
}
}).useApi(this._pm.answer.bind(this._pm));
});

app.useApi(this._pm.answer.bind(this._pm));
}

private executeCommand({ command, data }: any) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"module": "ESNext",
"target": "ES2016",
"sourceMap": true,
"strict": true
Expand Down
3 changes: 1 addition & 2 deletions packages/web/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
AApp: typeof import('ant-design-vue/es')['App']
ABackTop: typeof import('ant-design-vue/es')['BackTop']
AButton: typeof import('ant-design-vue/es')['Button']
AButtonGroup: typeof import('ant-design-vue/es')['ButtonGroup']
ACheckboxGroup: typeof import('ant-design-vue/es')['CheckboxGroup']
AConfigProvider: typeof import('ant-design-vue/es')['ConfigProvider']
ADivider: typeof import('ant-design-vue/es')['Divider']
ADropdown: typeof import('ant-design-vue/es')['Dropdown']
AEmpty: typeof import('ant-design-vue/es')['Empty']
AForm: typeof import('ant-design-vue/es')['Form']
Expand Down
59 changes: 59 additions & 0 deletions packages/web/src/api/modules/rule-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export function discoverMap(ruleId: string) {
});
}

export function discoverMapByRule(data: any) {
return request({
method: 'post',
url: 'rule-manager/discover-map-by-rule',
data
});
}

// 发现页分类下内容
export function discover(data: any) {
return request({
Expand All @@ -24,6 +32,14 @@ export function discover(data: any) {
});
}

export function discoverByRule(data: any) {
return request({
method: 'post',
url: 'rule-manager/discover-by-rule',
data
});
}

// 根据规则ID搜索内容
export function searchByRuleId(data: { ruleId: string; keyword: string }) {
return request({
Expand All @@ -34,6 +50,15 @@ export function searchByRuleId(data: { ruleId: string; keyword: string }) {
});
}

// 根据规则搜索内容
export function searchByRule(data: any) {
return request({
method: 'post',
url: 'rule-manager/search-by-rule',
data
});
}

// 获取内容
export function getContent(data: any) {
return request({
Expand All @@ -43,6 +68,15 @@ export function getContent(data: any) {
});
}

// 获取内容
export function getContentByRule(data: any) {
return request({
method: 'post',
url: 'rule-manager/content-by-rule',
data
});
}

// 获取章节
export function getChapter(filePath: string, ruleId?: string) {
return request({
Expand All @@ -54,3 +88,28 @@ export function getChapter(filePath: string, ruleId?: string) {
}
});
}

// 获取章节
export function getChapterByRule(data: any) {
return request({
method: 'post',
url: 'rule-manager/chapter-by-rule',
data
});
}

export function analyzerText(data: any) {
return request({
method: 'post',
url: 'rule-manager/analyzer-text',
data
});
}

export function analyzerUrl(data: any) {
return request({
method: 'post',
url: 'rule-manager/analyzer-url',
data
});
}
10 changes: 0 additions & 10 deletions packages/web/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { version } from '../../package.json';

export enum CONTENT_TYPE {
MANGA = 0,
NOVEL = 1,
VIDEO = 2,
AUDIO = 3,
RSS = 4,
NOVELMORE = 5,
GAME = 101
}

export type TPlatform = 'browser' | 'vscode' | 'electron' | 'utools';

export const PLATFORM: TPlatform = import.meta.env.VITE_APP_PLATFORM;
Expand Down
Loading

0 comments on commit 23a01c6

Please sign in to comment.