Skip to content

Commit

Permalink
feat: filter by title
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 25, 2024
1 parent 2580488 commit 6235e0f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/get-bonus/src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ export class Scraper {
}

async search(text: string, options: Partial<SearchOptions> = {}) {
const { onlySearchTitle = true } = options;

const results = await Promise.all(
this.providers.map(async (provider) => {
try {
const resp = await provider.search(text, options);

// 对搜索结果进行进一步过滤, 结果的标题必须包含所有搜索字符串
if (onlySearchTitle) {
const pieces = text.split(' ').filter(Boolean);
const filtered = resp.filter((result) => pieces.every((p) => result.title.includes(p)));
resp.splice(0, resp.length, ...filtered);
}

return [provider.id, resp] as const;
} catch (error) {
console.log(`搜索 ${provider.id} 失败: ${text}`);
Expand All @@ -20,6 +30,7 @@ export class Scraper {
}
})
);

return Object.fromEntries(results);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/get-bonus/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface SearchOptions {
onlySearchTitle: string;

category: string;
// TODO
}
Expand Down

0 comments on commit 6235e0f

Please sign in to comment.