Changeset 14410 for platform/tdiary

Show
Ignore:
Timestamp:
06/22/08 14:58:26 (5 months ago)
Author:
hsbt
Message:

refactoring.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • platform/tdiary/plugin/section_footer2.rb

    r12922 r14410  
    66 
    77require 'digest/md5' 
     8require 'json/ext' 
     9require 'timeout' 
    810require 'open-uri' 
    9 require 'timeout' 
    1011require 'yaml' 
    1112require 'pathname' 
     
    1617        uri[0, 0] = @conf.base_url unless %r|^https?://|i =~ uri 
    1718        uri.gsub!( %r|/\./|, '/' ) 
    18         if escape then 
     19        if escape  
    1920                uri + CGI::escape(anchor( "#{ymd}p%02d" % index )) 
    2021        else 
     
    4344add_section_leave_proc do |date, index| 
    4445        r = '<div class="tags">' 
    45  
    4646        unless @conf.mobile_agent? then 
    4747                # add category_tag 
     
    4949                        r << "Tags: " 
    5050                        @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( /\]$/, '' ) << ' ' 
    5652                        end 
     53                        r << ' | ' 
    5754                end 
    58  
     55                 
    5956                # add del.icio.us link 
    60                 r << add_delicious_json(date, index) 
     57                r << add_delicious(date, index) 
    6158 
    6259                # add SBM link 
     
    6764 
    6865                # add Permalink 
    69                 r << add_permalink(date, index) 
     66                r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> | 
    7067        end 
    71  
    7268        r << "</div>\n" 
    7369end 
    7470 
    75 def add_delicious_json(date, index) 
    76         require 'json' 
     71def 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 
     83end 
    7784 
     85def add_delicious( date, index ) 
    7886        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}">| 
    8491 
    8592        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 
    8994 
    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 
    93109                        end 
    94110                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 ) 
    110113        end 
    111114 
    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>' 
    123116        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 > 0 
    127                 r << %Q| #{count} user| 
    128                 r << 's' if count > 1 
    129         end 
    130         r << '</a>' 
    131117 
    132118        return r 
     
    134120 
    135121def 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 = "" 
    137124 
    138         r = "" 
    139         unless config.nil? then 
     125        unless config.nil? 
    140126                title = config["title"][@conf.lang] 
    141                 r << ' | ' 
    142127                r << %Q|<a href="#{config["url"]}#{permalink(date, index)}">| 
    143128                r << %Q|<img src="#{config["src"]}" style="border: none;vertical-align: middle;" | 
    144129                r << %Q|title="#{title}" | 
    145130                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? 
    149132                r << '</a>' 
     133                r << ' | ' 
    150134        end 
    151135 
    152136        return r 
    153137end 
    154  
    155 def add_permalink(date, index) 
    156         r = " | " 
    157         r << %Q|<a href="#{permalink(date, index, false)}">Permalink</a> | 
    158         return r 
    159 end