Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails, How to determine if a request was made by a robot or search engine spider?

I've Rails apps, that record an IP-address from every request to specific URL, but in my IP database i've found facebook blok IP like 66.220.15.* and Google IP (i suggest it come from bot). Is there any formula to determine an IP from request was made by a robot or search engine spider ? Thanks

like image 539
Agung Prasetyo Avatar asked May 04 '11 10:05

Agung Prasetyo


3 Answers

Since the well behaved bots at least typically include a reference URI in the UA string they send, something like:

request.env["HTTP_USER_AGENT"].match(/\(.*https?:\/\/.*\)/)

is an easy way to see if the request is from a bot vs. a human user's agent. This seems to be more robust than trying to match against a comprehensive list.

like image 76
tribalvibes Avatar answered Nov 16 '22 02:11

tribalvibes


I think you can use browser gem for check bots.

if browser.bot?
  # code here
end

https://github.com/fnando/browser

like image 39
dimasjt Avatar answered Nov 16 '22 01:11

dimasjt


Robots are required (by common sense / courtesy more than any kind of law) to send along a User-Agent with their request. You can check for this using request.env["HTTP_USER_AGENT"] and filter as you please.

like image 13
Ryan Bigg Avatar answered Nov 16 '22 02:11

Ryan Bigg