Daily Archives for Monday, October 2007

Detectando los crawlers desde rails

Supongo que por allí deben haber funciones más elaboradas y documentadas. Pero os adjunto el código que yo uso para detectar si se trata de una visita normal o un crawler ( motor de búsqueda) que rastrea mi site. Como veréis hay poca ciencia, pero si experiencia.

 

En application controller añadir la siguiente función:

def is_crawler(agent)
  testv=false
  if agent =~ /Slurp/ then #yahoo
    testv=true
  end
  if agent =~ /Mediapartners-Google/ then
    testv=true
  end
   if agent =~ /VoilaBot/ then
    testv=true
  end
  #Googlebot
   if agent =~ /Googlebot/ then
    testv=true
  end
  if agent =~ /Spider/ then
    testv=true
  end
  #msnbot-media
  if agent =~ /msnbot-media/ then
    testv=true
  end
  #SnapPreviewBot
  if agent =~ /SnapPreviewBot/ then
    testv=true
  end
  if agent =~ /ichiro/ then
    testv=true
  end
  if agent =~ /Jeeves/ then
    testv=true
  end
  return testv
end

Esta función analiza la cadena del agent y detecta si es la de un crawler. He ido elaborando la lista a base de ir viendo las visitas que tenía.

 

Ahora para usar nuestra función en el controlador que queramos añadimos:

     agent = request.user_agent
     if not is_crawler(agent) then

      #……..acción a hacer si no es crawler

    else

     #——- acción a hacer si es crawler

    end

 

Es una función sencilla pero la he querido compartir ya que yo he tenido la paciencia de ir añadiendo las diferentes cadenas que llevan los diferentes crawlers. No se si existe algún plugin, pero sería interesante crearlo.