class PointmdComments::Aggregators::Main

Constants

CHROME_ARGS

Attributes

all_comments[R]
browser[R]
comments_aggregator[R]
output[R]
path[R]
posts[R]
posts_aggregator[R]
source[R]

Public Class Methods

new(options) click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 8
def initialize(options)
  # Currently 'path' is not supported
  @path                = nil
  @output              = options[:output] || default_output_path
  @source              = options[:source] || Aggregators::Posts::DEFAULT_SOURCE
  @posts_aggregator    = Aggregators::Posts.new(source: source, path: path)
  @comments_aggregator = Aggregators::Comments.new

  client = Selenium::WebDriver::Remote::Http::Default.new
  # NOTE: #timeout= is deprecated, use #read_timeout= and #open_timeout= instead
  client.timeout = 600 # instead of the default 60 (seconds)

  @browser             = ::Watir::Browser.new :chrome, http_client: client, headless: true, args: CHROME_ARGS
  @all_comments        = []
end

Public Instance Methods

call() click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 24
def call
  @posts = posts_aggregator.call

  collect_all_comments

  write_to_csv
end

Private Instance Methods

collect_all_comments() click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 34
def collect_all_comments
  posts.each do |url|
    post_comments = collect_comments_from(url)

    post_comments.each do |c|
      all_comments << ["https://point.md#{url}", c]
    end
  end
end
collect_comments_from(url) click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 44
def collect_comments_from(url)
  puts "Collecting comments for #{url}.."

  browser.goto "https://point.md#{url}"
  browser.div(id: 'simpals-comments-list').wait_until(&:present?)

  page = Nokogiri::HTML.parse(browser.html)

  comments_aggregator.call(page)
end
current_time() click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 69
def current_time
  Time.now.strftime('%Y%m%d_%H%M')
end
default_output_path() click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 65
def default_output_path
  "pointmd_comments_#{current_time}.csv"
end
write_to_csv() click to toggle source
# File lib/pointmd_comments/aggregators/main.rb, line 55
def write_to_csv
  # File#expand_path is needed to process paths like '~/test.txt' => '/Users/me/test.txt'
  file_path = File.expand_path(output)
  puts "File Path is #{file_path}"

  CSV.open(file_path, 'w') do |csv|
    all_comments.each { |c| csv << c }
  end
end