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

feat: allow preset cookies url #64

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/kimurai/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def request_to(handler, delay = nil, url:, data: {}, response_type: :html)
logger.warn "Spider: request_to: not unique url: #{url}, skipped" and return
end

visited = delay ? browser.visit(url, delay: delay) : browser.visit(url)
visited = delay ? browser.visit(url, delay: delay, data: data) : browser.visit(url, data: data)
return unless visited

public_send(handler, browser.current_response(response_type), { url: url, data: data })
Expand Down
10 changes: 5 additions & 5 deletions lib/kimurai/capybara_ext/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class Session
attr_accessor :spider

alias_method :original_visit, :visit
def visit(visit_uri, delay: config.before_request[:delay], skip_request_options: false, max_retries: 3)
def visit(visit_uri, delay: config.before_request[:delay], skip_request_options: false, max_retries: 3, data: {})
if spider
process_delay(delay) if delay
retries, sleep_interval = 0, 0

begin
check_request_options(visit_uri) unless skip_request_options
check_request_options(visit_uri, data[:preset_cookies_url]) unless skip_request_options
driver.requests += 1 and logger.info "Browser: started get request to: #{visit_uri}"
spider.class.update(:visits, :requests) if spider.with_info

Expand Down Expand Up @@ -179,7 +179,7 @@ def process_delay(delay)
sleep interval
end

def check_request_options(url_to_visit)
def check_request_options(url_to_visit, preset_cookies_url = nil)
# restart_if
if memory_limit = config.restart_if[:memory_limit]
memory = driver.current_memory
Expand All @@ -201,7 +201,7 @@ def check_request_options(url_to_visit)
# (Selenium only) if config.cookies present and browser was just created,
# visit url_to_visit first and only then set cookies:
if driver.visited.nil? && config.cookies && mode.match?(/selenium/)
visit(url_to_visit, skip_request_options: true)
visit(preset_cookies_url || url_to_visit, skip_request_options: true)
config.cookies.each do |cookie|
driver.set_cookie(cookie[:name], cookie[:value], cookie)
end
Expand All @@ -218,7 +218,7 @@ def check_request_options(url_to_visit)
# (Selenium only) if browser is not visited yet any page, visit url_to_visit
# first and then set cookies (needs after browser restart):
if driver.visited.nil? && mode.match?(/selenium/)
visit(url_to_visit, skip_request_options: true)
visit(preset_cookies_url || url_to_visit, skip_request_options: true)
end

config.cookies.each do |cookie|
Expand Down