#!/usr/bin/env ruby -w # # $Id$ require 'uri' require 'net/http' require 'rss/1.0' require 'rss/dublincore' cat = (ARGV.length > 0) ? ARGV[0] : "apa" match = (ARGV.length > 1) ? /#{ARGV[1]}/i : //i price_limit = (ARGV.length > 2) ? ARGV[2].to_i : 2**32 hours = (ARGV.length > 3) ? ARGV[3].to_i : 2**32 earliest = Time::now - hours * 60 * 60 puts "earliest: #{earliest}" if $-d city = (ARGV.length > 4) ? ARGV[4] : "sfbay" region = (ARGV.length > 5) ? ARGV[5] : nil if ARGV.length > 6 puts "usage: #{$0} [] [] [] []" + "[] []" exit 64 # EX_USAGE end url_base = "http://#{city}.craigslist.org/" url = URI::parse(url_base + ((region) ? region + "/" : "") + cat + "/index.rss") $stderr.puts url if $-d res = Net::HTTP::start(url.host, url.port) do |http| http.get(url.path) end raise "GET #{url} failed: result=#{res.code}" unless res.code.to_i == 200 rss = RSS::Parser.parse(res.body) rss.items.each do |item| if item.title =~ /^(.*) \$(\d+)( \d+bd)?$/ listing = $1 price = $2.to_i else listing = item.title price = 0 end if match =~ listing and price < price_limit and item.dc_date > earliest print item.title + "\n " + item.link + "\n\n" end end