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