| 1 | =begin |
|---|
| 2 | delicious.rb |
|---|
| 3 | del.icio.us から その日付分のメモを取ってきて表示するプラグイン |
|---|
| 4 | |
|---|
| 5 | tdiary.conf で以下を設定します。tDiaryの設定画面からも設定可能です。 |
|---|
| 6 | |
|---|
| 7 | @options['delicious.id'] = 'YOUR DELICIOUS ID HERE' |
|---|
| 8 | @options['delicious.pw'] = 'YOUR DELICIOUS PW HERE' |
|---|
| 9 | |
|---|
| 10 | proxy は以下に設定します。 |
|---|
| 11 | |
|---|
| 12 | @options['amazon.proxy'] = 'PROXY_HOST:PORT' |
|---|
| 13 | |
|---|
| 14 | reference: |
|---|
| 15 | * mm_footer.rb by ishinao san |
|---|
| 16 | * rss-show.rb (in Hiki) by TAKEUCHI Hitoshi san |
|---|
| 17 | * fujisan.rb by Michitaka Ohno. |
|---|
| 18 | |
|---|
| 19 | =end |
|---|
| 20 | |
|---|
| 21 | require 'net/https' |
|---|
| 22 | require 'rexml/document' |
|---|
| 23 | require 'fileutils' |
|---|
| 24 | |
|---|
| 25 | def delicious_save_cache cache_file, file |
|---|
| 26 | FileUtils.mkdir_p "#{@cache_path}/delicious" |
|---|
| 27 | File.open("#{@cache_path}/delicious/#{cache_file}", 'w') do |f| |
|---|
| 28 | f.flock(File::LOCK_EX) |
|---|
| 29 | f.puts file |
|---|
| 30 | f.flock(File::LOCK_UN) |
|---|
| 31 | end |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | def delicious_parse_xml(xml) |
|---|
| 35 | posts = [] |
|---|
| 36 | REXML::Document.new(xml).elements.each("posts/post") do |post| |
|---|
| 37 | post = <<-EOS |
|---|
| 38 | <li><a href="#{post.attribute("href").to_s}"> |
|---|
| 39 | #{post.attribute("description").to_s} |
|---|
| 40 | </a></li> |
|---|
| 41 | EOS |
|---|
| 42 | posts << post.gsub(/[\n\r]/,'') |
|---|
| 43 | end |
|---|
| 44 | |
|---|
| 45 | return posts |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | def delicious_get_html(date = Date.now) |
|---|
| 49 | req = Net::HTTP::Get.new "/v1/posts/get?dt=#{date.strftime('%Y-%m-%d')}" |
|---|
| 50 | req.basic_auth @options['delicious.id'], @options['delicious.pw'] |
|---|
| 51 | |
|---|
| 52 | https = Net::HTTP.new('api.del.icio.us', 443) |
|---|
| 53 | https.use_ssl = true |
|---|
| 54 | |
|---|
| 55 | parsed = https.start {|w| |
|---|
| 56 | response = w.request(req) |
|---|
| 57 | delicious_parse_xml(response.body) |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | delicious_save_cache date.strftime("%Y-%m-%d"), parsed |
|---|
| 61 | end |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | add_edit_proc do |date| |
|---|
| 65 | delicious_get_html date |
|---|
| 66 | nil |
|---|
| 67 | end |
|---|
| 68 | |
|---|
| 69 | add_body_leave_proc do |date| |
|---|
| 70 | path = "#{@cache_path}/delicious/#{date.strftime('%Y-%m-%d')}" |
|---|
| 71 | ret = '' |
|---|
| 72 | |
|---|
| 73 | if File.exist? path |
|---|
| 74 | File.open(path) do |file| |
|---|
| 75 | ret = <<-EOS |
|---|
| 76 | <h3>del.icio.us</h3> |
|---|
| 77 | <ul> |
|---|
| 78 | #{file.read} |
|---|
| 79 | </ul> |
|---|
| 80 | EOS |
|---|
| 81 | end |
|---|
| 82 | end |
|---|
| 83 | ret |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | def delicious_init |
|---|
| 88 | @conf['delicious.id'] ||= "" |
|---|
| 89 | @conf['delicious.pw'] ||= "" |
|---|
| 90 | @conf['delicious.title'] ||= "Todey's URL Clip" |
|---|
| 91 | end |
|---|
| 92 | |
|---|
| 93 | add_conf_proc( 'delicious', @delicious_label_conf ) do |
|---|
| 94 | delicious_conf_proc |
|---|
| 95 | end |
|---|
| 96 | |
|---|
| 97 | def delicious_conf_proc |
|---|
| 98 | if @mode == 'saveconf' then |
|---|
| 99 | @conf['delicious.id'] = @cgi.params['delicious.id'][0] |
|---|
| 100 | @conf['delicious.pw'] = @cgi.params['delicious.pw'][0] |
|---|
| 101 | end |
|---|
| 102 | |
|---|
| 103 | delicious_init |
|---|
| 104 | |
|---|
| 105 | <<-HTML |
|---|
| 106 | <h3>#{@delicious_label_id}</h3> |
|---|
| 107 | <p><input name="delicious.id" value="#{CGI::escapeHTML( @conf['delicious.id'] )}"></p> |
|---|
| 108 | <h3>#{@delicious_label_pw}</h3> |
|---|
| 109 | <p><input name="delicious.pw" value="#{CGI::escapeHTML( @conf['delicious.pw'] )}"></p> |
|---|
| 110 | HTML |
|---|
| 111 | end |
|---|