#!/usr/bin/env ruby # frozen_string_literal: true # BSD 2-Clause License # # Copyright (c) 2023, Gregory Cohen # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. city = 'Ottawa' city.downcase! if ARGV.length != 2 # puts "Usage min max" # abort 1 end require 'puppeteer-ruby' # system "killall google-chrome"#Warning[:deprecated] = false def site ARGV[0].strip end def site min, max = ARGV min ||= '400' max ||= '600' "https://www.kijiji.ca/b-for-rent/#{city}/c30349001l1700185?price=#{min}__#{max}&sort=priceDesc" end def e return 'list' ARGV[1].strip end Warning[:deprecated] = false require 'shellwords' require 'fileutils' require 'colored' Puppeteer.launch(headless: false) do |browser| page = browser.pages[0] page.viewport = Puppeteer::Viewport.new(width: 1280, height: 800) page.goto(site, wait_until: 'domcontentloaded') str = " () => { var list = []; for (var a of $$('a')) { list.push(a.href); } function $() { return document.querySelector(arguments[0]); } function $$() { return document.querySelectorAll(arguments[0]); } var ret = #{e}; if (ret.innerHTML) { ret = ret.innerHTML; }; return list; }" x = 0 next_list = [] a = page.evaluate(str) i = 0 a.each do |i| if /page-[0-9]+/.match?(i) next_list.push(i) next end n = i.split('/')[3] next unless n.include?('v-') && n.include?('rent') x += 1 $stderr.print "#{x}. " page = browser.new_page page.goto i sleep if x == ARGV[2].to_i end end