Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merged in schwarzesbuch and upped to 1.0.5

  • Loading branch information...
commit 513ff51283d9a45a718e3ea3d4a93d3b95740cb1 1 parent a0c4d4b
@purzelrakete purzelrakete authored
View
6 History.txt → CHANGES.markdown
@@ -1,3 +1,9 @@
+== 1.0.5 / 2008-11-26
+
+* moved to github
+* added patch for gmail. emails were coming in with notes.
+* added german stuff to blackbook gem
+
== 1.0.4 / 2008-02-27
* 1 minor enhancement
View
4 Manifest.txt
@@ -1,6 +1,6 @@
-History.txt
+CHANGES.markdown
Manifest.txt
-README.txt
+README.markdown
Rakefile
debug_blackbook.rb
init.rb
View
0  README
No changes.
View
5 README.txt → README.markdown
@@ -1,5 +1,5 @@
Blackbook
-http://rubyforge.org/projects/contentfree/
+http://github.com/purzelrakete/blackbook/tree/master
== DESCRIPTION:
@@ -41,7 +41,8 @@ If you create an additional importer or exporter - or simply find a bug - please
== INSTALL:
-* sudo gem install blackbook
+1. gem sources -a http://gems.github.com
+2. sudo gem install purzelrakete-blackbook
== THANKS:
View
6 debug_blackbook.rb
@@ -4,7 +4,7 @@
puts "===== aol #{aol.inspect}"
unless aol.detect{|c| c[:name].downcase == 'Dave Myron'.downcase && c[:email] == 'dave.myron@contentfree.com'}
- puts "AOL not working"
- exit 1
- end
+ puts "AOL not working"
+ exit 1
+end
View
2  lib/blackbook.rb
@@ -4,7 +4,7 @@
class Blackbook
include ::Singleton
- VERSION = '1.0.4'
+ VERSION = '1.0.5'
class BlackbookError < ::StandardError; end
class BadCredentialsError < BlackbookError; end
View
62 lib/blackbook/importer/freenet.rb
@@ -0,0 +1,62 @@
+require 'blackbook/importer/page_scraper'
+
+class Blackbook::Importer::Freenet < Blackbook::Importer::PageScraper
+ LOGIN_URL = "https://office.freenet.de"
+ MESSAGES_URL = "/main_overview.html"
+
+ def =~( options )
+ options && options[:username] =~ /@freenet\.de$/i
+ end
+
+ def login
+ username, password = options[:username], options[:password]
+
+ begin
+ page = agent.get LOGIN_URL
+
+ form = page.forms.with.name("loginform").first
+ form.username = username.split("@").first
+ form.password = password
+ page = form.submit
+
+ if page.body.match(/Fehlgeschlagene Login-Versuche/)
+ page = page.forms.with.name("sicherform").submit
+ page = agent.get MESSAGES_URL
+ end
+
+ if page.body.match(/Login (erneut )?fehlgeschlagen/) || page.body.match(/Ich bin bereits Mitglied/)
+ raise Blackbook::BadCredentialsError.new
+ end
+ rescue => e
+ raise e || Blackbook::BlackbookError.new
+ end
+ end
+
+ def prepare
+ login
+ end
+
+ def scrape_contacts
+ contacts = ["INBOX", "INBOX.sent"].map do |folder|
+ page = agent.get "/messages/mail_mailbox.html?mail_folder=#{folder}"
+ find_contacts(page)
+ end
+
+ contacts.inject([]) do |memo, contact|
+ memo << contact unless memo.include? contact
+ memo
+ end.flatten
+ end
+
+
+ protected
+ def find_contacts(page)
+ page.search("tr[@id]/td[9]").map do |cell|
+ recp, fullname, email = *cell.attributes["title"].match(/"(.*)"<(.*)>/)
+
+ { :name => fullname, :email => email }
+ end
+ end
+
+ Blackbook.register :freenet, self
+end
View
19 lib/blackbook/importer/gmail.rb
@@ -48,13 +48,18 @@ def scrape_contacts
page = agent.get('http://mail.google.com/mail/h/?v=cl&pnl=a')
contact_rows = page.search("input[@name='c']/../..")
contact_rows.collect do |row|
- columns = row/"td"
- {
- :name => ( columns[1] / "b" ).inner_html, # name
- :email => columns[2].inner_html.gsub( /(\n|&nbsp;)/, '' ) # email
- }
- end
+ email = columns[2].inner_html.gsub( /(\n|&nbsp;)/, '' ) # email
+ clean_email = email[/[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}/]
+
+ unless clean_email.blank?
+ columns = row/"td"
+ {
+ :name => ( columns[1] / "b" ).inner_html, # name
+ :email => clean_email
+ }
+ end
+ end.compact
end
Blackbook.register(:gmail, self)
-end
+end
View
68 lib/blackbook/importer/gmx.rb
@@ -0,0 +1,68 @@
+require 'blackbook/importer/page_scraper'
+
+class Blackbook::Importer::GMX < Blackbook::Importer::PageScraper
+ LOGIN_URL = "https://www.gmx.net/"
+
+ def =~( options )
+ options && options[:username] =~ /@gmx\.(de|net)$/i
+ end
+
+ def login
+ username, password = options[:username], options[:password]
+
+ begin
+ page = agent.get LOGIN_URL
+
+ form = page.forms.with.name("login").first
+ form.id = username
+ form.p = password
+ page = form.submit
+ @next = page
+
+ if (page.at("div.index") && page.at("div.index").inner_html == "Passwort vergessen?") ||
+ page.uri.to_s.match(/login-failed/)
+ raise Blackbook::BadCredentialsError.new
+ end
+ rescue => e
+ raise e || Blackbook::BlackbookError.new
+ end
+ end
+
+ def prepare
+ login
+ end
+
+ def scrape_contacts
+ page = @next
+
+ # Go to E-Mails
+ page = page.links.select { |link| link.text =~ /E-Mail/ }.first.click
+
+ contacts = [/Posteingang/, /Archiv/, /Gesendet/].map do |folder|
+ # puts "folder: #{folder}"
+ # puts "page title: #{page.title}"
+ # puts "page links: #{page.links.select { |link| link.text =~ folder }}"
+ page = page.links.select { |link| link.text =~ folder }.first.click
+ find_contacts(page)
+ end
+
+ contacts.inject([]) do |memo, contact|
+ memo << contact unless memo.include? contact
+ memo
+ end.flatten
+ end
+
+ protected
+
+ def find_contacts(page)
+ links = page.search("form#MI a").select { |link| link.attributes["title"] =~ /@/ }
+ links.map do |link|
+ recp = link.attributes["title"].gsub(/\n/, "").split(/\s/)
+ email = recp.pop.gsub(/[<>]/, "")
+ fullname = recp.join(" ")
+
+ { :name => fullname, :email => email }
+ end
+ end
+ Blackbook.register :gmx, self
+end
View
67 lib/blackbook/importer/web.de.rb
@@ -0,0 +1,67 @@
+require 'blackbook/importer/page_scraper'
+
+class Blackbook::Importer::WebDE < Blackbook::Importer::PageScraper
+ LOGIN_URL = "https://freemail.web.de"
+
+ def =~( options )
+ options && options[:username] =~ /@web\.de$/i
+ end
+
+ def login
+ username, password = options[:username], options[:password]
+
+ begin
+ page = agent.get LOGIN_URL
+
+ form = page.forms.with.name("login").first
+ form.username = username
+ form.password = password
+
+ page = form.submit
+ # follow logout hint
+ if (continue_link = page.links.select { |link| link.text =~ /weiter zu FreeMail/ }.first)
+ page = continue_link.click
+ end
+
+ if page.uri.to_s.match(/logonfailed/)
+ raise Blackbook::BadCredentialsError.new
+ end
+
+ # follow content frame
+ @next = agent.get page.frames[1].src
+ rescue => e
+ raise e || Blackbook::BlackbookError.new
+ end
+ end
+
+ def prepare
+ login
+ end
+
+ def scrape_contacts
+ page = @next
+
+ contacts = [/Posteingang/, /Unbekannt/].map do |folder|
+ page = page.links.select { |link| link.text =~ folder }.first.click
+ find_contacts(page)
+ end
+
+ contacts.inject([]) do |memo, contact|
+ memo << contact unless memo.include? contact
+ memo
+ end
+ end
+
+ protected
+ def find_contacts(page)
+ page.search("span.from a").map do |link|
+ recp = link.attributes["title"].gsub(/\n/, "").split(/\s/)
+ email = recp.pop
+ fullname = recp.join(" ")
+
+ { :name => fullname, :email => email }
+ end
+ end
+
+ Blackbook.register :webde, self
+end
View
9 test/config/credentials.yml.example
@@ -0,0 +1,9 @@
+gmx:
+ username: nil
+ password: nil
+freenet:
+ username: nil
+ password: nil
+webde:
+ username: nil
+ password: nil
View
53 test/freenet_importer_test.rb
@@ -0,0 +1,53 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+context "The Freenet importer" do
+
+ setup do
+ @username, @password = BlackbookExtensions::TestHelper::Credentials.new(:freenet).get
+
+ @importer = Blackbook::Importer::Freenet.new
+ @importer.create_agent
+ end
+
+ specify "should match emails" do
+ @importer.=~(nil).should.not.be
+ @importer.=~({}).should.not.be
+ @importer.=~({ 'username' => "john@foo.com" }).should.not.be
+ @importer.=~({ :username => "john@foo.com" }).should.not.be
+ @importer.=~({ :username => "john" }).should.not.be
+ @importer.=~({ :username => "john@freenet.de" }).should.be
+ @importer.=~({ :username => "JOHN@FREENET.DE" }).should.be
+ end
+
+ specify "should be able to login with correct credentials" do
+ should.not.raise Exception do
+ login(@username, @password)
+ end
+ end
+
+ specify "should raise BadCredentialsError on login with wrong credentials" do
+ should.raise Blackbook::BadCredentialsError do
+ login
+ end
+ end
+
+ specify "should raise BadCredentialsError on login with correct username and wrong password" do
+ should.raise Blackbook::BadCredentialsError do
+ login(@username)
+ end
+ end
+
+ specify "should able to get contacts" do
+ login(@username, @password)
+
+ contacts = @importer.scrape_contacts
+ contacts.should.not.be.empty
+ contacts.should.be.all { |contact| contact.is_a? Hash }
+ end
+
+ private
+ def login(username = "foo", password = "bar")
+ @importer.options = { :username => username, :password => password }
+ @importer.login
+ end
+end
View
53 test/gmx_importer_test.rb
@@ -0,0 +1,53 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+context "The GMX importer" do
+
+ setup do
+ @username, @password = BlackbookExtensions::TestHelper::Credentials.new(:gmx).get
+
+ @importer = Blackbook::Importer::GMX.new
+ @importer.create_agent
+ end
+
+ specify "should match emails" do
+ @importer.=~(nil).should.not.be
+ @importer.=~({}).should.not.be
+ @importer.=~({ 'username' => "john@foo.com" }).should.not.be
+ @importer.=~({ :username => "john@foo.com" }).should.not.be
+ @importer.=~({ :username => "john" }).should.not.be
+ @importer.=~({ :username => "john@gmx.de" }).should.be
+ @importer.=~({ :username => "JOHN@GMX.DE" }).should.be
+ end
+
+ specify "should be able to login with correct credentials" do
+ should.not.raise Exception do
+ login(@username, @password)
+ end
+ end
+
+ specify "should raise BadCredentialsError on login with wrong credentials" do
+ should.raise Blackbook::BadCredentialsError do
+ login
+ end
+ end
+
+ specify "should raise BadCredentialsError on login with correct username and wrong password" do
+ should.raise Blackbook::BadCredentialsError do
+ login(@username)
+ end
+ end
+
+ specify "should able to get contacts" do
+ login(@username, @password)
+
+ contacts = @importer.scrape_contacts
+ contacts.should.not.be.empty
+ contacts.should.be.all { |contact| contact.is_a? Hash }
+ end
+
+ private
+ def login(username = "foo", password = "bar")
+ @importer.options = { :username => username, :password => password }
+ @importer.login
+ end
+end
View
2  test/scripts/live_test.rb
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
-require File.join( File.dirname(__FILE__), '../../lib/blackbook.rb' )
+require File.join( File.dirname(__FILE__), '../../lib/schwarzesbuch.rb' )
require 'optparse'
options = {}
View
23 test/test_helper.rb
@@ -45,3 +45,26 @@ def connect
end
end
end
+
+plugin_root = "#{ File.dirname(__FILE__) }/.."
+
+require 'rubygems'
+require 'test/spec'
+require 'mocha'
+
+module BlackbookExtensions
+ module TestHelper
+ class Credentials
+ attr_accessor :network, :config
+
+ def initialize(network)
+ self.network = network.to_s
+ self.config = YAML.load_file( File.join(File.dirname(__FILE__), "config/credentials.yml") )
+ end
+
+ def get
+ [ self.config[self.network]["username"], self.config[self.network]["password"] ]
+ end
+ end
+ end
+end
View
53 test/web.de_importer_test.rb
@@ -0,0 +1,53 @@
+require File.dirname(__FILE__) + '/../test_helper.rb'
+
+context "The Web.de importer" do
+
+ setup do
+ @username, @password = BlackbookExtensions::TestHelper::Credentials.new(:webde).get
+
+ @importer = Blackbook::Importer::WebDE.new
+ @importer.create_agent
+ end
+
+ specify "should match emails" do
+ @importer.=~(nil).should.not.be
+ @importer.=~({}).should.not.be
+ @importer.=~({ 'username' => "john@foo.com" }).should.not.be
+ @importer.=~({ :username => "john@foo.com" }).should.not.be
+ @importer.=~({ :username => "john" }).should.not.be
+ @importer.=~({ :username => "john@web.de" }).should.be
+ @importer.=~({ :username => "JOHN@WEB.DE" }).should.be
+ end
+
+ specify "should be able to login with correct credentials" do
+ should.not.raise Exception do
+ login(@username, @password)
+ end
+ end
+
+ specify "should raise BadCredentialsError on login with wrong credentials" do
+ should.raise Blackbook::BadCredentialsError do
+ login
+ end
+ end
+
+ specify "should raise BadCredentialsError on login with correct username and wrong password" do
+ should.raise Blackbook::BadCredentialsError do
+ login(@username)
+ end
+ end
+
+ specify "should able to get contacts" do
+ login(@username, @password)
+
+ contacts = @importer.scrape_contacts
+ contacts.should.not.be.empty
+ contacts.should.be.all { |contact| contact.is_a? Hash }
+ end
+
+ private
+ def login(username = "foo", password = "bar")
+ @importer.options = { :username => username, :password => password }
+ @importer.login
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.