-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstep_entry_point.rb
executable file
·152 lines (141 loc) · 4.35 KB
/
step_entry_point.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
require 'watir-webdriver'
require 'set'
require 'json'
require 'logger'
require_relative 'linkedin_page'
require_relative 'records'
# initialize
config_reader = JSON.parse(File.read(ARGV[0]))
browser = Watir::Browser.new :chrome
current_dir = File.expand_path(File.dirname(__FILE__))
file = File.open(ARGV[1], File::WRONLY | File::APPEND | File::CREAT)
# To create new (and to remove old) logfile, add File::CREAT like:
# file = File.open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
logger = Logger.new(file)
linkedin_page = LinkedinPage.new(browser, logger)
records = Records.new(config_reader)
DELIMITER = ','
# open linkedin
linkedin_page.go_to_login_page config_reader['server_url']
# login account
linkedin_page.login_page_username.set config_reader["login"]["username"]
linkedin_page.login_page_password.set config_reader["login"]["password"]
linkedin_page.login_page_submit.click
linkedin_page.index_page.wait_until_present
# Set search keyword
linkedin_page.search_box.set config_reader["search"]["keywords"]
linkedin_page.search_button.click
linkedin_page.search_result_page.wait_until_present
# Set filters
filters = config_reader["search"]["filters"]
logger.info("started setting up filters")
filters.each do |key, value|
case key
when 'type'
linkedin_page.search_type_selector(value).click unless value.nil?
Watir::Wait.while {linkedin_page.search_loading}
logger.info("set type as #{value}")
when 'relationship_to_uncheck'
value.split(DELIMITER).each do |val|
linkedin_page.search_relationship_selector(val).clear
logger.info("unchecked relationship #{val}")
Watir::Wait.while {linkedin_page.search_loading}
end
when 'relationship_to_check'
value.split(DELIMITER).each do |val|
linkedin_page.search_relationship_selector(val).set
logger.info("checked relationship #{val}")
Watir::Wait.while {linkedin_page.search_loading}
end
when 'location'
value.split(DELIMITER).each do |val|
linkedin_page.search_locations(val).set
logger.info("set location as #{val}")
Watir::Wait.while {linkedin_page.search_loading}
end
else
logger.warn("Some of your filters are ignored")
end
end
# start sending invitation
index = 0
page = 1
logger.info("started sending invitation")
while true
begin
results = linkedin_page.search_results(index)
if !results.nil?
results.each_with_index do |li, i|
begin
index = index + 1
if !((li.class_name.include? "people") && (linkedin_page.name_exist? li) && (linkedin_page.description_exist? li))
next;
end
name = linkedin_page.name_of_person(li)
description = linkedin_page.description_of_person(li)
if records.exists?(name, description) then
logger.info("We already sent invitiation to #{name} before.")
next
else
records.add(name, description)
end
if !linkedin_page.blue_button_exists?(li) then
logger.debug("can not find blue button for #{name}")
next
else
blue_button = linkedin_page.blue_button(li)
end
if blue_button.text == 'Message'
logger.info("Already Connected with #{name}")
elsif blue_button.text == 'Follow'
blue_button.click
logger.info("Now following #{name}")
elsif blue_button.text == 'Connect'
blue_button.when_present.click
sleep 2
if linkedin_page.need_email_page?
browser.back
logger.info("Need email address to connect with #{name}")
linkedin_page.search_result_page.wait_until_present
break
else
logger.info("Invitation has been sent to #{name}")
end
end
rescue
logger.error("Had exception during #{li.to_s}")
next;
end
end
# navigate to next page
if index > (results.length - 1)
puts "Page #{page}, index: #{index}, length: #{results.length}"
if linkedin_page.next_page.exists? then
linkedin_page.next_page.click
page = page + 1
index = 0
logger.info("next page loading...")
sleep 2
Watir::Wait.while {linkedin_page.search_loading}
else
break
end
end
end
rescue
logger.error("Exception during fetching results in this page")
if linkedin_page.next_page.exists? then
linkedin_page.next_page.click
page = page + 1
index = 0
logger.info("next page loading...")
sleep 2
Watir::Wait.while {linkedin_page.search_loading}
else
break
end
end
end
browser.close
records.file_close
logger.close