# frozen_string_literal: true
class Scrapers::SimilarWeb::StartRun
BASE_URL = 'https://api.apify.com/v2/'
ENDPOINT_URL = 'acts/m0uka~similarweb-scraper/runs'
RUN_SUCCEEDED_EVENT_TYPE = 'ACTOR.RUN.SUCCEEDED'
def perform
start_actor
end
private
def bearer_token
"Bearer #{Rails.application.credentials.dig(:apify, :token)}"
end
def body_json
{
proxyConfigurationOptions: {
'useApifyProxy': true
},
websites: websites
}.to_json
end
def headers
{
"Content-Type": 'application/json; charset=utf-8',
"Authorization": bearer_token
}
end
def start_actor
HTTParty.post(url, body: body_json, headers: headers)
end
def url
"#{BASE_URL}#{ENDPOINT_URL}?webhooks=#{url_encoded_webhooks}"
end
def url_encoded_webhooks
webhooks_json = [{
'eventTypes': [RUN_SUCCEEDED_EVENT_TYPE],
'requestUrl': webhook_url
}].to_json
URI.encode_www_form_component(webhooks_json)
end
def webhook_url
return [ENV.fetch('WEBHOOK_URL', nil), 'wh/similarweb'].join if Rails.env.development?
similarweb_webhook_url
end
def websites
Calendar.all.map(&:similarweb_url)
end
end
# frozen_string_literal: true
class Scrapers::SimilarWeb::StartRun
BASE_URL = 'https://api.apify.com/v2/'
ENDPOINT_URL = 'acts/m0uka~similarweb-scraper/runs'
RUN_SUCCEEDED_EVENT_TYPE = 'ACTOR.RUN.SUCCEEDED'
def perform
start_actor
end
private
def bearer_token
"Bearer #{Rails.application.credentials.dig(:apify, :token)}"
end
def body_json
{
proxyConfigurationOptions: {
'useApifyProxy': true
},
websites: websites
}.to_json
end
def headers
{
"Content-Type": 'application/json; charset=utf-8',
"Authorization": bearer_token
}
end
def start_actor
HTTParty.post(url, body: body_json, headers: headers)
end
def url
"#{BASE_URL}#{ENDPOINT_URL}?webhooks=#{url_encoded_webhooks}"
end
def url_encoded_webhooks
webhooks_json = [{
'eventTypes': [RUN_SUCCEEDED_EVENT_TYPE],
'requestUrl': webhook_url
}].to_json
URI.encode_www_form_component(webhooks_json)
end
def webhook_url
return [ENV.fetch('WEBHOOK_URL', nil), 'wh/similarweb'].join if Rails.env.development?
similarweb_webhook_url
end
def websites
Calendar.all.map(&:similarweb_url)
end
end