Class: PbaCrawler::Generic

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

Constant Summary collapse

@@proxies =

list of proxies address

""
@@cursor =
0
@@proxy =
nil
@@proxy_list_reload =
false

Instance Method Summary collapse

Constructor Details

#initializeGeneric

Returns a new instance of Generic.



12
13
14
15
# File 'lib/pba_crawler/generic.rb', line 12

def initialize
  #load proxy list
  @@proxies = Proxy.order(:position)
end

Instance Method Details

#crawl_url(url, use_proxy = true) ⇒ Object



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
# File 'lib/pba_crawler/generic.rb', line 17

def crawl_url(url,use_proxy=true)

  if use_proxy.eql?(true)

    unless @@proxy_list_reload
      load_proxy_list
    end


    @@proxy = @@proxies.fetch(@@cursor)

    p "#{url}|#{@@proxy.address}|#{@@proxy.port}"

    agent = Mechanize.new { |a|
      a.html_parser = Nokogiri::HTML
      a.open_timeout = 20
      a.read_timeout = 20
    }

    agent.set_proxy(@@proxy.address,@@proxy.port)
    agent.user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'

    get_url(agent,url)
  else
    agent = Mechanize.new { |a|
      a.html_parser = Nokogiri::HTML
      a.open_timeout = 20
      a.read_timeout = 20
    }
    agent.get(url)
  end
end

#get_current_proxyObject



85
86
87
# File 'lib/pba_crawler/generic.rb', line 85

def get_current_proxy
  @@proxy
end

#get_url(agent, url) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pba_crawler/generic.rb', line 59

def get_url(agent,url)
  begin
    response = agent.get(url)
    if response
      p "ok"
      @@proxy.update_attributes(:last_request_at => Time.now)
    else
      p "nok"
      response = rescue_get_url(agent,url)
    end
  rescue
    response = rescue_get_url(agent,url)
  end
  response
end

#load_proxy_listObject



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
# File 'lib/pba_crawler/generic.rb', line 89

def load_proxy_list
  #/proxylist.php?port=&type=&ssl=&country=&latency=1000&reliability=9000&sort=reliability&desc=true&pnum=1#table

  url = "http://www.xroxy.com/proxylist.php?port=&type=&ssl=&country=&latency=1000&reliability=9000"

  last_created_proxy = Proxy.order("created_at DESC").first

  if last_created_proxy.nil? || ((Time.now - last_created_proxy.created_at) / 1.hour)>2
    Proxy.delete_all
    begin
      (1..4).each do |index|
        agent = crawl_url("#{url}&pnum=#{index}",false)
        agent.parser.xpath("//a[@title='View this Proxy details']").each do |a|
          address = a.content.strip
          port = a.parent.next.next.children.first.content.strip
          Proxy.create(:address=> address, :port=> port)
        end
        sleep 3
      end
    rescue

    end
    @@proxy_list_reload = true
  else
    @@proxy_list_reload = true
  end
  
end

#load_sampleObject



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
# File 'lib/pba_crawler/generic.rb', line 120

def load_sample
  list = [
    ["101.0.6.87","31280"],
    ["109.120.130.2","3128"],
    ["62.215.5.69","8080"],
    ["62.215.5.66","8080"],
    ["212.33.199.19","80"],
    ["196.1.70.201","80"],
    ["194.87.32.233","80"],
    ["94.125.27.20","8080"],
    ["95.66.35.1","80"],
    ["124.164.247.43","3128"],
    ["186.225.98.17","3128"],
    ["122.155.13.135","3128"],
    ["148.81.130.19","80"],
    ["202.129.58.68","80"],
    ["97.100.143.20","27977"],
    ["24.12.13.154","27977"],
    ["200.129.180.61","80"]
  ]
  Proxy.delete_all
  list.each do |add|
    Proxy.create(:address => add[0], :port => add[1].to_i, :last_request_at => Time.now)
  end
end

#next_proxyObject



50
51
52
53
54
55
56
57
# File 'lib/pba_crawler/generic.rb', line 50

def next_proxy
  if @@cursor + 1 < @@proxies.size
    @@cursor = @@cursor + 1
    true
  else
    false
  end
end

#rescue_get_url(agent, url) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/pba_crawler/generic.rb', line 75

def rescue_get_url(agent,url)
  if @@cursor + 1 < @@proxies.size
    @@cursor = @@cursor + 1
    @@proxy = @@proxies.fetch(@@cursor)
    p "#{url}|#{@@proxy.address}|#{@@proxy.port}"
    agent.set_proxy(@@proxy.address,@@proxy.port)
    response = get_url(agent,url)
   end
end