Sha256: d033a1d936453e3c95c2c97d8b58377645ed5312660694689a49ebce0c93bf62

Contents?: true

Size: 1.74 KB

Versions: 7

Compression:

Stored size: 1.74 KB

Contents

require 'date'
require_relative 'base_client'

class AdopsReportScrapper::TripleliftClient < AdopsReportScrapper::BaseClient
  private

  def login
    @client.visit 'https://console.triplelift.com/login'
    @client.fill_in 'Email', :with => @login
    @client.fill_in 'Password', :with => @secret
    @client.click_button 'Sign in'
    begin
      @client.find :xpath, '//*[text()="Reporting"]'
    rescue Exception => e
      raise e, 'Triplelift login error'
    end
  end

  def scrap
    @date_str = @date.strftime('%B %d, %Y')
    @client.find(:xpath, '//*[text()="Reporting"]').click
    wait_for_spin
    @client.find(:xpath, '//button[contains(text(),"Last 7 days")]').click
    @client.find(:xpath, '//a[contains(text(),"Yesterday")]').click
    sleep 1
    
    @client.find(:xpath, '//tl-checkbox/div/span[text()[normalize-space()="Placement"]]').click
    sleep 1
    @client.find(:xpath, '//tl-checkbox/div/span[text()[normalize-space()="Clicks"]]').click
    sleep 1
    @client.find(:xpath, '//*[text()[normalize-space()="Run query"]]').click
    sleep 10

    extract_data
  end

  def extract_data
    @data = []
    return if @client.find_all(:xpath, '//*[text()="No data available for selected date range"]').count > 0

    rows = @client.find_all :xpath, '//table/*/tr'
    rows = rows.to_a
    rows.pop
    header = rows.shift
    n_data = rows.map { |tr| tr.find_css('td,th').map { |td| td.visible_text } }
    if @data.count == 0
      n_header = header.find_css('td,th').map { |td| td.visible_text }
      @data << n_header
    end
    @data += n_data
  end

  def wait_for_spin
    30.times do |_i| # wait 5 min
      begin
        @client.find(:css, '.spinner')
      rescue Exception => e
        break
      end
      sleep 5
    end
    sleep 2
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
adops_report_scrapper-0.1.42 lib/adops_report_scrapper/triplelift_client.rb
adops_report_scrapper-0.1.41 lib/adops_report_scrapper/triplelift_client.rb
adops_report_scrapper-0.1.40 lib/adops_report_scrapper/triplelift_client.rb
adops_report_scrapper-0.1.38 lib/adops_report_scrapper/triplelift_client.rb
adops_report_scrapper-0.1.37 lib/adops_report_scrapper/triplelift_client.rb
adops_report_scrapper-0.1.36 lib/adops_report_scrapper/triplelift_client.rb
adops_report_scrapper-0.1.34 lib/adops_report_scrapper/triplelift_client.rb