OSDN Git Service

add blogger support
authormasahino <masahino@0978bef0-6439-0410-a06b-a62e4d60c955>
Tue, 2 Mar 2010 13:34:36 +0000 (13:34 +0000)
committermasahino <masahino@0978bef0-6439-0410-a06b-a62e4d60c955>
Tue, 2 Mar 2010 13:34:36 +0000 (13:34 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/lbw/ldblogwriter/trunk@84 0978bef0-6439-0410-a06b-a62e4d60c955

lib/ldblogwriter/atompub.rb
lib/ldblogwriter/services/blogger.rb [new file with mode: 0644]
lib/ldblogwriter/services/livedoor.rb
test/test-service_blogger.rb [new file with mode: 0644]
test/test-service_livedoor.rb

index 6fe6f76..8f492c6 100644 (file)
@@ -2,6 +2,7 @@
 require 'rexml/document'
 require 'uri'
 require 'net/http'
+require 'net/https'
 
 require 'ldblogwriter/wsse.rb'
 require 'ldblogwriter/atom_response.rb'
@@ -10,6 +11,8 @@ module LDBlogWriter
   class AtomPubClient
     attr_accessor :username, :password, :authtype
 
+    GOOGLE_LOGIN_URL = 'https://www.google.com/accounts/ClientLogin'
+
     def initialize(username, password, authtype=nil)
       @username = username
       @password = password
@@ -25,7 +28,7 @@ module LDBlogWriter
           puts res.body
           return false
         end
-        return AtomResponse.new(res.body).collection_uri
+        return AtomResponse.new(res.body)
       end
     end
 
@@ -102,6 +105,11 @@ module LDBlogWriter
     def auth_basic(username, password)
     end
 
+    def auth_google(username, password)
+      return {'Authorization' => 'GoogleLogin auth='+
+        get_google_auth_token(username, password)}
+    end
+
     def auth_method(type)
       "auth_#{type.to_s.downcase}".intern
     end
@@ -114,34 +122,36 @@ module LDBlogWriter
         return `file -bi #{filename}`.chomp
       end
     end
-  end
-end
-
-
-if $0 == __FILE__
-  $test = true
-end
-
-if defined?($test) && $test
-  require 'test/unit'
-  require 'config.rb'
-  require 'pp'
 
-  class TestAtomPub < Test::Unit::TestCase
-    def setup
-      config_file = ENV['HOME'] + "/.ldblogwriter.conf"
-      @conf = LDBlogWriter::Config.new(config_file)
-      @atom = LDBlogWriter::AtomPubClient::new(@conf.username, @conf.password, :wsse)
-    end
+    private
     
-    def test_create_media
-#      @atom.create_media(@conf.atom_pub_uri+"blog/" + @conf.username+"/image",
-#                         "../../test/test.jpg", "test_image.jpg")
+    def get_google_auth_token(username, password)
+      url = URI.parse(GOOGLE_LOGIN_URL)
+      req = Net::HTTP::Post.new(url.path)
+      
+      req.form_data = {'Email' => username,
+        'Passwd' => password,
+        'service' => 'blogger', 
+        'accountType' => 'GOOGLE',
+        'source' => "hoge-lbw-1"}
+      https = Net::HTTP.new(url.host, url.port)
+      https.use_ssl = true
+      https.verify_mode = OpenSSL::SSL::VERIFY_PEER
+      store = OpenSSL::X509::Store.new
+      store.set_default_paths
+      https.cert_store = store
+      https.start do
+        res = https.request(req)
+        if res.body =~ /Auth=(.+)/
+          return $1
+        else
+          puts res.body
+        end
+      end
+      return nil
     end
 
-    def test_get_resource_uri
-      uri_a = @atom.get_resource_uri(@conf.atom_pub_uri)
-    end
   end
 end
 
+
diff --git a/lib/ldblogwriter/services/blogger.rb b/lib/ldblogwriter/services/blogger.rb
new file mode 100644 (file)
index 0000000..6367768
--- /dev/null
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+require 'uri'
+require 'net/http'
+require 'cgi'
+
+require 'ldblogwriter/atompub.rb'
+require 'ldblogwriter/service.rb'
+
+module LDBlogWriter
+  class Blogger < AbstractService
+
+
+    def initialize(config)
+      super
+      @atom_client = LDBlogWriter::AtomPubClient.new(config.username, config.password, :google)
+#      get_resource_uri(@atom_client, @config.atom_pub_uri)
+      @entry_uri = config.post_uri
+    end
+
+    def post_entry(content, title, category = nil)
+      return @atom_client.create_entry(@entry_uri, 
+                                       to_xml(content, title, category))
+    end
+
+    def edit_entry
+    end
+
+    def delete_entry
+    end
+
+    def post_image(image_file_path, image_title = nil)
+      @atom_client.create_media(@image_uri, image_file_path)
+    end
+
+    def to_xml(content, title, category = nil)
+      data = "<entry xmlns='http://www.w3.org/2005/Atom'>\n"
+      data += "<title type='text'>#{title}</title>\n"
+      data += "<content type='xhtml'>\n"
+      data += "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+      data += content
+      data += "</div>\n"
+      data += "</content>\n"
+      data += "<author>\n"
+#      data += "<name>#{@conf.username}</name>\n"
+#      data += "<email>#{@conf.username}</email>\n"
+      data += "</author>\n"
+      data += "</entry>\n"
+      return data
+    end
+
+    private
+
+    def get_resource_uri(atom_client, atom_uri)
+      res_a = atom_client.get_resource_uri(atom_uri)
+#      @entry_uri = res_a[0]
+#      @image_uri = res_a[1]
+    end
+  end
+end
+
+
index f2db937..f5344f9 100644 (file)
@@ -5,6 +5,7 @@ require 'cgi'
 
 require 'ldblogwriter/atompub.rb'
 require 'ldblogwriter/service.rb'
+require 'ldblogwriter/atom_response.rb'
 
 module LDBlogWriter
   class LiveDoor < AbstractService
@@ -71,9 +72,9 @@ EOF
     private
 
     def get_resource_uri(atom_client, atom_uri)
-      res_a = atom_client.get_resource_uri(atom_uri)
-      @entry_uri = res_a[0]
-      @image_uri = res_a[1]
+      res = atom_client.get_resource_uri(atom_uri)
+      @entry_uri = res.collection_uri[0]
+      @image_uri = res.collection_uri[1]
     end
 
   end
diff --git a/test/test-service_blogger.rb b/test/test-service_blogger.rb
new file mode 100644 (file)
index 0000000..954946f
--- /dev/null
@@ -0,0 +1,28 @@
+$LOAD_PATH.unshift 'lib'
+
+require 'test/unit'
+require 'mocha'
+require 'ldblogwriter/services/blogger.rb'
+require 'ldblogwriter/config.rb'
+
+
+class TestLiveDoor < Test::Unit::TestCase
+  def setup
+    config_file = ENV['HOME'] + "/.ldblogwriter.conf"
+    conf = LDBlogWriter::Config.new(config_file)
+    @ld = LDBlogWriter::Blogger::new(conf)
+  end
+  
+  def test_to_xml
+    assert(@ld.to_xml("test content", "test title", "test"))
+  end
+
+  def test_post_entry
+    post_ret = Net::HTTPResponse.new("1.1", "201", "hoge")
+    post_ret['Location'] = "huga"
+    Net::HTTP.any_instance.stubs(:post).returns(post_ret)
+    ret = @ld.post_entry("test content", "this is test", "test")
+    assert_instance_of(String, ret)
+  end
+end
+
index d82d1e4..855e6b2 100644 (file)
@@ -14,7 +14,7 @@ class TestLiveDoor < Test::Unit::TestCase
   end
   
   def test_to_xml
-    assert(           @ld.to_xml("test content", "test title", "test"))
+    assert(@ld.to_xml("test content", "test title", "test"))
   end
 
   def test_post_entry