Changeset 14410 for platform/tdiary
- Timestamp:
- 06/22/08 14:58:26 (5 months ago)
- Files:
-
- 1 modified
-
platform/tdiary/plugin/section_footer2.rb (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
platform/tdiary/plugin/section_footer2.rb
r12922 r14410 6 6 7 7 require 'digest/md5' 8 require 'json/ext' 9 require 'timeout' 8 10 require 'open-uri' 9 require 'timeout'10 11 require 'yaml' 11 12 require 'pathname' … … 16 17 uri[0, 0] = @conf.base_url unless %r|^https?://|i =~ uri 17 18 uri.gsub!( %r|/\./|, '/' ) 18 if escape then19 if escape 19 20 uri + CGI::escape(anchor( "#{ymd}p%02d" % index )) 20 21 else … … 43 44 add_section_leave_proc do |date, index| 44 45 r = '<div class="tags">' 45 46 46 unless @conf.mobile_agent? then 47 47 # add category_tag … … 49 49 r << "Tags: " 50 50 @category_to_tag_list.each do |tag, blog| 51 if blog 52 r << %Q|<a href="#{@index}?blogcategory=#{h tag}">#{tag}</a> | 53 else 54 r << category_anchor( "#{tag}" ).sub( /^\[/, '' ).sub( /\]$/, '' ) << ' ' 55 end 51 r << category_anchor( "#{tag}" ).sub( /^\[/, '' ).sub( /\]$/, '' ) << ' ' 56 52 end 53 r << ' | ' 57 54 end 58 55 59 56 # add del.icio.us link 60 r << add_delicious _json(date, index)57 r << add_delicious(date, index) 61 58 62 59 # add SBM link … … 67 64 68 65 # add Permalink 69 r << add_permalink(date, index)66 r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> | 70 67 end 71 72 68 r << "</div>\n" 73 69 end 74 70 75 def add_delicious_json(date, index) 76 require 'json' 71 def call_delicious_json( url_md5 ) 72 json = nil 73 begin 74 timeout(5) do 75 open( "http://badges.del.icio.us/feeds/json/url/data?hash=#{url_md5}" ) do |f| 76 json = JSON.parse( f.read ) 77 end 78 end 79 rescue => e 80 @conf.debug( e ) 81 end 82 return json 83 end 77 84 85 def add_delicious( date, index ) 78 86 url_md5 = Digest::MD5.hexdigest(permalink(date, index, false)) 79 cache_dir = "#{@cache_path}/delicious/#{date.strftime( "%Y%m" )}/" 80 file_name = "#{cache_dir}/#{url_md5}.json" 81 cache_time = 8 * 60 * 60 # 8 hour 82 update = false 83 count = 0 87 db_file = "#{@cache_path}/delicious.cache" 88 89 r = '' 90 r << %Q|<a href="http://del.icio.us/url/#{url_md5}"><img src="http://images.del.icio.us/static/img/delicious.small.gif" style="border: none;vertical-align: middle;" alt="#{@section_footer2_delicious_label}" title="#{@section_footer2_delicious_label}">| 84 91 85 92 begin 86 Dir::mkdir( cache_dir ) unless File::directory?( cache_dir ) 87 cached_time = nil 88 cached_time = File::mtime( file_name ) if File::exist?( file_name ) 93 cache_time = 8 * 60 * 60 # 12 hour 89 94 90 unless cached_time.nil? 91 if Time.now > cached_time + cache_time 92 update = true 95 PStore.new(db_file).transaction do |db| 96 entry = db[url_md5] 97 entry = { :count => 0, :update=> Time.at(0) } if entry.nil? 98 99 if Time.now > entry[:update] + cache_time 100 json = call_delicious_json( url_md5 ) 101 entry[:count] = json[0]["total_posts"].to_i unless json[0].nil? 102 entry[:update] = Time.now 103 db[url_md5] = entry 104 end 105 106 if entry[:count] > 0 107 r << %Q| #{entry[:count]} user| 108 r << 's' if entry[:count] > 1 93 109 end 94 110 end 95 96 if cached_time.nil? or update 97 begin 98 timeout(10) do 99 open( "http://badges.del.icio.us/feeds/json/url/data?hash=#{url_md5}" ) do |file| 100 File::open( file_name, 'wb' ) do |f| 101 f.write( file.read ) 102 end 103 end 104 end 105 rescue TimeoutError 106 rescue 107 end 108 end 109 rescue 111 rescue => e 112 @conf.debug( e ) 110 113 end 111 114 112 begin 113 File::open( file_name ) do |f| 114 data = JSON.parse(@conf.to_native( f.read, 'utf-8' )) 115 unless data[0].nil? 116 count = data[0]["total_posts"].to_i 117 end 118 end 119 rescue 120 end 121 122 r = '' 115 r << '</a>' 123 116 r << ' | ' 124 r << %Q|<a href="http://del.icio.us/url/#{url_md5}"><img src="http://images.del.icio.us/static/img/delicious.small.gif" style="border: none;vertical-align: middle;" alt="#{@section_footer2_delicious_label}" title="#{@section_footer2_delicious_label}">|125 126 if count > 0127 r << %Q| #{count} user|128 r << 's' if count > 1129 end130 r << '</a>'131 117 132 118 return r … … 134 120 135 121 def parse_sbm_yaml(file, date, index) 136 config = YAML.load(Pathname.new(file).expand_path.read) 122 config = YAML.load( Pathname.new( file ).expand_path.read ) 123 r = "" 137 124 138 r = "" 139 unless config.nil? then 125 unless config.nil? 140 126 title = config["title"][@conf.lang] 141 r << ' | '142 127 r << %Q|<a href="#{config["url"]}#{permalink(date, index)}">| 143 128 r << %Q|<img src="#{config["src"]}" style="border: none;vertical-align: middle;" | 144 129 r << %Q|title="#{title}" | 145 130 r << %Q|alt="#{title}" />| 146 unless config["counter"].nil? then 147 r << %Q| <img src="#{config["counter"]}#{permalink(date, index)}" style="border: none;vertical-align: middle;" />| 148 end 131 r << %Q| <img src="#{config["counter"]}#{permalink(date, index)}" style="border: none;vertical-align: middle;" />| unless config["counter"].nil? 149 132 r << '</a>' 133 r << ' | ' 150 134 end 151 135 152 136 return r 153 137 end 154 155 def add_permalink(date, index)156 r = " | "157 r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> |158 return r159 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)