-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToDo.txt
245 lines (194 loc) · 5.42 KB
/
ToDo.txt
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
!!! Needs a Restructuring (create framework)
- ! improve the caching
- Optional Requires (ex. watir-webdriver for some)
- proxy support
- recursive-autodiscovery, proxy-switching, and other 'patterns'
Some of my more useful Code to Integrate:
-----------------------------------------------------------------
require 'nokogiri'
require 'open-uri'
require 'open_uri_redirections' # allow https redirects
require 'uri'
require 'json'
require 'ruby-progressbar'
require 'thread'
def getNokogiriFromURL(url)
tries = 0
begin
return Nokogiri::HTML(open(url))
rescue => e
tries += 1
retry unless tries > 5
puts "Failed Open: "+url if url != nil
puts e
return Nokogiri::HTML("") #make rest fail silently...
end
end
def downcaseNokogiri(nokodoc)
nokodoc = Nokogiri::HTML(nokodoc.to_s.downcase) #meh...
end
def makeAbsolute(base,relative)
URI.join(URI.escape(base.to_s),URI.escape(relative.to_s)).to_s #foreign characters / objects...
end
def makeLinksAbsolute(base, relatives)
result = relatives.map {|r| makeAbsolute(base,r) }
result
end
def flaCompUniqArray!(array)
array.flatten!
array.compact!
array.uniq!
end
def extractEmailsFromURL(url)
doc = getNokogiriFromURL(url)
result = []
# mailto: links
result << doc.xpath('//a[starts-with(@href, \'mailto:\')]/@href')
flaCompUniqArray!(result)
result.map!{|r| r.to_s.gsub('mailto:','')}
# plaintext
r = Regexp.new(/\b([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})\b/)
c = doc.to_s
c.encode!('UTF-8', 'UTF-8', :invalid => :replace) # .scan(r) whines about 'invalid byte sequence in UTF-8'
result << c.scan(r)
flaCompUniqArray!(result)
# -
# TODO sanitize, ex. remove ?subject...
return result
end
def extractEmailsFromWebsite(url)
result = []
checkSites = []
# try contact/about pages
doc = getNokogiriFromURL(url)
# Upper Case?
keywords = ['contact','about','email']
keywords.each do |k|
cases = [k,k.capitalize,k.upcase] # no better way?
cases.each do |c|
checkSites << doc.xpath('//a[contains(text(), \''+c+'\')]/@href').to_a
checkSites << doc.xpath('//a[contains(@href, \''+c+'\')]/@href').to_a
end
end
flaCompUniqArray!(checkSites)
checkSites.map!{ |e| makeAbsolute(url, e) }
checkSites << url
checkSites.each {|u| result << extractEmailsFromURL(u)}
flaCompUniqArray!(result)
return result
end
def getLinkByName(name, url)
#ask for doc instead? faster... or implement caching in getURL()
doc = getNokogiriFromURL(url)
result = doc.xpath('//a[text() = \''+name+'\']/@href').to_a.first
end
def getLinksByName(names, url)
names.map {|n| getLinkByName(n)}
end
def getRedirectResult(url)
tries = 0
begin
page = open(url, :allow_redirections => :all) # 'open_uri_redirections'
return page.base_uri.to_s
rescue => e
tries += 1
retry unless tries > 5
puts "Failed Redirect: "+url
puts e
return nil
end
end
# - IO (only designed for hashes/arrays)
def saveJSON(path,object)
File.open(path,"w") do |f|
f << object.to_json
end
end
def loadJSON(path)
JSON.load(File.open(path,"r"))
end
# Abstraction...
def getLinksByXpath(xpath,url)
# xpath should end with /@href
doc = getNokogiriFromURL(url)
result = doc.xpath(xpath).to_a
flaCompUniqArray!(result)
makeLinksAbsolute(url,result)
end
def scrapeSitesForXpath(xpath,url_array,show_progress)
result = []
bar = ProgressBar.create(:total => url_array.count) if(show_progress)
url_array.each do |l|
result << getLinksByXpath(xpath,l)
bar.increment if(show_progress)
end
flaCompUniqArray!(result)
bar.finish if(show_progress)
result
end
# class Task
# def new(information,procedure)
# info = information
# proc = procedure
# end
# def do
# proc.call(info)
# end
# end
# Some code from a multithreading script of mine
# Abstract Therapists into Task
# Abstract Progress Bar Away? Optional?
def performTasks()
# multithreading
thread_count = 100
semaphore = Mutex.new
running = true
threads = []
# load tasks - progress
todo_therapists = loadJSON('therapists.json')
done_therapists = []
done_therapists = loadJSON('therapists_done.json') if File.file?('therapists_done.json')
puts todo_therapists.count.to_s+' Total'
todo_therapists = todo_therapists - done_therapists
puts done_therapists.count.to_s+' Done'
puts todo_therapists.count.to_s+' To Go'
bar = ProgressBar.create(:title => ":: ", :format => '&_ %c/%C [%b>>%i] %P%% :%e', :smoothing=>0.5, :starting_at => done_therapists.count, :total => done_therapists.count+todo_therapists.count)
# load current info
therapists = []
therapists = loadJSON('therapists_info.json') if File.file?('therapists_info.json')
# start thread monster
thread_count.times do
threads << Thread.new {
while(running & !todo_therapists.empty?) do
# pick a task
url = ""
semaphore.synchronize {
url = todo_therapists.pop
}
# do magic
info = getTherapistInfo(url)
# sync/report to rest
semaphore.synchronize {
done_therapists << url
therapists << info
saveJSON('therapists_done.json',done_therapists)
saveJSON('therapists_info.json',therapists)
bar.increment
}
end
}
end
## Original Linear Code
#todo_therapists.each do |url|
# therapists << getTherapistInfo(url)
# done_therapists << url
# saveJSON('therapists_done.json',done_therapists)
# saveJSON('therapists_info.json',therapists)
# #puts '-> '+done_therapists.count.to_s+'/'+todo_therapists.count.to_s
# bar.increment
#end
# wait for threads
threads.each {|t| t.join}
# finish =)
bar.finish
end