anemone 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE.txt +19 -0
- data/lib/anemone/anemone.rb +32 -9
- data/lib/anemone/core.rb +23 -9
- data/lib/anemone/http.rb +13 -6
- data/lib/anemone/page.rb +17 -7
- data/lib/anemone/page_hash.rb +14 -0
- data/lib/anemone/tentacle.rb +9 -3
- data/spec/anemone_spec.rb +40 -0
- data/spec/core_spec.rb +181 -0
- data/spec/fakeweb_helper.rb +58 -0
- data/spec/page_spec.rb +49 -0
- data/spec/spec_helper.rb +7 -0
- metadata +16 -12
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2009 Vertive, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
data/lib/anemone/anemone.rb
CHANGED
|
@@ -3,11 +3,8 @@ require 'anemone/core'
|
|
|
3
3
|
|
|
4
4
|
module Anemone
|
|
5
5
|
# Version number
|
|
6
|
-
VERSION = '0.
|
|
7
|
-
|
|
8
|
-
# User-Agent string used for HTTP requests
|
|
9
|
-
USER_AGENT = "Anemone/#{self::VERSION}"
|
|
10
|
-
|
|
6
|
+
VERSION = '0.2.0'
|
|
7
|
+
|
|
11
8
|
#module-wide options
|
|
12
9
|
def Anemone.options=(options)
|
|
13
10
|
@options = options
|
|
@@ -23,15 +20,41 @@ module Anemone
|
|
|
23
20
|
def Anemone.crawl(urls, options = {}, &block)
|
|
24
21
|
Anemone.options = OpenStruct.new(options)
|
|
25
22
|
|
|
26
|
-
#by default, run 4 Tentacle threads to fetch pages
|
|
23
|
+
# by default, run 4 Tentacle threads to fetch pages
|
|
27
24
|
Anemone.options.threads ||= 4
|
|
28
25
|
|
|
29
|
-
#disable verbose output by default
|
|
26
|
+
# disable verbose output by default
|
|
30
27
|
Anemone.options.verbose ||= false
|
|
31
28
|
|
|
32
|
-
#by default, don't throw away the page response body after scanning it for links
|
|
29
|
+
# by default, don't throw away the page response body after scanning it for links
|
|
33
30
|
Anemone.options.discard_page_bodies ||= false
|
|
34
|
-
|
|
31
|
+
|
|
32
|
+
# by default, identify self as Anemone/VERSION
|
|
33
|
+
Anemone.options.user_agent ||= "Anemone/#{self::VERSION}"
|
|
34
|
+
|
|
35
|
+
# no delay between requests by default
|
|
36
|
+
Anemone.options.delay ||= 0
|
|
37
|
+
|
|
38
|
+
# by default, don't obey the robots exclusion protocol
|
|
39
|
+
if Anemone.options.obey_robots_txt ||= false
|
|
40
|
+
begin
|
|
41
|
+
require 'robots'
|
|
42
|
+
rescue LoadError
|
|
43
|
+
warn "To support the robot exclusion protocol, install the robots gem:\n" \
|
|
44
|
+
"sudo gem sources -a http://gems.github.com\n" \
|
|
45
|
+
"sudo gem install fizx-robots"
|
|
46
|
+
exit
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# by default, don't limit the depth of the crawl
|
|
51
|
+
Anemone.options.depth_limit ||= :infinity
|
|
52
|
+
|
|
53
|
+
#use a single thread if a delay was requested
|
|
54
|
+
if(Anemone.options.delay != 0)
|
|
55
|
+
Anemone.options.threads = 1
|
|
56
|
+
end
|
|
57
|
+
|
|
35
58
|
Core.crawl(urls, &block)
|
|
36
59
|
end
|
|
37
60
|
end
|
data/lib/anemone/core.rb
CHANGED
|
@@ -23,6 +23,10 @@ module Anemone
|
|
|
23
23
|
@skip_link_patterns = []
|
|
24
24
|
@after_crawl_blocks = []
|
|
25
25
|
|
|
26
|
+
if Anemone.options.obey_robots_txt
|
|
27
|
+
@robots = Robots.new(Anemone.options.user_agent)
|
|
28
|
+
end
|
|
29
|
+
|
|
26
30
|
block.call(self) if block
|
|
27
31
|
end
|
|
28
32
|
|
|
@@ -113,18 +117,18 @@ module Anemone
|
|
|
113
117
|
|
|
114
118
|
puts "#{page.url} Queue: #{link_queue.size}" if Anemone.options.verbose
|
|
115
119
|
|
|
116
|
-
#perform the on_every_page blocks for this page
|
|
120
|
+
# perform the on_every_page blocks for this page
|
|
117
121
|
do_page_blocks(page)
|
|
118
122
|
|
|
119
123
|
page.doc = nil if Anemone.options.discard_page_bodies
|
|
120
124
|
|
|
121
125
|
links_to_follow(page).each do |link|
|
|
122
|
-
link_queue.enq(link)
|
|
126
|
+
link_queue.enq([link, page])
|
|
123
127
|
@pages[link] = nil
|
|
124
128
|
end
|
|
125
129
|
|
|
126
|
-
#create an entry in the page hash for each alias of this page,
|
|
127
|
-
#i.e. all the pages that redirected to this page
|
|
130
|
+
# create an entry in the page hash for each alias of this page,
|
|
131
|
+
# i.e. all the pages that redirected to this page
|
|
128
132
|
page.aliases.each do |aka|
|
|
129
133
|
if !@pages.has_key?(aka) or @pages[aka].nil?
|
|
130
134
|
@pages[aka] = page.alias_clone(aka)
|
|
@@ -184,16 +188,26 @@ module Anemone
|
|
|
184
188
|
#
|
|
185
189
|
def links_to_follow(page)
|
|
186
190
|
links = @focus_crawl_block ? @focus_crawl_block.call(page) : page.links
|
|
187
|
-
links.
|
|
191
|
+
links.select { |link| visit_link?(link, page) }
|
|
188
192
|
end
|
|
189
193
|
|
|
190
194
|
#
|
|
191
195
|
# Returns +true+ if *link* has not been visited already,
|
|
192
|
-
# and is not excluded by a skip_link pattern
|
|
193
|
-
#
|
|
196
|
+
# and is not excluded by a skip_link pattern...
|
|
197
|
+
# and is not excluded by robots.txt...
|
|
198
|
+
# and is not deeper than the depth limit
|
|
199
|
+
# Returns +false+ otherwise.
|
|
194
200
|
#
|
|
195
|
-
def visit_link?(link)
|
|
196
|
-
|
|
201
|
+
def visit_link?(link, from_page = nil)
|
|
202
|
+
allowed = Anemone.options.obey_robots_txt ? @robots.allowed?(link) : true
|
|
203
|
+
|
|
204
|
+
if from_page
|
|
205
|
+
too_deep = from_page.depth >= Anemone.options.depth_limit rescue false
|
|
206
|
+
else
|
|
207
|
+
too_deep = false
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
!@pages.has_key?(link) and !skip_link?(link) and allowed and !too_deep
|
|
197
211
|
end
|
|
198
212
|
|
|
199
213
|
#
|
data/lib/anemone/http.rb
CHANGED
|
@@ -9,8 +9,8 @@ module Anemone
|
|
|
9
9
|
# Retrieve an HTTP response for *url*, following redirects.
|
|
10
10
|
# Returns the response object, response code, and final URI location.
|
|
11
11
|
#
|
|
12
|
-
def self.get(url)
|
|
13
|
-
response = get_response(url)
|
|
12
|
+
def self.get(url, referer = nil)
|
|
13
|
+
response = get_response(url, referer)
|
|
14
14
|
code = Integer(response.code)
|
|
15
15
|
loc = url
|
|
16
16
|
|
|
@@ -18,7 +18,7 @@ module Anemone
|
|
|
18
18
|
while response.is_a?(Net::HTTPRedirection) and limit > 0
|
|
19
19
|
loc = URI(response['location'])
|
|
20
20
|
loc = url.merge(loc) if loc.relative?
|
|
21
|
-
response = get_response(loc)
|
|
21
|
+
response = get_response(loc, referer)
|
|
22
22
|
limit -= 1
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -28,10 +28,17 @@ module Anemone
|
|
|
28
28
|
#
|
|
29
29
|
# Get an HTTPResponse for *url*, sending the appropriate User-Agent string
|
|
30
30
|
#
|
|
31
|
-
def self.get_response(url)
|
|
31
|
+
def self.get_response(url, referer = nil)
|
|
32
|
+
full_path = url.query.nil? ? url.path : "#{url.path}?#{url.query}"
|
|
33
|
+
user_agent = Anemone.options.user_agent rescue nil
|
|
34
|
+
|
|
35
|
+
opts = {}
|
|
36
|
+
opts['User-Agent'] = user_agent if user_agent
|
|
37
|
+
opts['Referer'] = referer.to_s if referer
|
|
38
|
+
|
|
32
39
|
Net::HTTP.start(url.host, url.port) do |http|
|
|
33
|
-
return http.get(
|
|
40
|
+
return http.get(full_path, opts)
|
|
34
41
|
end
|
|
35
42
|
end
|
|
36
43
|
end
|
|
37
|
-
end
|
|
44
|
+
end
|
data/lib/anemone/page.rb
CHANGED
|
@@ -22,24 +22,32 @@ module Anemone
|
|
|
22
22
|
attr_accessor :aliases
|
|
23
23
|
# Boolean indicating whether or not this page has been visited in PageHash#shortest_paths!
|
|
24
24
|
attr_accessor :visited
|
|
25
|
-
#
|
|
25
|
+
# Depth of this page from the root of the crawl. This is not necessarily the
|
|
26
|
+
# shortest path; use PageHash#shortest_paths! to find that value.
|
|
26
27
|
attr_accessor :depth
|
|
28
|
+
# URL of the page that brought us to this page
|
|
29
|
+
attr_accessor :referer
|
|
27
30
|
|
|
28
31
|
#
|
|
29
32
|
# Create a new Page from the response of an HTTP request to *url*
|
|
30
33
|
#
|
|
31
|
-
def self.fetch(url)
|
|
34
|
+
def self.fetch(url, from_page = nil)
|
|
32
35
|
begin
|
|
33
|
-
url = URI(url)
|
|
36
|
+
url = URI(url) unless url.is_a?(URI)
|
|
34
37
|
|
|
35
|
-
|
|
38
|
+
if from_page
|
|
39
|
+
referer = from_page.url
|
|
40
|
+
depth = from_page.depth + 1
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
response, code, location = Anemone::HTTP.get(url, referer)
|
|
36
44
|
|
|
37
45
|
aka = nil
|
|
38
46
|
if !url.eql?(location)
|
|
39
47
|
aka = location
|
|
40
48
|
end
|
|
41
49
|
|
|
42
|
-
return Page.new(url, response.body, code, response.to_hash, aka)
|
|
50
|
+
return Page.new(url, response.body, code, response.to_hash, aka, referer, depth)
|
|
43
51
|
rescue
|
|
44
52
|
return Page.new(url)
|
|
45
53
|
end
|
|
@@ -48,14 +56,16 @@ module Anemone
|
|
|
48
56
|
#
|
|
49
57
|
# Create a new page
|
|
50
58
|
#
|
|
51
|
-
def initialize(url, body = nil, code = nil, headers = nil, aka = nil)
|
|
59
|
+
def initialize(url, body = nil, code = nil, headers = nil, aka = nil, referer = nil, depth = 0)
|
|
52
60
|
@url = url
|
|
53
61
|
@code = code
|
|
54
62
|
@headers = headers
|
|
55
63
|
@links = []
|
|
56
64
|
@aliases = []
|
|
57
65
|
@data = OpenStruct.new
|
|
58
|
-
|
|
66
|
+
@referer = referer
|
|
67
|
+
@depth = depth || 0
|
|
68
|
+
|
|
59
69
|
@aliases << aka if !aka.nil?
|
|
60
70
|
|
|
61
71
|
if body
|
data/lib/anemone/page_hash.rb
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
module Anemone
|
|
2
2
|
class PageHash < Hash
|
|
3
3
|
|
|
4
|
+
# We typically index the hash with a URI,
|
|
5
|
+
# but convert it to a String for easier retrieval
|
|
6
|
+
def [](index)
|
|
7
|
+
super(index.to_s)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def []=(index, other)
|
|
11
|
+
super(index.to_s, other)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def has_key?(key)
|
|
15
|
+
super(key.to_s)
|
|
16
|
+
end
|
|
17
|
+
|
|
4
18
|
#
|
|
5
19
|
# Use a breadth-first search to calculate the single-source
|
|
6
20
|
# shortest paths from *root* to all pages in the PageHash
|
data/lib/anemone/tentacle.rb
CHANGED
|
@@ -17,13 +17,19 @@ module Anemone
|
|
|
17
17
|
#
|
|
18
18
|
def run
|
|
19
19
|
while true do
|
|
20
|
-
link = @link_queue.deq
|
|
20
|
+
link, from_page = @link_queue.deq
|
|
21
21
|
|
|
22
22
|
break if link == :END
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
|
|
24
|
+
if from_page
|
|
25
|
+
page = Page.fetch(link, from_page)
|
|
26
|
+
else
|
|
27
|
+
page = Page.fetch(link)
|
|
28
|
+
end
|
|
25
29
|
|
|
26
30
|
@page_queue.enq(page)
|
|
31
|
+
|
|
32
|
+
sleep Anemone.options.delay
|
|
27
33
|
end
|
|
28
34
|
end
|
|
29
35
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Anemone do
|
|
4
|
+
|
|
5
|
+
it "should have a version" do
|
|
6
|
+
Anemone.const_defined?('VERSION').should == true
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should have options" do
|
|
10
|
+
Anemone.should respond_to(:options)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should accept options for the crawl" do
|
|
14
|
+
Anemone.crawl(SPEC_DOMAIN, :verbose => false,
|
|
15
|
+
:threads => 2,
|
|
16
|
+
:discard_page_bodies => true,
|
|
17
|
+
:user_agent => 'test',
|
|
18
|
+
:obey_robots_txt => true,
|
|
19
|
+
:depth_limit => 3)
|
|
20
|
+
Anemone.options.verbose.should == false
|
|
21
|
+
Anemone.options.threads.should == 2
|
|
22
|
+
Anemone.options.discard_page_bodies.should == true
|
|
23
|
+
Anemone.options.delay.should == 0
|
|
24
|
+
Anemone.options.user_agent.should == 'test'
|
|
25
|
+
Anemone.options.obey_robots_txt.should == true
|
|
26
|
+
Anemone.options.depth_limit.should == 3
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "should use 1 thread if a delay is requested" do
|
|
30
|
+
Anemone.crawl(SPEC_DOMAIN, :delay => 0.01, :threads => 2)
|
|
31
|
+
Anemone.options.threads.should == 1
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "should return a Anemone::Core from the crawl, which has a PageHash" do
|
|
35
|
+
result = Anemone.crawl(SPEC_DOMAIN)
|
|
36
|
+
result.should be_an_instance_of(Anemone::Core)
|
|
37
|
+
result.pages.should be_an_instance_of(Anemone::PageHash)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
data/spec/core_spec.rb
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
module Anemone
|
|
4
|
+
describe Core do
|
|
5
|
+
|
|
6
|
+
before(:each) do
|
|
7
|
+
FakeWeb.clean_registry
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "should crawl all the html pages in a domain by following <a> href's" do
|
|
11
|
+
pages = []
|
|
12
|
+
pages << FakePage.new('0', :links => ['1', '2'])
|
|
13
|
+
pages << FakePage.new('1', :links => ['3'])
|
|
14
|
+
pages << FakePage.new('2')
|
|
15
|
+
pages << FakePage.new('3')
|
|
16
|
+
|
|
17
|
+
Anemone.crawl(pages[0].url).should have(4).pages
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should not leave the original domain" do
|
|
21
|
+
pages = []
|
|
22
|
+
pages << FakePage.new('0', :links => ['1'], :hrefs => 'http://www.other.com/')
|
|
23
|
+
pages << FakePage.new('1')
|
|
24
|
+
|
|
25
|
+
core = Anemone.crawl(pages[0].url)
|
|
26
|
+
|
|
27
|
+
core.should have(2).pages
|
|
28
|
+
core.pages.keys.should_not include('http://www.other.com/')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should follow http redirects" do
|
|
32
|
+
pages = []
|
|
33
|
+
pages << FakePage.new('0', :links => ['1'])
|
|
34
|
+
pages << FakePage.new('1', :redirect => '2')
|
|
35
|
+
pages << FakePage.new('2')
|
|
36
|
+
|
|
37
|
+
Anemone.crawl(pages[0].url).should have(3).pages
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should accept multiple starting URLs" do
|
|
41
|
+
pages = []
|
|
42
|
+
pages << FakePage.new('0', :links => ['1'])
|
|
43
|
+
pages << FakePage.new('1')
|
|
44
|
+
pages << FakePage.new('2', :links => ['3'])
|
|
45
|
+
pages << FakePage.new('3')
|
|
46
|
+
|
|
47
|
+
Anemone.crawl([pages[0].url, pages[2].url]).should have(4).pages
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "should include the query string when following links" do
|
|
51
|
+
pages = []
|
|
52
|
+
pages << FakePage.new('0', :links => ['1?foo=1'])
|
|
53
|
+
pages << FakePage.new('1?foo=1')
|
|
54
|
+
pages << FakePage.new('1')
|
|
55
|
+
|
|
56
|
+
core = Anemone.crawl(pages[0].url)
|
|
57
|
+
|
|
58
|
+
core.should have(2).pages
|
|
59
|
+
core.pages.keys.should_not include(pages[2].url)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "should be able to skip links based on a RegEx" do
|
|
63
|
+
pages = []
|
|
64
|
+
pages << FakePage.new('0', :links => ['1', '2'])
|
|
65
|
+
pages << FakePage.new('1')
|
|
66
|
+
pages << FakePage.new('2')
|
|
67
|
+
|
|
68
|
+
core = Anemone.crawl(pages[0].url) do |a|
|
|
69
|
+
a.skip_links_like /1/
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
core.should have(2).pages
|
|
73
|
+
core.pages.keys.should_not include(pages[1].url)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "should be able to call a block on every page" do
|
|
77
|
+
pages = []
|
|
78
|
+
pages << FakePage.new('0', :links => ['1', '2'])
|
|
79
|
+
pages << FakePage.new('1')
|
|
80
|
+
pages << FakePage.new('2')
|
|
81
|
+
|
|
82
|
+
count = 0
|
|
83
|
+
Anemone.crawl(pages[0].url) do |a|
|
|
84
|
+
a.on_every_page { count += 1 }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
count.should == 3
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "should not discard page bodies by default" do
|
|
91
|
+
Anemone.crawl(FakePage.new('0').url).pages.values.first.doc.should_not be_nil
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "should optionally discard page bodies to conserve memory" do
|
|
95
|
+
core = Anemone.crawl(FakePage.new('0').url, :discard_page_bodies => true)
|
|
96
|
+
core.pages.values.first.doc.should be_nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "should provide a focus_crawl method to select the links on each page to follow" do
|
|
100
|
+
pages = []
|
|
101
|
+
pages << FakePage.new('0', :links => ['1', '2'])
|
|
102
|
+
pages << FakePage.new('1')
|
|
103
|
+
pages << FakePage.new('2')
|
|
104
|
+
|
|
105
|
+
core = Anemone.crawl(pages[0].url) do |a|
|
|
106
|
+
a.focus_crawl {|p| p.links.reject{|l| l.to_s =~ /1/}}
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
core.should have(2).pages
|
|
110
|
+
core.pages.keys.should_not include(pages[1].url)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "should optionally delay between page requests" do
|
|
114
|
+
delay = 0.25
|
|
115
|
+
|
|
116
|
+
pages = []
|
|
117
|
+
pages << FakePage.new('0', :links => '1')
|
|
118
|
+
pages << FakePage.new('1')
|
|
119
|
+
|
|
120
|
+
start = Time.now
|
|
121
|
+
Anemone.crawl(pages[0].url, :delay => delay)
|
|
122
|
+
finish = Time.now
|
|
123
|
+
|
|
124
|
+
(finish - start).should satisfy {|t| t > delay * 2}
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it "should optionally obey the robots exclusion protocol" do
|
|
128
|
+
pages = []
|
|
129
|
+
pages << FakePage.new('0', :links => '1')
|
|
130
|
+
pages << FakePage.new('1')
|
|
131
|
+
pages << FakePage.new('robots.txt',
|
|
132
|
+
:body => "User-agent: *\nDisallow: /1",
|
|
133
|
+
:content_type => 'text/plain')
|
|
134
|
+
|
|
135
|
+
core = Anemone.crawl(pages[0].url, :obey_robots_txt => true)
|
|
136
|
+
urls = core.pages.keys
|
|
137
|
+
|
|
138
|
+
urls.should include(pages[0].url)
|
|
139
|
+
urls.should_not include(pages[1].url)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it "should track the page depth and referer" do
|
|
143
|
+
num_pages = 5
|
|
144
|
+
|
|
145
|
+
pages = []
|
|
146
|
+
|
|
147
|
+
num_pages.times do |n|
|
|
148
|
+
# register this page with a link to the next page
|
|
149
|
+
link = (n + 1).to_s if n + 1 < num_pages
|
|
150
|
+
pages << FakePage.new(n.to_s, :links => [link].compact)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
core = Anemone.crawl(pages[0].url)
|
|
154
|
+
|
|
155
|
+
num_pages.times do |n|
|
|
156
|
+
page = core.pages[pages[n].url]
|
|
157
|
+
page.depth.should == n
|
|
158
|
+
page.referer.should == core.pages[pages[n-1].url].url if n > 0
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
core.pages[pages[0].url].referer.should == nil
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it "should optionally limit the depth of the crawl" do
|
|
165
|
+
num_pages = 5
|
|
166
|
+
|
|
167
|
+
pages = []
|
|
168
|
+
|
|
169
|
+
num_pages.times do |n|
|
|
170
|
+
# register this page with a link to the next page
|
|
171
|
+
link = (n + 1).to_s if n + 1 < num_pages
|
|
172
|
+
pages << FakePage.new(n.to_s, :links => [link].compact)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
core = Anemone.crawl(pages[0].url, :depth_limit => 3)
|
|
176
|
+
|
|
177
|
+
core.should have(4).pages
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
end
|
|
181
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'fakeweb'
|
|
3
|
+
rescue LoadError
|
|
4
|
+
warn "You need the 'fakeweb' gem installed to test Anemone"
|
|
5
|
+
exit
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
FakeWeb.allow_net_connect = false
|
|
9
|
+
|
|
10
|
+
module Anemone
|
|
11
|
+
SPEC_DOMAIN = "http://www.example.com/"
|
|
12
|
+
|
|
13
|
+
class FakePage
|
|
14
|
+
attr_accessor :links
|
|
15
|
+
attr_accessor :hrefs
|
|
16
|
+
attr_accessor :body
|
|
17
|
+
|
|
18
|
+
def initialize(name = '', options = {})
|
|
19
|
+
@name = name
|
|
20
|
+
@links = [options[:links]].flatten if options.has_key?(:links)
|
|
21
|
+
@hrefs = [options[:hrefs]].flatten if options.has_key?(:hrefs)
|
|
22
|
+
@redirect = options[:redirect] if options.has_key?(:redirect)
|
|
23
|
+
@content_type = options[:content_type] || "text/html"
|
|
24
|
+
@body = options[:body]
|
|
25
|
+
|
|
26
|
+
create_body unless @body
|
|
27
|
+
add_to_fakeweb
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def url
|
|
31
|
+
SPEC_DOMAIN + @name
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def create_body
|
|
37
|
+
@body = "<html><body>"
|
|
38
|
+
@links.each{|l| @body += "<a href=\"#{SPEC_DOMAIN}#{l}\"></a>"} if @links
|
|
39
|
+
@hrefs.each{|h| @body += "<a href=\"#{h}\"></a>"} if @hrefs
|
|
40
|
+
@body += "</body></html>"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def add_to_fakeweb
|
|
44
|
+
options = {:body => @body, :content_type => @content_type, :status => [200, "OK"]}
|
|
45
|
+
|
|
46
|
+
if @redirect
|
|
47
|
+
options[:status] = [301, "Permanently Moved"]
|
|
48
|
+
options[:location] = SPEC_DOMAIN + @redirect
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
FakeWeb.register_uri(:get, SPEC_DOMAIN + @name, options)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
#default root
|
|
57
|
+
Anemone::FakePage.new
|
|
58
|
+
|
data/spec/page_spec.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
module Anemone
|
|
4
|
+
describe Page do
|
|
5
|
+
|
|
6
|
+
before(:each) do
|
|
7
|
+
@page = Page.fetch(FakePage.new('home').url)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "should be able to fetch a page" do
|
|
11
|
+
@page.should_not be_nil
|
|
12
|
+
@page.url.to_s.should include('home')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "should store the response headers when fetching a page" do
|
|
16
|
+
@page.headers.should_not be_nil
|
|
17
|
+
@page.headers.should have_key('content-type')
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should have an OpenStruct attribute for the developer to store data in" do
|
|
21
|
+
@page.data.should_not be_nil
|
|
22
|
+
@page.data.should be_an_instance_of(OpenStruct)
|
|
23
|
+
|
|
24
|
+
@page.data.test = 'test'
|
|
25
|
+
@page.data.test.should == 'test'
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "should have a Nokogori::HTML::Document attribute for the page body" do
|
|
29
|
+
@page.doc.should_not be_nil
|
|
30
|
+
@page.doc.should be_an_instance_of(Nokogiri::HTML::Document)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "should indicate whether it was fetched after an HTTP redirect" do
|
|
34
|
+
@page.should respond_to(:redirect?)
|
|
35
|
+
|
|
36
|
+
@page.redirect?.should == false
|
|
37
|
+
|
|
38
|
+
Page.fetch(FakePage.new('redir', :redirect => 'home').url).redirect?.should == true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "should have a method to tell if a URI is in the same domain as the page" do
|
|
42
|
+
@page.should respond_to(:in_domain?)
|
|
43
|
+
|
|
44
|
+
@page.in_domain?(URI(FakePage.new('test').url)).should == true
|
|
45
|
+
@page.in_domain?(URI('http://www.other.com/')).should == false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: anemone
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chris Kite
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-07
|
|
12
|
+
date: 2009-09-07 00:00:00 -05:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
@@ -35,20 +35,20 @@ extensions: []
|
|
|
35
35
|
extra_rdoc_files:
|
|
36
36
|
- README.rdoc
|
|
37
37
|
files:
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
- bin/anemone_pagedepth.rb
|
|
38
|
+
- LICENSE.txt
|
|
39
|
+
- README.rdoc
|
|
41
40
|
- bin/anemone_count.rb
|
|
42
41
|
- bin/anemone_cron.rb
|
|
42
|
+
- bin/anemone_pagedepth.rb
|
|
43
|
+
- bin/anemone_serialize.rb
|
|
44
|
+
- bin/anemone_url_list.rb
|
|
43
45
|
- lib/anemone.rb
|
|
44
|
-
- lib/anemone
|
|
45
|
-
- lib/anemone/page.rb
|
|
46
|
-
- lib/anemone/core.rb
|
|
47
46
|
- lib/anemone/anemone.rb
|
|
47
|
+
- lib/anemone/core.rb
|
|
48
48
|
- lib/anemone/http.rb
|
|
49
|
-
- lib/anemone/
|
|
49
|
+
- lib/anemone/page.rb
|
|
50
50
|
- lib/anemone/page_hash.rb
|
|
51
|
-
-
|
|
51
|
+
- lib/anemone/tentacle.rb
|
|
52
52
|
has_rdoc: true
|
|
53
53
|
homepage: http://anemone.rubyforge.org
|
|
54
54
|
post_install_message:
|
|
@@ -78,5 +78,9 @@ rubygems_version: 1.3.1
|
|
|
78
78
|
signing_key:
|
|
79
79
|
specification_version: 2
|
|
80
80
|
summary: Anemone web-spider framework
|
|
81
|
-
test_files:
|
|
82
|
-
|
|
81
|
+
test_files:
|
|
82
|
+
- spec/anemone_spec.rb
|
|
83
|
+
- spec/core_spec.rb
|
|
84
|
+
- spec/page_spec.rb
|
|
85
|
+
- spec/fakeweb_helper.rb
|
|
86
|
+
- spec/spec_helper.rb
|