-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl.rb
executable file
·50 lines (44 loc) · 1.6 KB
/
cl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby
require 'mechanize'
agent = Mechanize.new
header = "<html><body><table class='sortable' id='jobs'><thead>
<tr><th scope=col>Date</th>
<th scope=col>Title</th>
<th scope=col>Category</th>
<th scope=col>Location</th>
<th scope=col>Area</th>
</tr></thead><tbody>"
footer = "</tbody></table></body></html>"
newFile = File.open('craigs_jobs.html', 'w')
newFile.syswrite header
#change this to the cities you want
locations = ARGV.empty? ? ['milwaukee', 'madison'] : ARGV
#change this to the categories you want
categories = ['sof', 'sad', 'tch', 'web']
jobs = []
locations.each do |location|
categories.each do |category|
page = agent.get("http://#{location}.craigslist.org/#{category}/")
page.search('//h4[@class="ban"]|//p/a').each do |found|
if found.name=='h4' && !found.text.empty?
@date = Time.parse(found.text)
@date = Time.parse("#{found.text} #{Time.now.year-1}") if @date > Time.now
else
jobs << {location: location,
area: (found.next.next.nil? ? "" : found.next.next.text),
category: category,
date: @date.strftime("%Y/%m/%d"),
link: "<a href='#{found.attributes['href'].value}'>#{found.text[0,40]}</a>"}
end
end
end
end
def tabularize job
"<tr><td>#{job[:date]}</td>
<td>#{job[:link]}</td>
<td>#{job[:category]}</td>
<td>#{job[:location]}</td>
<td>#{job[:area]} </td></tr>"
end
jobs.each{ |job| newFile.syswrite tabularize(job) }
newFile.syswrite footer