Created
April 15, 2013 21:38
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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