Skip to content

Instantly share code, notes, and snippets.

@mqu
Created April 15, 2013 21:38
Show Gist options
  • Save mqu/5391476 to your computer and use it in GitHub Desktop.
Save mqu/5391476 to your computer and use it in GitHub Desktop.
recherche sur l'annaire http://annu.com ; supporte les recherches inversées et toutes sorte de recherche. Avoir les résultats en ligne de commande, quel bonheur.
#!/usr/bin/ruby
# coding: utf-8
# author : Marc Quinton, février 2013, licence : http://fr.wikipedia.org/wiki/WTFPL
require 'pp'
require 'rubygems'
require 'mechanize'
class PhoneEntry
def initialize args
@values = args
end
def to_s
return sprintf("%s %s (%s)\n - %s", @values[:phone], @values[:name], @values[:type].to_s, @values[:addr].join(' ') )
end
end
class Annu
def initialize
@url = 'http://www.annu.com/includes/resultats.php'
@agent = Mechanize.new
end
def search str
page = @agent.post(@url, {
"q" => ARGV[0],
})
list = []
# particuliers
page.parser.css('#particuliers ol.list li.entry').each do |e|
# replace <br> by "\n"
e.css('br').each{ |br| br.replace("\n") }
list << PhoneEntry.new({
:name => e.search('h2').text,
:phone => e.search('.phone span')[0].text,
:addr => e.search('.adr p').text.split("\n"),
:type => :particulier
# :fax => e.search('.phone span')[1].text
})
end
# professionnels
page.parser.css('#professionnels ol.list li.entry').each do |e|
# replace <br> by "\n"
e.css('br').each{ |br| br.replace("\n") }
list << PhoneEntry.new({
:name => e.search('h2').text,
:phone => e.search('.phone span')[0].text,
:addr => e.search('.adr p').text.split("\n"),
:type => :professionnel
# :fax => e.search('.phone span')[1].text
})
end
return list
end
end
annu = Annu.new
annu.search(ARGV[0]).each do |e|
puts e
puts "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment