-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages.rb
117 lines (94 loc) · 2.4 KB
/
pages.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
require 'date'
require 'mechanize'
class Page
class << self
attr_accessor :offer_link_selector, :offer_content_selector, :next_page_text
end
def initialize page
@page = page
end
def offer_links
page.links_with(css: self.class.offer_link_selector)
end
def find_content offer_link
print '.'
offer_link.click.search(self.class.offer_content_selector).first
rescue StandardError => error
puts error.message
nil
end
def offers
offer_links.map { |offer_link| find_content offer_link }.compact
end
def next
next_page = page.link_with(text: next_page_text)&.click
next_page ? self.class.new(next_page) : nil
end
def next_page_text
self.class.next_page_text
end
protected
attr_reader :page
end
class MultitrabajosPage < Page
@offer_link_selector = 'a.aviso_box'.freeze
@offer_content_selector = '#contenido_aviso'.freeze
@next_page_text = 'Siguiente'.freeze
end
class ComputrabajoPage < Page
@offer_link_selector = 'a.js-o-link'.freeze
@offer_content_selector = '.detalle_oferta'.freeze
@next_page_text = 'Siguiente'.freeze
end
class UnmejorempleoPage < Page
@offer_link_selector = '.item-normal a'.freeze
@offer_content_selector = 'article'.freeze
@index = 0
class << self
attr_accessor :index
end
def next_page_text
(@index + 1).to_s
end
def initialize page
super
@index = self.class.index += 1
end
end
class Pages
include Enumerable
attr_reader :offers, :name
def initial_page url, page_class
page_class.new(Mechanize.new.get(url))
end
def initialize name, page_class, url
@current_page = initial_page url, page_class
@offers = reduce([]) { |a, e| a.concat e.offers }
@name = name
end
def each
while @current_page
yield @current_page
@current_page = @current_page.next
end
end
def save_offers db
offers.each do |offer|
db.execute "INSERT INTO offers values (?, ?, date('now'));",
[offer.serialize, name]
end
puts "\n#{offers.size} offers have been successfully saved."
end
end
class UnmejorempleoPages < Pages
IT_CATEGORY_ID = 1
def initial_page url, page_class
page_class.new it_category_page url
end
def it_category_page url
agent = Mechanize.new
search_form = agent.get(url).form_with id: 'form1'
search_form.categoria_id = IT_CATEGORY_ID
agent.submit search_form, search_form.buttons.first
end
end