1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#
# Copyright (C) 2007 peo <peo@mb.neweb.ne.jp>
# You can redistribute it and/or modify it under GPL2.
#
# modified hsbt.
require 'net/http'
@miniblog_config = (Struct.const_defined?("MiniBlogConfig") ? Struct::MiniBlogConfig :
Struct.new("MiniBlogConfig", :host, :path))
@miniblog_list = {
'HatenaHaiku' => @miniblog_config.new('h.hatena.ne.jp', '/api/statuses/update.json'),
}
module Miniblog
class Updater
def initialize( user, pass, config )
@user = user
@pass = pass
@config = config
end
# this code is based on http://la.ma.la/blog/diary_200704111918.htm
def update( status )
Net::HTTP.version_1_2
req = Net::HTTP::Post.new(@config.path)
req.basic_auth(@user, @pass)
req.body = status
Net::HTTP.start( @config.host, 80 ) do |http|
response = http.request(req)
if response.body =~ /error/
raise 'update failed.'
end
end
end
end
end
def notify_miniblog
notify_miniblog_init
date = @date.strftime('%Y%m%d')
diary = @diaries[date]
sectitle = ''
index = 0
diary.each_section do |sec|
index += 1
sectitle = sec.subtitle
end
# strip category
sectitle.gsub!(/\[[^\]]+\] */, '')
url = CGI.escape(@conf.base_url + anchor("#{date}p%02d" % index))
prefix = @conf['miniblog.notify.prefix']
format = @conf['miniblog.notify.format']
source = 'tdiary/notify_miniblog.rb'
status = 'status=' + format % [prefix, sectitle, url] + '&source=' + source
if @conf['miniblog.service'] == "HatenaHaiku" then
status += '&keyword=id:' + @conf['miniblog.user']
end
config = @miniblog_list[@conf['miniblog.service']]
begin
miniblog_updater = Miniblog::Updater.new(@conf['miniblog.user'], @conf['miniblog.pass'], config)
miniblog_updater.update( status )
rescue => e
@logger.debug( e )
end
end
def notify_miniblog_init
@conf['miniblog.notify.prefix'] ||= '[blog update]'
@conf['miniblog.notify.format'] ||= '%s %s %s'
@conf['miniblog.service'] ||= 'Twitter'
end
add_update_proc do
if @mode == 'append' then
notify_miniblog if @cgi.params['miniblog.notify'][0] == 'true'
end
end
add_edit_proc do
checked = ''
if @mode == 'preview' then
checked = @cgi.params['miniblog.notify'][0] == 'true' ? ' checked' : ''
end
<<-HTML
<div class="miniblog.notify"><label for="miniblog.notify">
<input type="checkbox" id="miniblog.notify" name="miniblog.notify" value="true"#{checked} tabindex="400">
Post the update to #{@conf['miniblog.service']}
</label>
</div>
HTML
end
add_conf_proc( 'notify_miniblog', 'MiniBlog' ) do
notify_miniblog_init
if @mode == 'saveconf' then
@conf['miniblog.service'] = @cgi.params['miniblog.service'][0]
@conf['miniblog.user'] = @cgi.params['miniblog.user'][0]
@conf['miniblog.pass'] = @cgi.params['miniblog.pass'][0]
@conf['miniblog.notify.prefix'] = @cgi.params['miniblog.notify.prefix'][0]
@conf['miniblog.notify.format'] = @cgi.params['miniblog.notify.format'][0]
end
options = ''
@miniblog_list.each_key do |key|
options << %Q|<option value="#{h key}"#{" selected" if @conf['miniblog.service'] == key}>#{h key}</option>\n|
end
<<-HTML
<h3 class="subtitle">MiniBlog Service</h3>
<p><select name="miniblog.service">
#{options}
</select></p>
<h3 class="subtitle">Account Name</h3>
<p><input name="miniblog.user" value="#{h @conf['miniblog.user']}"></p>
<h3 class="subtitle">Account Password</h3>
<p><input name="miniblog.pass" value="#{h @conf['miniblog.pass']}"></p>
<h3 class="subtitle">Notify prefix</h3>
<p><input name="miniblog.notify.prefix" value="#{h @conf['miniblog.notify.prefix']}"></p>
<h3 class="subtitle">Notify status format</h3>
<p><input name="miniblog.notify.format" value="#{h @conf['miniblog.notify.format']}"></p>
HTML
end
# vim:ts=3
|