Class: SpiderMech

Inherits:
Object
  • Object
show all
Defined in:
lib/spidermech.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_page) ⇒ SpiderMech

Returns a new instance of SpiderMech.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spidermech.rb', line 10

def initialize(start_page)
  @logger = Logger.new 'spidermech.log'
  @start_page = start_page

  @queue   = []
  @crawled = []
  @data    = []

  @queue << @start_page

  @bot = Mechanize.new
end

Instance Attribute Details

#crawledObject (readonly)

Returns the value of attribute crawled.



7
8
9
# File 'lib/spidermech.rb', line 7

def crawled
  @crawled
end

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/spidermech.rb', line 8

def data
  @data
end

#queueObject (readonly)

Returns the value of attribute queue.



6
7
8
# File 'lib/spidermech.rb', line 6

def queue
  @queue
end

Instance Method Details

#crawlObject



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
# File 'lib/spidermech.rb', line 52

def crawl
  url = @queue.shift

  if @crawled.include? url
    # @logger.warn "Already crawled #{url}"
    return
  else
    @logger.info "Crawling #{url}"
    @logger.info "Left in Queue: #{left_in_queue}"
  end

  page = @bot.get url

  if page.class != Mechanize::Page
    @logger.info "File crawling is not supported."
    return
  end

  @crawled << url

  # get all the assets
  data = {
    :url => url,
    :assets => {
      :scripts => find_scripts(page),
      :images => find_images(page),
      :css => find_css(page)
    },

    :links => []
  }

  page.links.each do |link|

    begin
      if link.href[0] == '/' # this is a relative link
        @queue << link.href
        data[:links] << link.href
      elsif link.href[0..@start_page.length] == @start_page # still part of this domain
        @queue << link.href
        data[:links] << link.href
      else
        # @logger.info "This link did not fall under our jurisdiction: #{link.href}"
      end
    rescue Exception => e
      # @logger.error e
    end
  end

  @data << data
end

#find_css(page) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/spidermech.rb', line 124

def find_css(page)
  page.search('link').map do |css|
    begin
      css.attributes['href'].value
     rescue Exception => e
       # @logger.error e
     end
  end
end

#find_images(page) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/spidermech.rb', line 114

def find_images(page)
  page.search('img').map do |img|
    begin
      img.attributes['src'].value
    rescue Exception => e
      # @logger.error e
    end
  end
end

#find_scripts(page) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/spidermech.rb', line 104

def find_scripts(page)
  page.search('script').map do |script| 
    begin
      script.attributes['src'].value
    rescue Exception => e
      # @logger.error e
    end
  end
end

#left_in_queueObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/spidermech.rb', line 23

def left_in_queue
  i = 0

  @queue.each do |link|
    if @crawled.include? link
      # we don't need to crawl this one
    else
      i += 1
    end
  end

  i
end

#runObject



44
45
46
47
48
49
50
# File 'lib/spidermech.rb', line 44

def run
  while !@queue.empty?
    crawl
  end

  @data
end

#save_jsonObject



37
38
39
40
41
42
# File 'lib/spidermech.rb', line 37

def save_json
  filename = "#{URI.parse(@start_page).host}.json"
  @logger.info "Writing sitemap data to #{filename}"
  json = @data.to_json
  File.open(filename, 'w') { |f| f.write json }
end