root/platform/tdiary/plugin/section_footer2.rb @ 29375

Revision 29375, 3.4 kB (checked in by hsbt, 4 years ago)

supported ruby 1.9.1.

Line 
1# section_footer2.rb
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 = {}
29        ''
30end
31
32alias subtitle_link_original subtitle_link
33def subtitle_link( date, index, subtitle )
34        s = ''
35        if subtitle then
36                s = subtitle.sub( /^(?:\[[^\[]+?\])+/ ) do
37                        $&.scan( /\[(.*?)\]/ ) do |tag|
38                                @category_to_tag_list[tag] = false # false when diary
39                        end
40                        ''
41                end
42        end
43        subtitle_link_original( date, index, s.strip )
44end
45
46add_section_leave_proc do |date, index|
47        unless @conf.mobile_agent? or @conf.iphone? or feed? or bot?
48                r = '<div class="tags">'
49                # add category_tag
50                if @category_to_tag_list and not @category_to_tag_list.empty? then
51                        r << "Tags: "
52                        @category_to_tag_list.each do |tag, blog|
53                                r << category_anchor( "#{tag}" ).sub( /^\[/, '' ).sub( /\]$/, '' ) << ' '
54                        end
55                        r << ' | '
56                end
57
58                # add del.icio.us link
59                r << add_delicious(date, index)
60
61                # add SBM link
62                yaml_dir = "#{@cache_path}/yaml/"
63                Dir.glob( yaml_dir + "*.yaml" ) do |file|
64                        r << parse_sbm_yaml(file, date, index)
65                end
66
67                # add Permalink
68                r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> |
69
70                r << "</div>\n"
71        end
72end
73
74def call_delicious_json( url_md5 )
75        json = nil
76        begin
77                timeout(10) do
78                        open( "http://feeds.delicious.com/v2/json/urlinfo/#{url_md5}" ) do |f|
79                                json = JSON.parse( f.read )
80                        end
81                end
82        rescue => e
83                @conf.debug( e )
84        end
85        return json
86end
87
88def add_delicious( date, index )
89        url_md5 = Digest::MD5.hexdigest(permalink(date, index, false))
90        db_file = "#{@cache_path}/delicious.cache"
91
92        r = ''
93        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}">|
94
95        begin
96                cache_time = 8 * 60 * 60  # 12 hour
97
98                PStore.new(db_file).transaction do |db|
99                        entry = db[url_md5]
100                        entry = { :count => 0, :update=> Time.at(0) } if entry.nil?
101                       
102                        if Time.now > entry[:update] + cache_time
103                                json = call_delicious_json( url_md5 )
104                                entry[:count] = json[0]["total_posts"].to_i unless json[0].nil?
105                                entry[:update] = Time.now
106                                db[url_md5] = entry
107                        end
108
109                        if entry[:count] > 0
110                                r << %Q| #{entry[:count]} user|
111                                r << 's' if entry[:count] > 1
112                        end
113                end
114        rescue => e
115                @conf.debug( e )
116        end
117
118        r << '</a>'
119        r << ' | '
120
121        return r
122end
123
124def parse_sbm_yaml(file, date, index)
125        config = YAML.load( Pathname.new( file ).expand_path.read )
126        r = ""
127
128        unless config.nil?
129                title = config["title"][@conf.lang]
130                r << %Q|<a href="#{config["url"]}#{permalink(date, index)}">|
131                r << %Q|<img src="#{config["src"]}" style="border: none;vertical-align: middle;" |
132                r << %Q|title="#{title}" |
133                r << %Q|alt="#{title}" />|
134                r << %Q| <img src="#{config["counter"]}#{permalink(date, index)}" style="border: none;vertical-align: middle;" />| unless config["counter"].nil?
135                r << '</a>'
136                r << ' | '
137        end
138
139        return r
140end
Note: See TracBrowser for help on using the browser.