root/platform/tdiary/plugin/section_footer2.rb

Revision 17023, 3.4 kB (checked in by hsbt, 4 months ago)

modified delicious.com url, and timeout value.

Line 
1# section_footer2.rb $Revision 1.0 $
2#
3# Copyright (c) 2008 SHIBATA Hiroshi <h-sbt@nifty.com>
4# You can redistribute it and/or modify it under GPL2.
5#
6
7require 'digest/md5'
8require 'rubygems'
9require 'json/ext'
10require 'timeout'
11require 'open-uri'
12require 'yaml'
13require 'pathname'
14
15def permalink( date, index, escape = true )
16        ymd = date.strftime( "%Y%m%d" )
17        uri = @conf.index.dup
18        uri[0, 0] = @conf.base_url unless %r|^https?://|i =~ uri
19        uri.gsub!( %r|/\./|, '/' )
20        if escape
21                uri + CGI::escape(anchor( "#{ymd}p%02d" % index ))
22        else
23                uri + anchor( "#{ymd}p%02d" % index )
24        end
25end
26
27add_section_enter_proc do |date, index|
28        @category_to_tag_list = {}
29end
30
31alias subtitle_link_original subtitle_link
32def subtitle_link( date, index, subtitle )
33        s = ''
34        if subtitle then
35                s = subtitle.sub( /^(?:\[[^\[]+?\])+/ ) do
36                        $&.scan( /\[(.*?)\]/ ) do |tag|
37                                @category_to_tag_list[tag] = false # false when diary
38                        end
39                        ''
40                end
41        end
42        subtitle_link_original( date, index, s.strip )
43end
44
45add_section_leave_proc do |date, index|
46        unless @conf.mobile_agent? or @conf.iphone? or feed? or bot?
47                r = '<div class="tags">'
48                # add category_tag
49                if @category_to_tag_list and not @category_to_tag_list.empty? then
50                        r << "Tags: "
51                        @category_to_tag_list.each do |tag, blog|
52                                r << category_anchor( "#{tag}" ).sub( /^\[/, '' ).sub( /\]$/, '' ) << ' '
53                        end
54                        r << ' | '
55                end
56
57                # add del.icio.us link
58                r << add_delicious(date, index)
59
60                # add SBM link
61                yaml_dir = "#{@cache_path}/yaml/"
62                Dir.glob( yaml_dir + "*.yaml" ) do |file|
63                        r << parse_sbm_yaml(file, date, index)
64                end
65
66                # add Permalink
67                r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> |
68
69                r << "</div>\n"
70        end
71end
72
73def call_delicious_json( url_md5 )
74        json = nil
75        begin
76                timeout(10) do
77                        open( "http://feeds.delicious.com/v2/json/urlinfo/#{url_md5}" ) do |f|
78                                json = JSON.parse( f.read )
79                        end
80                end
81        rescue => e
82                @conf.debug( e )
83        end
84        return json
85end
86
87def add_delicious( date, index )
88        url_md5 = Digest::MD5.hexdigest(permalink(date, index, false))
89        db_file = "#{@cache_path}/delicious.cache"
90
91        r = ''
92        r << %Q|<a href="http://delicious.com/url/#{url_md5}"><img src="http://static.delicious.com/img/delicious.small.gif" style="border: none;vertical-align: middle;" alt="#{@section_footer2_delicious_label}" title="#{@section_footer2_delicious_label}">|
93
94        begin
95                cache_time = 8 * 60 * 60  # 12 hour
96
97                PStore.new(db_file).transaction do |db|
98                        entry = db[url_md5]
99                        entry = { :count => 0, :update=> Time.at(0) } if entry.nil?
100                       
101                        if Time.now > entry[:update] + cache_time
102                                json = call_delicious_json( url_md5 )
103                                entry[:count] = json[0]["total_posts"].to_i unless json[0].nil?
104                                entry[:update] = Time.now
105                                db[url_md5] = entry
106                        end
107
108                        if entry[:count] > 0
109                                r << %Q| #{entry[:count]} user|
110                                r << 's' if entry[:count] > 1
111                        end
112                end
113        rescue => e
114                @conf.debug( e )
115        end
116
117        r << '</a>'
118        r << ' | '
119
120        return r
121end
122
123def parse_sbm_yaml(file, date, index)
124        config = YAML.load( Pathname.new( file ).expand_path.read )
125        r = ""
126
127        unless config.nil?
128                title = config["title"][@conf.lang]
129                r << %Q|<a href="#{config["url"]}#{permalink(date, index)}">|
130                r << %Q|<img src="#{config["src"]}" style="border: none;vertical-align: middle;" |
131                r << %Q|title="#{title}" |
132                r << %Q|alt="#{title}" />|
133                r << %Q| <img src="#{config["counter"]}#{permalink(date, index)}" style="border: none;vertical-align: middle;" />| unless config["counter"].nil?
134                r << '</a>'
135                r << ' | '
136        end
137
138        return r
139end
Note: See TracBrowser for help on using the browser.