forked from slurpcode/slurp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseek.rb
executable file
Β·291 lines (265 loc) Β· 7.44 KB
/
seek.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env ruby
# frozen_string_literal: true
# Need to finish `time`
# Wow, looks like there are still jobs for COBOL
require "csv"
require "fileutils"
require "mechanize"
require "optparse"
require "optparse/time"
require "paint"
# require 'pp'
def wtype(worktype)
case worktype
when "full"
242
when "part"
243
when "contract"
244
when "casual"
245
when "242"
242
when "243"
243
when "244"
244
when "245"
245
else
"242%2C243%2C244%2C245"
end
end
def enwtype(worktype)
case worktype
when 242
"full"
when 243
"part"
when 244
"contract"
when 245
"casual"
else
"all"
end
end
# Custom OptionParser class
class Parser
VERSION = "1.0.0"
# Custom OptionParser ScriptOptions
class ScriptOptions
attr_accessor :keyword, :location, :range, :worktype, :delay, :time
def define_options(parser)
parser.banner = "Usage: #{Paint['seek.rb [options]', :red, :white]}"
parser.separator ""
parser.separator "Specific options:"
# add additional options
specify_keyword_option(parser)
specify_location_option(parser)
specify_range_option(parser)
specify_worktype_option(parser)
delay_execution_option(parser)
execute_at_time_option(parser)
parser.separator ""
parser.separator "Common options:"
# No argument, shows at tail. This will print an options summary.
# Try it and see!
parser.on_tail("-h", "--help", "Show this message") do
puts parser
exit
end
# Another typical switch to print the version.
parser.on_tail("--version", "Show version") do
puts VERSION
exit
end
end
def specify_keyword_option(parser)
parser.on(
"-k",
"--keyword keyword",
'Keywords to search
separators include:
and, or, not'
) { |k| self.keyword = k }
end
def specify_location_option(parser)
parser.on("-l", "--location location", "Suburb, city or region") do |l|
self.location = l
end
end
def specify_range_option(parser)
parser.on(
"-r",
"--range range",
'Listed time in days
999 (default) or
1, 3, 7, 14, 31 or
any positive number'
) { |r| self.range = r }
end
def specify_worktype_option(parser)
parser.on(
"-w",
"--worktype worktype",
'Work type
all (default)
full or 242 (full time)
part or 243 (part time)
contract or 244 (contract/temp)
casual or 245 (casual/vacation)'
) { |w| self.worktype = wtype(w) }
end
def delay_execution_option(parser)
# Cast 'delay' argument to a Float.
parser.on("--delay N", Float, "Delay N seconds before executing") do |n|
self.delay = n
end
end
def execute_at_time_option(parser)
# Cast 'time' argument to a Time object.
parser.on(
"-t",
"--time [TIME]",
Time,
"Begin execution at given time"
) { |time| self.time = time }
end
end
#
# Return a structure describing the options.
#
def parse(args)
# The options specified on the command line will be collected in
# *options*.
@options = ScriptOptions.new
@args =
OptionParser.new do |parser|
@options.define_options(parser)
parser.parse!(args)
end
@options
end
attr_reader :parser, :options
end
example = Parser.new
options = example.parse(ARGV)
# pp options # example.options
# pp ARGV
sleep(options.delay) if options.delay
if options.keyword.nil?
print "Enter the keywords to search separators include: and, or, not: "
options.keyword = $stdin.gets.chomp
end
if options.location.nil?
print "Enter the suburb, city or region: "
options.location = $stdin.gets.chomp
end
if options.range.nil?
print "Listed time in days 999 (default) or 1, 3, 7, 14, 31 or any positive number: "
options.range = $stdin.gets.chomp
end
if options.worktype.nil?
print 'Work type
all (default)
full or 242 (full time)
part or 243 (part time)
contract or 244 (contract/temp)
casual or 245 (casual/vacation): '
options.worktype = $stdin.gets.chomp
end
agent = Mechanize.new
agent.user_agent_alias = "Windows Chrome"
site = "https://www.seek.com.au"
page =
agent.get(
"#{site}/jobs",
[
["keywords", options.keyword],
["where", options.location],
["range", options.range],
["worktype", options.worktype]
]
)
results = []
results <<
[
"Title",
"URL",
"Advertiser",
"Location",
"Area",
"Listing Date",
"Salary",
"Classification",
"Sub Classification",
"Work Type",
"Short Description"
]
loop do
# for each page # html = page.body
jobs = page.search("article")
jobs.each do |job|
title = job.xpath("@aria-label")
url =
site + job.xpath('descendant::a[@data-automation="jobTitle"]/@href').to_s
advertiser =
job.xpath('descendant::a[@data-automation="jobCompany"]/text()')
location = job.xpath('descendant::a[@data-automation="jobLocation"]/text()')
area = job.xpath('descendant::a[@data-automation="jobArea"]/text()')
listing_date =
job.xpath('descendant::span[@data-automation="jobListingDate"]/text()')
salary =
job.xpath('descendant::span[@data-automation="jobSalary"]/span/text()')
classification =
job.xpath('descendant::a[@data-automation="jobClassification"]/text()')
sub_classification =
job.xpath('descendant::a[@data-automation="jobSubClassification"]/text()')
short_description =
job.xpath(
'descendant::span[@data-automation="jobShortDescription"]//text()'
)
# get details from job ad page
ad = agent.get(url)
# at selects the first using CSS selectors
work_type = ad.at('dd[data-automation="job-detail-work-type"]').text
listing_date = ad.at('dd[data-automation="job-detail-date"]').text if listing_date.empty?
results <<
[
title,
url,
advertiser,
location,
area,
listing_date,
salary,
classification,
sub_classification,
work_type,
short_description
]
end
if (link = page.link_with(text: "Next")) # As long as there is still a next page link
page = link.click
else
# If no link left, then break out of loop
break
end
end
if results.size > 1
keyword = options.keyword.tr(" ", "-")
location = options.location.tr(" ", "-") unless options.location.empty?
range = "range-#{options.range}" unless options.range.empty?
options.worktype = enwtype(options.worktype)
worktype = "worktype-#{options.worktype}" unless options.worktype.empty?
filename = [keyword, location, range, worktype].compact.join("-").downcase
filename = filename[1..] if filename[0] == "-"
FileUtils.mkdir_p("jobs")
CSV.open("jobs/#{filename}.csv", "w+") do |csv_file|
results.each { |row| csv_file << row }
end
puts "#{results.size - 1} jobs found"
`open "jobs/#{filename}.csv"`
end