feat: add max crawl depth
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
class Captain::Tools::NotionCrawlService
|
||||
# The crawler will go 4 levels from the root. 5 levels in total
|
||||
MAX_CRAWL_DEPTH = 4
|
||||
|
||||
attr_reader :account, :page_id
|
||||
|
||||
def initialize(account, page_id)
|
||||
@@ -17,8 +20,9 @@ class Captain::Tools::NotionCrawlService
|
||||
|
||||
private
|
||||
|
||||
def extract_child_pages(page_id, visited = Set.new)
|
||||
def extract_child_pages(page_id, visited = Set.new, current_depth = 0)
|
||||
return [] if visited.include?(page_id)
|
||||
return [] if current_depth >= MAX_CRAWL_DEPTH
|
||||
|
||||
visited.add(page_id)
|
||||
|
||||
@@ -30,7 +34,7 @@ class Captain::Tools::NotionCrawlService
|
||||
page_data['child_pages']&.each do |child_page|
|
||||
child_id = child_page['id']
|
||||
child_pages << child_id
|
||||
child_pages.concat(extract_child_pages(child_id, visited))
|
||||
child_pages.concat(extract_child_pages(child_id, visited, current_depth + 1))
|
||||
end
|
||||
|
||||
child_pages
|
||||
@@ -39,4 +43,4 @@ class Captain::Tools::NotionCrawlService
|
||||
def processor_service
|
||||
@processor_service ||= Integrations::Notion::ProcessorService.new(account: account)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user