OSDN Git Service

git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/lbw/ldblogwriter/trunk@80 0978bef0...
authormasahino <masahino@0978bef0-6439-0410-a06b-a62e4d60c955>
Tue, 23 Feb 2010 13:49:39 +0000 (13:49 +0000)
committermasahino <masahino@0978bef0-6439-0410-a06b-a62e4d60c955>
Tue, 23 Feb 2010 13:49:39 +0000 (13:49 +0000)
lib/ldblogwriter.rb
lib/ldblogwriter/atompub.rb
lib/ldblogwriter/parser.rb
lib/ldblogwriter/services/livedoor.rb
misc/booklog.rb
test/test-parser.rb
test/test.jpg

index 6847c8d..e0f68f0 100644 (file)
@@ -56,51 +56,16 @@ module LDBlogWriter
       end
       puts "filename : #{filename}"
       # load
-      category = ""
-      title = ""
       src_text = ""
-      content = ""
       File.open(filename, "r") do |file|
-        line = file.gets
-        line = Kconv::toutf8(line)
-        line.gsub!(/^<(.*)>\s+/) do |str|
-          category = $1
-          if get_categories.include?(category)
-            puts "category : #{category}"
-          else
-            puts "unknown category : #{category}"
-          end
-          str.replace("")
-        end
-        title = line
-        puts "title : #{title}"
         src_text = file.read
         src_text = Kconv::toutf8(src_text)
       end
-      entry = BlogEntry.new(@conf, title, category)
-      if @conf.convert_to_html == true
-        src_text = check_image_file(filename, src_text)
-        content = Parser.new(@conf, @plugin, @service).to_html(src_text, entry)
-        if @conf.html_directory != nil
-          save_html_file(@conf.html_directory, File.basename(filename), content)
-        end
-      else
-        content = src_text
-      end
-      entry.content = content
-      if $DEBUG
-        puts "category: " + entry.category
-        puts "title: " +entry.title
-        puts "contnt: " + entry.content
-      end
-      
-      command = Command::new(@conf.auth_type)
+      entry = Parser.new(@conf, @plugin, @service).get_entry(src_text)
+
       if @edit_uri_h[File.basename(filename)] == nil
         # post
         if dry_run == false
-#          edit_uri = command.post(@conf.post_uri, @conf.username,
-#                                  @conf.password,
-#                                  entry)
           edit_uri = @service.post_entry(entry.content, entry.title, entry.category)
           if $DEBUG
             puts "editURI : #{edit_uri}"
@@ -114,8 +79,7 @@ module LDBlogWriter
         # edit
         if dry_run == false
           edit_uri = @edit_uri_h[File.basename(filename)]
-          command.edit(edit_uri, @conf.username, @conf.password,
-                       entry)
+          @service.edit_entry(edit_uri, entry)
           entry.get_entry_info(edit_uri)
         end
       end
index ebdaaf1..6fe6f76 100644 (file)
@@ -36,7 +36,7 @@ module LDBlogWriter
       uri = URI.parse(uri_str)
       Net::HTTP.start(uri.host, uri.port) do |http|
         res = http.post(uri.path, entry_xml,
-                        authenticate(@username, @password).update({'Content-Type' => 'application/atom+xml'}))
+                        authenticate(@username, @password, @authtype).update({'Content-Type' => 'application/atom+xml'}))
         case res.code
         when "201"
           edit_uri = res['Location']
index 94cbd38..49dbd61 100644 (file)
@@ -20,6 +20,30 @@ module LDBlogWriter
       @service = service
     end
 
+    def get_entry(src_text)
+      lines = src_text.rstrip.split(/\r?\n/)
+      first_line = lines.shift
+      category = nil
+      first_line.gsub!(/^<(.*)>\s+/) do |str|
+        category = $1
+        str.replace("")
+      end
+      title = first_line
+      src_text = lines.join("\n")
+      @entry = BlogEntry.new(@conf, title, category)
+      if @conf.convert_to_html == true
+#        src_text = check_image_file(filename, src_text)
+        content = to_html(src_text)
+#        if @conf.html_directory != nil
+#          save_html_file(@conf.html_directory, File.basename(filename), content)
+#        end
+      else
+        content = src_text
+      end
+      @entry.content = content
+      return @entry
+    end
+
     def escape_html(str)
       str.gsub!(/&/, '&amp;')
       str.gsub!(/"/, '&quot;')
@@ -28,8 +52,11 @@ module LDBlogWriter
       return str
     end
 
+#    def to_html(src, entry = nil)
     def to_html(src, entry = nil)
-      @entry = entry
+      if entry != nil
+        @entry = entry
+      end
       buf = []
       lines = src.rstrip.split(/\r?\n/).map {|line| line.chomp}
 
index 833e86e..e105f07 100644 (file)
@@ -4,6 +4,7 @@ require 'net/http'
 require 'cgi'
 
 require 'ldblogwriter/atompub.rb'
+require 'ldblogwriter/service.rb'
 
 module LDBlogWriter
   class LiveDoor < AbstractService
@@ -13,7 +14,7 @@ module LDBlogWriter
       get_resource_uri(@atom_client, @config.atom_pub_uri)
     end
 
-    def post_entry(conetent, title, category = nil)
+    def post_entry(content, title, category = nil)
       return @atom_client.create_entry(@entry_uri, to_xml(content, title, category))
     end
 
@@ -34,7 +35,7 @@ module LDBlogWriter
     xmlns:app="http://www.w3.org/2007/app"
     xmlns:blogcms="http://blogcms.jp/-/atom">
 EOF
-      data += "<title>#{@title.chomp}</title>\n"
+      data += "<title>#{title.chomp}</title>\n"
 
 # #    <link rel="alternate" type="text/html" 
 #        href="http://blog.livedoor.jp/staff/archives/000000.html" />
@@ -47,10 +48,10 @@ EOF
 
       data += "<content type=\"text/html\" xml:lang=\"ja\">\n"
 #      data += [@content].pack("m").chomp
-      data += CGI::escapeHTML(@content)
+      data += CGI::escapeHTML(content)
       data += "</content>\n"
 
-      data += "<category scheme=\"http://livedoor.blogcms.jp/atom/blog/masahino123/category\" term=\"#{@category.chomp}\"/>\n"
+      data += "<category scheme=\"http://livedoor.blogcms.jp/atom/blog/masahino123/category\" term=\"#{category.chomp}\"/>\n"
 
 #     <blogcms:source>
 #         <blogcms:body><![CDATA[<p>記事本文</p>]]></blogcms:body>
index af0dbc7..f325bc4 100644 (file)
@@ -7,8 +7,8 @@ module Booklog
     require 'kconv'
 
     BooklogHomeURI = 'http://booklog.jp'
-    BooklogLoginURI = 'http://booklog.jp/login.php'
-    BooklogInputURI = 'http://booklog.jp/input.php'
+    BooklogLoginURI = 'http://booklog.jp/login'
+    BooklogInputURI = 'http://booklog.jp/input'
     def initialize(user_id, password)
       @agent = WWW::Mechanize.new
       @agent.post_connect_hooks << lambda{|params| params[:response_body] = NKF.nkf('-w8m0', params[:response_body])}
@@ -21,16 +21,18 @@ module Booklog
 #      login_form = login_page.forms.with.action("./uhome.php").first
       login_form = login_page.form_with(:name => 'frm')
       login_form.account = user_id
-      login_form.pw = password
+      login_form.password = password
       result_page = login_form.submit
     end
 
     # ISBNによる登録
     def input(isbn_list)
       input_page = @agent.get(BooklogInputURI)
-      input_form = input_page.form('frm')
-      input_form['asin'] = isbn_list.join("\n")
-      result_page = input_form.submit
+      input_form = input_page.form_with(:action => BooklogInputURI)
+      input_form['isbns'] = isbn_list.join("\n")
+#      result_page = input_form.submit
+      result_page = input_form['buttons'].click
+      pp result_page
     end
 
     def comment(asin, comment)
@@ -64,8 +66,12 @@ if defined?($test) && $test
     end
 
     def test_authentication
-      Booklog::Agent.new(@config.options['booklog_userig'], @config.options['booklog_password'])
+      Booklog::Agent.new(@config.options['booklog_userid'], @config.options['booklog_password'])
     end
 
+    def test_input
+      agent = Booklog::Agent.new(@config.options['booklog_userid'], @config.options['booklog_password'])
+      agent.input(['4480426280'])
+    end
   end
 end
index 461b99a..fb446b6 100644 (file)
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
-$LOAD_PATH.unshift '../lib'
+$LOAD_PATH.unshift 'lib'
+
 
 require 'test/unit'
 require 'ldblogwriter/config.rb'
@@ -16,13 +17,14 @@ end
 
 class TestParser < Test::Unit::TestCase
   def setup
-    @conf = LDBlogWriter::Config.new('test.conf')
+    @conf = LDBlogWriter::Config.new('test/test.conf')
     @parser = LDBlogWriter::Parser.new(@conf, nil)
   end
 
     def test_to_html
+      str = "テスト。\n\n空行ごとに分離されるとうれしいな。\n"
       assert_equal("<p>\nテスト。\n</p>\n<p>\n空行ごとに分離されるとうれしいな。\n</p>", 
-                   @parser.to_html("テスト。\n\n空行ごとに分離されるとうれしいな。\n"))
+                   @parser.to_html(str))
     end
 
   def test_parse_img
@@ -80,7 +82,7 @@ class TestParser < Test::Unit::TestCase
   def test_parse_a_href
     entry = LDBlogWriter::BlogEntry.new(@conf, "test", "category")
     @parser.to_html("[[hoge:http://blog.livedoor.jp/masahino123/archives/64994357.html]]", entry)
-    assert_equal(["http://app.blog.livedoor.jp/masahino123/tb.cgi/64994357"],
+    assert_equal(["http://trackback.blogsys.jp/livedoor/masahino123/64994357"],
                  entry.trackback_url_array)
   end
 
@@ -105,8 +107,17 @@ class TestParser < Test::Unit::TestCase
   def test_parse_p
   end
 
-  def tet_a_href
+  def test_a_href
   end
 
-  
+  def test_get_entry
+    entry = @parser.get_entry("<hoge> title")
+    assert_equal("hoge", entry.category)
+    assert_equal("title", entry.title)
+
+    entry = @parser.get_entry("title2")
+    assert_equal(nil, entry.category)
+    assert_equal("title2", entry.title)
+    entry
+  end
 end
index e69de29..56f21e6 100644 (file)
Binary files a/test/test.jpg and b/test/test.jpg differ