#!/usr/bin/env ruby
=begin
tl.rb - tDiary Loader.
Copyright (C) 2004 by Hahahaha.
<rin_ne@big.or.jp>
http://www20.big.or.jp/~rin_ne/
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=end
=begin
#
# Configure file example.
id:yourid
password:yourpassword
cgi_url:http://example.com/tdiary/update.rb
# txt_dir:/usr/yourid/diary
# touch:/usr/yourid/diary/hw.touch
# proxy:http://www.example.com:8080/
# g:yourgroup
# client_encoding:Shift_JIS
# server_encoding:UTF-8
## for Unix, if Encode module is not available.
# filter:iconv -f euc-jp -t utf-8 %s
=end
$:.push( File.dirname(File.expand_path($0)) )
require 'optparse'
require 'uri'
require 'iconv'
require 'stringio'
require 'net/http'
require 'cgi'
Net::HTTP.version_1_2
Version = '0.1.0'
ProgramName = 'tDiary Loader'
require 'tdcommon'
BODY_DATA = Regexp.new('<input .*?name="title".*?value="(.*?)">.*?' +
'<div class="textarea">(.*?)</textarea>')
# Main routine.
#
class MainRoutine
include MessagePrint
def initialize
@conf = nil
@server = nil
end
def run
@conf = Configure.new
@conf.mode = 'edit'
# Parse ARGV.
ARGV.options {|opt|
begin
opt.banner = Banner + "\n" + "#{opt.banner}"
opt.summary_width = 16
opt.on( '-d',
"Debug. Use this switch for verbose log.") {
unless debug
debug_on
print_debug("Debug flag on.")
print_message(Banner)
end
}
opt.on( '-u username',
"Username. Specify username.") {|arg|
@conf.user = arg
}
opt.on( '-p password',
"Password. Specify password.") {|arg|
@conf.password = arg
}
opt.on( '-a agent',
"User agent. Default value is '#{DefaultAgent}'.") {|arg|
@conf.agent = arg
}
opt.on( '-T seconds',
"Timeout. Default value is #{@conf.timeout}.") {|arg|
raise("-T #{arg}: not integer.") if /\d+/ !~ arg
@conf.timeout = arg.to_i
}
opt.on( '-f filename',
"File. Save receive data to this file.") {|arg|
@conf.target_file = arg
}
opt.on( '-s [yyyy-mm-dd]',
"STDIN or -F filename is treated as 'yyyy-mm-dd' or current date.") {|arg|
if arg
raise("-s #{arg}: Invalid date format.") if DATE_FORMAT !~ arg
@conf.target_date = arg
else
@conf.target_date = Time.now.localtime
end
}
opt.on( '-o',
"Output to STDOUT.") {
@conf.use_stdout = arg
silent_mode
}
opt.on( '-n config_file',
"Config file. Default value is '#{@conf.config_file}'.") {|arg|
raise("-n #{arg}: No such file.") unless FileTest.file?(arg)
@conf.config_file = arg
}
opt.parse!
rescue
error_exit($!)
end
}
@conf.target_date = Time.now.localtime unless @conf.target_date
@conf.load
if (@conf.target_date.class == Time)
@conf.target_date = (@conf.target_date - @conf.delay).strftime('%Y-%m-%d')
end
@server = TDiaryServer.new(@conf)
@server.open('post')
print_message("Load #{@conf.mode}:#{@conf.target_date}")
@server.receive_entry
print_message('Load OK.')
@server.close
write(@server.response.body)
val = BODY_DATA.match(CGI.unescapeHTML(@server.response.body))
p val
title = convert_to_client(val[1])
body = convert_to_client(val[2])
File.open('output.txt') {|file|
file.puts(title)
file.write(body)
}
end
# Convert from client to server.
def convert_to_server(str)
return '' unless str
return str if @conf.server_encoding == @conf.client_encoding
return Iconv.conv(@conf.server_encoding, @conf.client_encoding, str)
end
# Convert from server to client.
def convert_to_client(str)
return '' unless str
return str if @conf.server_encoding == @conf.client_encoding
return Iconv.conv(@conf.client_encoding, @conf.server_encoding, str)
end
end
begin
routine = MainRoutine.new
routine.run
end