Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle captcha errors #68

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/services/search/paper-search.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { jest } from '@jest/globals'
import { GoogleScholar } from '@rpidanny/google-scholar'
import { CaptchaError } from '@rpidanny/odysseus/dist/index.js'
import { Quill } from '@rpidanny/quill'
import { mock } from 'jest-mock-extended'

Expand Down Expand Up @@ -125,6 +126,18 @@ describe('PaperSearchService', () => {
)
})

it('should skip paper when CaptchaError is thrown', async () => {
paperService.getTextContent.mockRejectedValue(new CaptchaError())

const entities = await service.search({
keywords: 'some keywords',
minItemCount: 10,
filterPattern: 'cas9',
})

expect(entities).toHaveLength(0)
})

it('should summarize papers if summarize is true', async () => {
llmService.summarize.mockResolvedValue('This is a summary.')

Expand Down
37 changes: 23 additions & 14 deletions src/services/search/paper-search.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GoogleScholar, IPaperMetadata } from '@rpidanny/google-scholar/dist'
import { CaptchaError } from '@rpidanny/odysseus'
import { Quill } from '@rpidanny/quill'
import { join } from 'path'

Expand Down Expand Up @@ -83,21 +84,29 @@ export class PaperSearchService {

if (!filterPattern && !summarize) return entity

const textContent = await this.paperService.getTextContent(paper)

if (filterPattern) {
const matches = await this.paperService.findInPaper(textContent, filterPattern)
if (matches.length === 0) return undefined
this.logMatches(matches)
entity.matches = matches
}

if (summarize) {
const summary = await this.llmService.summarize(textContent)
entity.summary = summary
try {
const textContent = await this.paperService.getTextContent(paper)

if (filterPattern) {
const matches = await this.paperService.findInPaper(textContent, filterPattern)
if (matches.length === 0) return undefined
this.logMatches(matches)
entity.matches = matches
}

if (summarize) {
const summary = await this.llmService.summarize(textContent)
entity.summary = summary
}

return entity
} catch (error) {
if (error instanceof CaptchaError) {
this.logger?.debug(`Failed processing paper due to Captcha: ${paper.url}`)
return
}
throw error
}

return entity
}

private logMatches(foundItems: ITextMatch[]): void {
Expand Down
Loading