-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
executable file
·165 lines (147 loc) · 3.36 KB
/
main.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
#!/usr/bin/env ruby
require 'base64'
require 'net/http'
require 'uri'
def strcmp(haystack, start, needle)
return false if haystack.length < start + needle.length
for i in 0...needle.length
return false if haystack[start+i].chr.downcase != needle[i].chr.downcase
end
true
end
def links(html)
links = []
start = 0
searching = false
for i in 0...html.length
if strcmp(html, i, "<a")
searching = true
start = i
end
if searching && html[i].chr == '>'
searching = false
links << html[start..i]
end
end
links
end
def href(link)
start = 0
searching = false
for i in 0...link.length
if strcmp(link, i, "href=\"")
searching = true
start = i + 6
end
if searching && i > start && link[i].chr == '"'
searching = false
return link[start..i-1]
end
end
""
end
$robots = [ 'comment' ]
$visited = {}
def crawl(url, depth)
return if $visited.key?(url)
# Throttling
sleep 1
filename = Base64.urlsafe_encode64(url.to_s)
if depth > 0 && File.exist?("#{filename}.html")
puts "Already retrieved: #{url}"
return
end
res = Net::HTTP.get_response(url, {'User-Agent' => 'Mozilla/5.0 (compatible; PotatoCastlesBot; +http://potatocastles.com)'} )
if res.is_a?(Net::HTTPRedirection)
path = res['Location']
puts "Redirected to: #{path}"
if !path.ascii_only?
puts "Skipping... URL Contains UTF-8: #{path}"
return
end
begin
nextUrl = URI.join(url, URI(path))
if url.host != nextUrl.host
puts "Skipping... External URL #{nextUrl}"
return
end
skip = false
$robots.each do |rule|
if nextUrl.to_s.include? rule
puts "Skipping... Disallowed by robots.txt rule '#{rule}'"
skip = true
break
end
end
return if skip
nextUrl.fragment = nil
nextUrl.normalize!
crawl(nextUrl, depth)
rescue URI::InvalidURIError
puts "Invalid URL: #{path}"
end
return
end
if !res.is_a?(Net::HTTPSuccess)
puts "Failed on request for resource: #{url}"
return
end
$visited[url] = true
puts "Retrieved: #{url}"
File.open("#{filename}.html", 'w') do |file|
file.write(res.body)
end if depth > 0
lnks = links(res.body)
lnks.each do |lnk|
path = href(lnk)
if !path.ascii_only?
puts "Skipping... URL Contains UTF-8: #{path}"
next
end
begin
nextUrl = URI.join(url, URI(path))
if url.host != nextUrl.host
puts "Skipping... External URL #{nextUrl}"
next
end
skip = false
$robots.each do |rule|
if nextUrl.to_s.include? rule
puts "Skipping... Disallowed by robots.txt rule '#{rule}'"
skip = true
break
end
end
next if skip
nextUrl.fragment = nil
nextUrl.normalize!
crawl(nextUrl, depth + 1)
rescue URI::InvalidURIError
puts "Invalid URL: #{path}"
end
end
end
def robots(url)
url = URI.join(url, URI('/robots.txt'))
res = Net::HTTP.get_response(url, {'User-Agent' => 'Mozilla/5.0 (compatible; PotatoCastlesBot; +http://potatocastles.com)'} )
if !res.is_a?(Net::HTTPSuccess)
puts "Failed requesting robots.txt"
return
end
$robots.concat res.body.lines.grep(/^Disallow:/).map { |rule| rule.delete_prefix('Disallow:').strip.tr('*', '') }
end
$usage = <<-END
Usage:
#{$0} [url]
END
if __FILE__ == $0
abort $usage if $*[0].nil?
url = URI($*[0])
abort "ERROR: Missing scheme. Try http://#{url}" if url.scheme.nil?
url.normalize!
robots(url)
puts '## robots.txt ##'
puts $robots
puts '## Crawling ##'
crawl(url, 0)
end